Blog Home  Home Feed your aggregator (RSS 2.0)  
Implements IVillage - Querying DateTime in SQL Server without Time
It takes a village to keep up with .Net
 
 Wednesday, April 04, 2007

I wanted to share this bit of SQL wisdom I picked up a few months ago on the ASP.Net forums (http://forums.asp.net). Let's say you have an orders table and you have an orderDate column that contains the Date & Time of the order.  If you wanted to create a query to select all orders made yesterday at any time, I cameup with this working (if not long winded) method:

 Where orderDate = CAST(MONTH(DATEADD(day, - 1, GETDATE())) AS varchar) + '/' + CAST(DAY(DATEADD(day, - 1, GETDATE())) AS varchar) + '/' + CAST(YEAR(DATEADD(day, - 1, GETDATE())) AS varchar))

I was treated to an extreme stream lining of this on the forums.  The resulting query was:

Where orderDate >= convert(Varchar, Getdate() -1, 101) And orderDate < convert(Varchar, Getdate() , 101)
 
The use of convert with the right format number negates a lot of work and is much easier to read.
Thursday, January 10, 2008 8:20:07 AM (Eastern Standard Time, UTC-05:00)
or you could do it in a slightly shorter way: http://mitch-wheat.blogspot.com/2007/05/sql-server-compare-date-part-of.html
Sunday, May 24, 2009 7:49:16 AM (Eastern Daylight Time, UTC-04:00)
Greeting. You should not live one way in private, another in public. Help me! I find sites on the topic: Turbo Tax. I found only this - <a href="http://turbo-tax.biz">turbo tax</a>. Drug rehab and addiction treatment advice and support. Some form of drug and alcohol rehab treatment is necessary for the majority of addicts to the residential treatment for alcohol or drug abuse cost. Thank :eek: Reese from Poland.
Name
E-mail
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):

Copyright © 2010 Christian M Loris. All rights reserved.
DasBlog 'Portal' theme by Johnny Hughes.