SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Date Time Fields
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

rnelsch
Starting Member

USA
31 Posts

Posted - 04/25/2012 :  15:50:18  Show Profile  Reply with Quote
Its been years since I've written a query to include a date but ignore the time. Out field has both date and time built in, ie. 4/25/2012 9:11:42 AM and I want to run a query using the "Where" clause for the date but not the time? Any suggestions?



Ryan A Nelsch

Lamprey
Flowing Fount of Yak Knowledge

3831 Posts

Posted - 04/25/2012 :  17:02:14  Show Profile  Reply with Quote
Assuming you want all rows with a date equal to 2012-04-25, then something like:
SELECT *
FROM TableName
WHERE DateTimeColumn >= '20120425'
AND DateTimeColumn < '20120426'
Go to Top of Page

sql-programmers
Posting Yak Master

USA
189 Posts

Posted - 04/25/2012 :  23:13:55  Show Profile  Reply with Quote

Hi,

DECLARE @STARTDATE DATETIME = '4/25/2012 9:11:42 AM'
DECLARE @ENDDATE DATETIME = '4/26/2012 9:11:42 AM'
SELECT @STARTDATE, @ENDDATE
SELECT CONVERT(VARCHAR, @STARTDATE, 112), CONVERT(VARCHAR, @ENDDATE, 112)
FROM TableName
WHERE CONVERT(VARCHAR, DateTimeColumn, 112) >= CONVERT(VARCHAR, @STARTDATE, 112)
AND CONVERT(VARCHAR, DateTimeColumn, 112) < CONVERT(VARCHAR, @ENDDATE, 112)




SQL Server Programmers and Consultants
http://www.sql-programmers.com/
Go to Top of Page

khtan
In (Som, Ni, Yak)

Singapore
16746 Posts

Posted - 04/25/2012 :  23:24:44  Show Profile  Reply with Quote
quote:
Originally posted by sql-programmers


WHERE CONVERT(VARCHAR, DateTimeColumn, 112) >= CONVERT(VARCHAR, @STARTDATE, 112)
AND   CONVERT(VARCHAR, DateTimeColumn, 112) < CONVERT(VARCHAR, @ENDDATE, 112)


SQL Server Programmers and Consultants
http://www.sql-programmers.com/



Applying function on the column will result in the engine not able to utilize any index on the DateTimeColumn.

The method posted by Lamprey is recommended.



KH
Time is always against us

Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22461 Posts

Posted - 04/26/2012 :  08:03:39  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
More examples
http://beyondrelational.com/modules/2/blogs/70/posts/10899/understanding-datetime-column-part-iii.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000