Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL Query Help: WHERE ([fielddate]) = (GETDATE()) 2

Status
Not open for further replies.

SW2004

Technical User
Jul 13, 2004
24
GB
Hi,

I'm trying to find all faults in my helpdesk database that have been completed today. The query below doesn't return anything even though there are completed faults today. Can anyone help? Thanks.

SELECT [date fault reported], [date fault completed], [fault number], [status] FROM [fault table]
WHERE ([date fault completed]) = (GETDATE())
 
i always use this...
Code:
where convert(char(10),datefield,101) = convert(char(10),getdate(),101)
 
Karl, can you add CONVERT(date) to axis of evil (tm) list? [smile]

How about:

WHERE DATEDIFF(dd, [date fault completed], GETDATE()) = 0


-----
Math problems? Call 0800-[(10x)(13i)^2]-[sin(xy)/2.362x]
 
First I'll put it on the Watch List and elevate the SQL Alerts to Code Yellow. Should sufficient evidence of DOS attacks against SQL Servers surface, it will indeed be added to the Axis whereupon you will be immediately notified. However, it may have to wait until I can get this to run.
Code:
[Blue]SELECT[/Blue] [Fuchsia]Convert[/Fuchsia][Gray]([/Gray]PhoneNumber[Gray],[/Gray][red]'0800-[(10x)(13i)^2]-[sin(xy)/2.362x]'[/red][Gray],[/Gray][Fuchsia]Pi[/Fuchsia][Gray])[/Gray]
-Karl

[red] Cursors, triggers, user-defined functions and dynamic SQL are an axis of evil![/red]
[green]Life's uncertain...eat dessert first...www.deerfieldbakery.com[/green]
 
FYI, using a function to search for a certain day's data will mean that any indexes created over that date column cannot be utilised. If your tables start getting large then the performance of the query may start to decline.

A better idea may be to use the following query, which will allow an index to be used if necessary:

Code:
SELECT [date fault reported], [date fault completed], [fault number], [status]
FROM [fault table]
WHERE [date fault completed] >= CONVERT(varchar(8), GETDATE(), 112)
	AND [date fault completed] < CONVERT(varchar(8), GETDATE() + 1, 112)

--James
 
Good point. And good query - CONVERT() only over GETDATE(), used ISO format (112) etc.

I thought SQL Server converts function calls into range retrieval operators (from-to) when possible.

------
Math problems? Call 0800-[(10x)(13i)^2]-[sin(xy)/2.362x]
 
vg,

I know why cursors, "in (...)", and goto's are part of the Axis of Evil. Just to expand my own knowledge, why does Convert() deserve to be a member of the Axis of Evil? Is it because it is slow? And, is the related function Cast() also a candidate for the Axis of Evil?
 
It doesn't. We were just kidding...for the most part. However, the date math functions are faster and the only more efficient use of Convert that I can think of (aside from formatting) is to convert seconds, minutes, etc. into hh:mm:ss.
-Karl

[red] Cursors, triggers, user-defined functions and dynamic SQL are an axis of evil![/red]
[green]Life's uncertain...eat dessert first...www.deerfieldbakery.com[/green]
 
Karl

should my link to the 150 functions be a candidate for the Axis of Evil ?

[bandito] [blue]DBomrrsm[/blue] [bandito]

[blue]Software code, like laws and sausages, should never be examined in production[/blue][black] - [/black][purple]Edward Tenner[/purple]
 
Definitely![alien]
-Karl

[red] Cursors, triggers, user-defined functions and dynamic SQL are an axis of evil![/red]
[green]Life's uncertain...eat dessert first...www.deerfieldbakery.com[/green]
 
Hey! I found INC() very very useful [upsidedown]

------
Math problems? Call 0800-[(10x)(13i)^2]-[sin(xy)/2.362x]
 
vg,

Don't know if you were responding to my post, but what I meant was "in (select...)". Instead of that I use "Exists (select...)" with a correlated subquery (much faster). "in (select...)" should be in the axis of evil.

[peace]
 
No... that was reply to "150 functions" thing... I'm joking too much these days :X

------
Math problems? Call 0800-[(10x)(13i)^2]-[sin(xy)/2.362x]
 
No prob. But I do think "in (select...)" is a candidate for the Axis of Evil.

[noevil]
 
Vongrunt, didn't you demonstrate that IN (Select...) can be efficient? I think it was that long thread that I wanted you to turn into a FAQ. It's now lost forever in the bit bucket! :)
-Karl

[red] Cursors, triggers, user-defined functions and dynamic SQL are an axis of evil![/red]
[green]Life's uncertain...eat dessert first...www.deerfieldbakery.com[/green]
 
Only just got to INC() - got to be the most useful function of all time - bet you it is as fast as hell too !!

[bandito] [blue]DBomrrsm[/blue] [bandito]

[blue]Software code, like laws and sausages, should never be examined in production[/blue][black] - [/black][purple]Edward Tenner[/purple]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top