Author |
Topic |
RichardSteele
Posting Yak Master
160 Posts |
Posted - 2007-09-09 : 22:32:36
|
What would the proper way to write the following query be? Thanks in advance.Select * from mydb where smalldatetimefield > today's date. |
|
Zoroaster
Aged Yak Warrior
702 Posts |
Posted - 2007-09-09 : 22:52:08
|
quote: Originally posted by RichardSteele What would the proper way to write the following query be? Thanks in advance.Select * from mydb where smalldatetimefield > today's date.
Not sure exactly what you are asking, it can't be greater than todays date ever but it can be greater than or equal to, you can also convert the current date's time to 00:00:00 so that it counts from start of day. See following:SELECT *FROM MyDBWHERE SMALLDATETIMEFIELD >= CAST(CONVERT(varchar(10),GETDATE(),1)AS SMALLDATETIME) Future guru in the making. |
|
|
RichardSteele
Posting Yak Master
160 Posts |
Posted - 2007-09-09 : 22:58:09
|
That's perfect. Thanks! |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-10 : 05:53:46
|
Always try to keep original datatypes.SELECT *FROM MyDBWHERE SMALLDATETIMEFIELD >= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0) E 12°55'05.25"N 56°04'39.16" |
|
|
Zoroaster
Aged Yak Warrior
702 Posts |
Posted - 2007-09-10 : 09:39:46
|
quote: Originally posted by Peso Always try to keep original datatypes.SELECT *FROM MyDBWHERE SMALLDATETIMEFIELD >= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0) E 12°55'05.25"N 56°04'39.16"
I didn't think of that, but I like it. Future guru in the making. |
|
|
X002548
Not Just a Number
15586 Posts |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-10 : 10:27:57
|
Even when not used over a column, just a function? E 12°55'05.25"N 56°04'39.16" |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2007-09-10 : 10:32:32
|
quote: Originally posted by X002548 And just to be clear, but using DATEDIFF would cause a scan I believe
????- Jeffhttp://weblogs.sqlteam.com/JeffS |
|
|
X002548
Not Just a Number
15586 Posts |
|
jsmith8858
Dr. Cross Join
7423 Posts |
|
Zoroaster
Aged Yak Warrior
702 Posts |
Posted - 2007-09-10 : 11:45:33
|
So, which way is better? Future guru in the making. |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2007-09-10 : 12:24:27
|
quote: Originally posted by Zoroaster So, which way is better? Future guru in the making.
This one:SELECT *FROM MyDBWHERE SMALLDATETIMEFIELD >= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0)Remember: when you CONVERT, it is NO LONGER A DATETIME -- it just a VARCHAR. Using the above formula simply returns a true datetime value, rounded to midnight of the current date, which requires no conversions and will always be more efficient.- Jeffhttp://weblogs.sqlteam.com/JeffS |
|
|
Kristen
Test
22859 Posts |
Posted - 2007-09-10 : 12:31:40
|
AlthoughDATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0)probably looks like a complicated way to just chop off the time part there has been discussion about the fastest way to do this, and this method came out fastest (mainly because it avoids things like "convert-to-char-and-back-to-datetime-again" ), which is why it is often quoted on here.For the performance test see here: http://www.sqlteam.com/Forums/topic.asp?TOPIC_ID=38940#120953Kristen |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2007-09-10 : 12:37:50
|
>>probably looks like a complicated way to just chop off the time partJust remember that we never chop off the time part, we just round to midnight. Like rounding a MONEY value from $12.8396 to $12.0000 -- the decimal portion is always there.- Jeffhttp://weblogs.sqlteam.com/JeffS |
|
|
Zoroaster
Aged Yak Warrior
702 Posts |
Posted - 2007-09-10 : 12:42:50
|
quote: Originally posted by jsmith8858 >>probably looks like a complicated way to just chop off the time partJust remember that we never chop off the time part, we just round to midnight. Like rounding a MONEY value from $12.8396 to $12.0000 -- the decimal portion is always there.- Jeffhttp://weblogs.sqlteam.com/JeffS
I miss the date function in Oracle sometimes Future guru in the making. |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
|
Kristen
Test
22859 Posts |
|
Zoroaster
Aged Yak Warrior
702 Posts |
|
Zoroaster
Aged Yak Warrior
702 Posts |
Posted - 2007-09-10 : 15:42:48
|
quote: Originally posted by Kristen "I miss the date function in Oracle sometimes"Well MS has gone completely overboard on the next version of SQL Server, including inconsistent naming and complex-ifying the whole 9-yards See also: http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=85099#323598Kristen
Interesting, I just got a copy of Katmai beta, will start playing with it soon. Future guru in the making. |
|
|
|