简体   繁体   中英

convert date format from mmddyy to yyyy-mm-dd hh:mm:sec

I have a date format which I extracted from filename. this date is in format 'mmddyy' .

I want to convert it into 'yyyy-mm-dd hh:mm:sec' format.

For example Date = 083115 -- mm = 08,dd=31,yy=15

Result date = 2015-08-31:00:00:00 time will always be 00:00:00

I am using SQL server 2012

thanks

Here is a solution

DECLARE @Date nvarchar(10)
SET @Date = '083115'
SELECT  CONVERT(datetime,RIGHT(@Date,2)+LEFT(@Date,2)+SUBSTRING(@Date,3,2))

this will give you 2015-08-31 00:00:00.000

Thanks to louis from sql server central.com, i got the solution

http://www.sqlservercentral.com/Forums/Topic1715611-3077-1.aspx

DECLARE @D nvarchar(6) = '083115'

SELECT CONVERT(DateTime, SUBSTRING(@D, 5, 2) + SUBSTRING(@D, 1, 4), 12)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM