简体   繁体   English

SQL Server 2012将日期时间数据从varchar转换为Date部分

[英]SQL Server 2012 convert datetime data from varchar to Date portion

Have researched a lot, both on this site, and others, but still don't have a valid solution. 在此站点以及其他站点上都进行了很多研究,但是仍然没有有效的解决方案。 I have a column of varchar datatype, it contains DateTime data. 我有一列varchar数据类型,它包含DateTime数据。 I need to store only the Date portion in a Date type column. 我只需要在Date类型列中存储Date部分。 Tried different ways of Cast, Convert, and other functions, but still haven't been able to make it work. 尝试了Cast,Convert和其他功能的不同方式,但仍无法使其正常工作。

Basically I want to convert this 基本上我想转换这个

Tue Apr 26 2016 13:54:53 GMT+0200 (CEST)

to

04/26/2016

Assuming your Day and Month part is always 3 characters long this can be done simply as this: 假设您的“日期和月份”部分始终为3个字符长,则可以这样简单地完成操作:

DECLARE @d VARCHAR(100)='Tue Apr 26 2016 13:54:53 GMT+0200 (CEST)';
SELECT CONVERT(DATE,SUBSTRING(@d,4,12),109);

If this was to easy one would have to find the blank(s) with CHARINDEX , but I don't think so... 如果这很容易的话,则必须使用CHARINDEX查找空白,但我不这么认为...

The format code 109 means mon dd yyyy hh:mi:ss:mmmAM (or PM) Details here . 格式代码109表示此处的 mon dd yyyy hh:mi:ss:mmmAM (or PM) 详细信息

And be aware that formats containing language depending parts are directly sent by the devil to create never ending pain... This will not work on a server with a different language setting! 并且请注意,包含语言相关部分的格式是由魔鬼直接发送的,以产生永无止境的痛苦……这在具有不同语言设置的服务器上将不起作用!

Declare @String varchar(max) = 'Tue Apr 26 2016 13:54:53 GMT+0200 (CEST)'
Select cast(Substring(@String,12,4)+Substring(@String,4,7) as date)

Returns 退货

2016-04-26

Okay a few things here you have a field that looks to be psuedo ISO 8601 but is not the standard. 好的,这里您有一个看起来像是伪ISO 8601但不是标准的字段。 The first question will be: "Where does this come from?" 第一个问题是:“这是从哪里来的?” Typically you don't have the 'Tue' or 'GMT' or '(CEST)' in a standard and the offset from Greenwich Meantime is in the format (+/-)##:## NOT (+/-)####. 通常,您在标准中没有'Tue'或'GMT'或'(CEST)',并且与格林威治标准时间的偏移量的格式为(+/-)##:## NOT(+/-)# ###。 SQL and many other formats can easily accept a standardized string in the ISO 8601 format. SQL和许多其他格式可以轻松接受ISO 8601格式的标准化字符串。 Good brief here: https://www.w3.org/TR/NOTE-datetime 此处的简短摘要: https : //www.w3.org/TR/NOTE-datetime

That being said you can easily get what you want with a little know how: 话虽这么说,您只要一点点知道就可以轻松获得想要的东西:

DECLARE 
    @S VARCHAR(128) = 'Tue Apr 26 2016 13:54:53 GMT+0200 (CEST)'
,   @Valid VARCHAR(128)

--Legitimate ISO 8601 string:
SELECT @Valid = RTRIM(LTRIM(REPLACE(STUFF(STUFF(@S, 1, 4, ''), LEN(@S)-12, 12, ':00'), 'GMT', '')))
SELECT @Valid

--Legitimate DateTimeOffset
SELECT CAST(@Valid AS DATETIMEOFFSET)

--Now that I have a legimiate DateTimeOffset I can downconvert easily
SELECT CAST(CAST(@Valid AS DATETIMEOFFSET) AS DATE)

--AND... Now that I have a legimate Date I can format it many different ways
SELECT CONVERT(VARCHAR, CAST(CAST(@Valid AS DATETIMEOFFSET) AS DATE), 101)

The real thing to realize here is there is magical conversion of DateTime using the convert function. 这里真正要实现的是使用convert函数对DateTime进行神奇的转换。 But you may be wondering 'what if I want it to look different?'. 但是您可能想知道“如果我希望它看起来与众不同?”。 Try this page: 试试这个页面:

http://www.sql-server-helper.com/tips/date-formats.aspx http://www.sql-server-helper.com/tips/date-formats.aspx

I would be leery though of just finding the placement of were things appear to be coming from a string even though I can parse your example. 尽管我可以解析您的示例,但是只要发现事物似乎是来自字符串的位置,那我就不会大惊小怪。 If you are getting things not following a standard you should know why. 如果您发现事情没有遵循标准,您应该知道原因。 The main reason being you may be able to get this to work for a specific instance but not be able to repeat this pattern over and over. 主要原因是您可能可以使它适用于特定实例,但不能一遍又一遍地重复此模式。

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

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