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
 Problems with Getdate()

Author  Topic 

Pinto
Aged Yak Warrior

590 Posts

Posted - 2006-07-18 : 06:15:21
I have the following query and it should return a record. It works if I remove >= GETDATE() and have the criteria as =2, but if I add the date criteria it returns no records. The date in the table is 18/07/2006

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2006-07-18 : 06:17:51
Possibility comparison condition is failing due to time part...use convert like below to truncate time part from date:

convert(varchar(10),getdate(),101) = convert(varchar(10),DateCol,101)

Harsh Athalye
India.
"Nothing is Impossible"
Go to Top of Page

Pinto
Aged Yak Warrior

590 Posts

Posted - 2006-07-18 : 06:24:34
I found this which now returns my record

dateadd(day,datediff(day,0,GETDATE()), 0)
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-07-21 : 11:46:51
quote:
Originally posted by harsh_athalye

Possibility comparison condition is failing due to time part...use convert like below to truncate time part from date:

convert(varchar(10),getdate(),101) = convert(varchar(10),DateCol,101)

Harsh Athalye
India.
"Nothing is Impossible"


To omit time part dont use convert function. Use DateAdd and DateDiff

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2006-07-21 : 11:58:13
quote:
Originally posted by madhivanan
[brTo omit time part dont use convert function. Use DateAdd and DateDiff

Madhivanan

Failing to plan is Planning to fail



Madhivanan,

Just as a curiosity, I wanted to know whether there is any performance penalty associated with using convert() than datediff/dateadd() combination...b'coz at my previous assignment I have seen convert used in almost every case to omit time part ??


Harsh Athalye
India.
"Nothing is Impossible"
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-07-21 : 12:12:23
Goto Script Library and look for MVJ's date related topics. You can find that convert becomes slow and ineffecient if there are thousands of rows. Also you need to convert it back to datetime to make it as Date

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2006-07-21 : 15:34:57
quote:
Originally posted by harsh_athalye

quote:
Originally posted by madhivanan
[brTo omit time part dont use convert function. Use DateAdd and DateDiff

Madhivanan

Failing to plan is Planning to fail



Madhivanan,

Just as a curiosity, I wanted to know whether there is any performance penalty associated with using convert() than datediff/dateadd() combination...b'coz at my previous assignment I have seen convert used in almost every case to omit time part ??


Harsh Athalye
India.
"Nothing is Impossible"



I ran testing with a number of different methods of removing the time part of datetime where I converted a million datetimes. I found that datediff/dateadd() was much faster then any other method, and that using convert to a string and back to datetime was by far the slowest.


CODO ERGO SUM
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2006-07-21 : 15:40:18
quote:
Originally posted by Pinto

I have the following query and it should return a record. It works if I remove >= GETDATE() and have the criteria as =2, but if I add the date criteria it returns no records. The date in the table is 18/07/2006



It would be nice if you posted the query...read the hint link in my sig



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2006-07-24 : 11:58:23
Oh, the other problem with GetDate() (I once read somewhere) is that it still leaves you alone on a Saturday night....



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam
Go to Top of Page
   

- Advertisement -