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
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Date conversion help

Author  Topic 

snejsnej
Starting Member

9 Posts

Posted - 2015-04-08 : 19:57:02
Hi,

I have a date in the form of '2015-02-16 00:00:00.000' that I'd like to convert to 15Feb16 (yymmmdd), preferably keeping the date data type. I was able to convert it to a string, but that greatly complicates what I have to do afterwards.

I looked in a few different locations for help including:
https://msdn.microsoft.com/en-us/library/ms187928.aspx

...but I wasn't able to get what I need.

Any help greatly appreciated.
Jens








Kristen
Test

22859 Posts

Posted - 2015-04-08 : 21:40:22
quote:
Originally posted by snejsnej

I have a date in the form of '2015-02-16 00:00:00.000'


is the date that you have in a DATE or DATETIME datatype column? or is it a text string date?

Its probably the former and the format you have shown is probably just the default display??

Folk will tell you that you should not format the date in SQL - as you have already said doing that will change it from DATE datatype to STRING and then anything downstream of you won't be able to sort / manipulate the date as a date (without using some conversion).

If you need to format the date in SQL you can use the CONVERT function

https://msdn.microsoft.com/en-us/library/ms187928.aspx

If you are using SQL2012 or newer you can also use the FORMAT function

https://msdn.microsoft.com/en-GB/library/hh213505.aspx
Go to Top of Page

snejsnej
Starting Member

9 Posts

Posted - 2015-04-09 : 01:32:43
Hi Kristen - the FORMAT function worked, thanks very much!
Jens
Go to Top of Page
   

- Advertisement -