Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Pull records on basis of datetime field

Author  Topic 

XMarshall
Starting Member

1 Post

Posted - 2008-12-22 : 05:39:16
Hi,
I am trying to pull data from the datetime type field -"Tran_Date" and which contains data like this - "2008-12-18 16:34:26.717"

I am writing the following queries, but none of them are returning any data:

select * from MonthlyExpense.dbo.MonthlyExpense
where Tran_Date=CONVERT(datetime,'2008-12-18 1:55:51 PM',121)

OR

select * from MonthlyExpense.dbo.MonthlyExpense
where Tran_Date =CAST('2008-12-18 16:34:26.717' as datetime)

I also want data to be retrieved based on the Date part of the datetime field. Pls help.

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2008-12-22 : 05:48:44
select * from MonthlyExpense.dbo.MonthlyExpense
where Tran_Date = '2008-12-1816:34:26.717'

u didn't require to convert date into datetime
try like above query
Go to Top of Page

raky
Aged Yak Warrior

767 Posts

Posted - 2008-12-22 : 05:49:00
try this

select * from MonthlyExpense.dbo.MonthlyExpense
where dateadd(dd,datediff(dd,0,Tran_Date),0) = '2008-12-18'

The above query will retrieve all records which has tran_date as Dec 18th, 2008 irrespective of time ...
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-12-22 : 05:56:03
Raky, this is simpler

where datediff(dd, Tran_Date, '2008-12-18') = 0

but still doesn't give you access to good index use.
This below let's you use index (if any present) in a good way.
select	*
from MonthlyExpense.dbo.MonthlyExpense
where Tran_Date >= '2008-12-18'
and Tran_Date < '2008-12-19'




E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page
   

- Advertisement -