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
 Convert datetime but not to VARCHAR
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

dibblejon
Starting Member

35 Posts

Posted - 04/25/2012 :  09:52:57  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

Germany
8513 Posts

Posted - 04/25/2012 :  09:55:58  Show Profile  Visit webfred's Homepage  Reply with Quote
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

Norway
3241 Posts

Posted - 04/25/2012 :  09:56:28  Show Profile  Reply with Quote
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

35 Posts

Posted - 04/25/2012 :  10:04:00  Show Profile  Reply with Quote
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

35 Posts

Posted - 04/25/2012 :  10:13:40  Show Profile  Reply with Quote
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
  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.08 seconds. Powered By: Snitz Forums 2000