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 datetime but not to VARCHAR

Author  Topic 

dibblejon
Starting Member

36 Posts

Posted - 2012-04-25 : 09:52:57
Hi

I need to convert a datetime column in a table which pulls it's data in from a third party app.

It's raw state is 2011-11-25 07:33:00.000

I want to convert this so I just get the YYYY-MM-DD and I want to retain as a date formatted field.

Apologies if this has been covered a million times before!

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-04-25 : 09:55:58
select
convert(date,'2011-11-25 07:33:00.000')



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

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2012-04-25 : 09:56:28
If your'e on sql 2008 you could simply do select cast('2011-11-25 07:33:00.000' as date). If you're on 2005 or older you have to have the time-part in there as well if you want to use a datetime datatype

- Lumbago
My blog-> http://thefirstsql.com/2011/07/08/how-to-find-gaps-in-identity-columns-at-the-speed-of-light/
Go to Top of Page

dibblejon
Starting Member

36 Posts

Posted - 2012-04-25 : 10:04:00
Please forgive my ignorance.... Or poor explanation

I have just run:

SELECT        CONVERT(date, CALLDATEANDTIME) AS Date
FROM dbo.CALLS


But get an error saying Error Message: Cannot call methods on date

The field called CALLDATEANDTIME is the field I am trying to convert.

What am I doing incorrectly?
Go to Top of Page

dibblejon
Starting Member

36 Posts

Posted - 2012-04-25 : 10:13:40
Found what I needed

DATEADD(dd, 0, DATEDIFF(dd, 0, CALLDATEANDTIME))

Returns YYYY-MM-DD 00:00:00.000

Which is fine for what we need.
Go to Top of Page
   

- Advertisement -