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
 Reason
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

mani_1234
Starting Member

India
24 Posts

Posted - 10/04/2012 :  06:41:22  Show Profile  Reply with Quote
i have table struct like this
create table #t
( a datetime )
a
--------------------------------------------
2012-10-04 15:33:27.560
2012-10-04 15:33:30.613
2012-10-04 15:33:32.800
2012-10-05 15:33:40.327
2012-10-04 15:39:57.433
2012-10-04 15:41:30.997

following are the queries ..
1.select * from #t
where CONVERT(varchar(10),a,120) ='2012-10-04'
2.select * from #t
where a like '2012-10-04%'
query 1 returns the correct ans. bt 2nd query s not returing ..
Any reason for output ?

sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 10/04/2012 :  06:49:30  Show Profile  Reply with Quote
The reason for what you are observing is that datetime is not stored as a string in the database. The internal representation for datetime is an 8-byte float that includes the date part and time part.

If you are looking for all the rows which have the date part = 2012-10-04, query it like this:
SELECT * FROM #t
WHERE a >= '20121004' AND a < '20121005';
Go to Top of Page

DonAtWork
Flowing Fount of Yak Knowledge

2161 Posts

Posted - 10/04/2012 :  07:05:14  Show Profile  Reply with Quote
I always liked this one:
SELECT dateadd(dd,datediff(dd,0,getdate()),0)









How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Go to Top of Page

sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 10/04/2012 :  07:08:47  Show Profile  Reply with Quote
quote:
Originally posted by DonAtWork

I always liked this one:
SELECT dateadd(dd,datediff(dd,0,getdate()),0)


Is it October 4th already!?! I thought that was a random date that OP picked out of thin air!!
Go to Top of Page

mani_1234
Starting Member

India
24 Posts

Posted - 10/04/2012 :  07:26:21  Show Profile  Reply with Quote
ohk thnx i got the point
Go to Top of Page

DonAtWork
Flowing Fount of Yak Knowledge

2161 Posts

Posted - 10/04/2012 :  08:54:44  Show Profile  Reply with Quote
quote:
Originally posted by sunitabeck
Is it October 4th already!?! I thought that was a random date that OP picked out of thin air!!


HA! i didn't realize it was October 4th either.








How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22744 Posts

Posted - 10/05/2012 :  03:08:00  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
Read this series of posts for better understanding http://beyondrelational.com/modules/2/blogs/70/posts/10897/understanding-datetime-column-part-i.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