简体   繁体   English

将CMMDDYY转换为SQL Server中的日期

[英]Convert CMMDDYY into date in SQL Server

If I have a date string in the format of CMMDDYY 如果我有一个CMMDDYY格式的日期字符串

C = 0, 1 or 2 where 0 represents 19, 1 represents 20 and 2 represents 21 C = 0,1或2,其中0代表19,1代表20,2代表21

Example: 例:

1022511 would be 02/25/2011 in mm/dd/yyyy format. 1022511将是02/25/2011,采用mm / dd / yyyy格式。

Is there a way to convert this into mm/dd/yyyy format in sql server? 有没有办法在sql server中将其转换为mm / dd / yyyy格式?

Is CMMDDYY even a valid date format? CMMDDYY甚至是有效的日期格式吗? I haven't found much information on it. 我没有找到太多关于它的信息。

It doesn't sound like a built in date format but you should be able to find it on msdn if it is. 它听起来不像内置日期格式,但如果是这样,你应该可以在msdn上找到它。 If not you could write a function in SQL server that parses a string in that format to a DateTime object 如果没有,您可以在SQL服务器中编写一个函数,将该格式的字符串解析为DateTime对象

The Year algorithm is simply: (19 + int(C))*100 + int(yy) 年算法很简单: (19 + int(C))*100 + int(yy)

Try this: 尝试这个:

declare @st nvarchar(7)
set @st = '1030609'

select 
    cast(
        substring(@st,2,2) + '/' 
        + substring(@st,4,2) + '/' 
        + cast((19 + cast(substring(@st,1,1) as int) )* 100 
                       + cast(substring(@st,6,2) as int) as nvarchar)
    as datetime)

This outputs: 这输出:

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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