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
 Convert VARCHAR to DATETIME the run select

Author  Topic 

ECML_David
Starting Member

2 Posts

Posted - 2012-03-01 : 10:07:23
Hi All,

First post so thanks for having me :)

i have a database that is holding dates in VARCHAR in the format:
16/08/2010 10:31:22

I want to be able to select all records that have been created today but I'm stuggling to get my head around converting the VARCHAR then running the SELECT statment against it. I dont want to alter all the records just convert them for the purpose of the SELECT

Any help very gratefuly recieved:

select * from opencall where(convert(datetime,logdate,101)) < GETDATE()

X002548
Not Just a Number

15586 Posts

Posted - 2012-03-01 : 10:17:13
WHERE DATEDIFF(dd,GetDate(),CONVERT(datetime,logdate)) = 0

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


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-03-01 : 10:20:12
try 103 instead of 101


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

ECML_David
Starting Member

2 Posts

Posted - 2012-03-01 : 10:43:48
OK so this seems to work beter:

select * from opencall DATEDIFF(dd,GetDate(),CONVERT(datetime,logdate,103)) = 0

however i get the message:

"dd" is not a recognized table hints option. If it is intended as a parameter to a table-valued function ensure that your database mode is set to compatabilty 90

server is SQL 2005
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-03-01 : 10:53:38
you forgot the WHERE clause

And get rid of the format for the conversion

SELECT *
FROM opencall
WHERE DATEDIFF(dd,GetDate(),CONVERT(datetime,logdate)) = 0

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


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-03-01 : 10:59:42
quote:
Originally posted by ECML_David

Hi All,

First post so thanks for having me :)

i have a database that is holding dates in VARCHAR in the format:
16/08/2010 10:31:22

I want to be able to select all records that have been created today but I'm stuggling to get my head around converting the VARCHAR then running the SELECT statment against it. I dont want to alter all the records just convert them for the purpose of the SELECT

Any help very gratefuly recieved:

select * from opencall where(convert(datetime,logdate,101)) < GETDATE()


By doing so you're really making it hard for yourself. Always try to use proper datatype for fields. Storing date values as varchar will really make date manipulations difficult and also causes problems if you're storing dates in varying formats

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-03-01 : 11:17:37
Yes that is true..but sometime you have to play the hand your dealt

SELECT *
FROM opencall
WHERE DATEDIFF(dd,GetDate(),CASE WHEN ISDATE(logdate) = 1 THEN CONVERT(datetime,logdate) ELSE GETDATE() END) = 0


And what does

SELECT * FROM opencall WHERE ISDATE(logdate) = 0

Give you?



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


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page
   

- Advertisement -