如何使用SQL Server 2016将以下文本字符串转换为日期类型

时间:2018-11-11 04:17:02

标签: sql string date sql-server-2016

ActivateDate        ShipDate                Month   Month_Length    Day-2c  Day-2c_Length   YEAR-201x   SHIPDateConcatenate     ActivateDateConcatenate
NULL                6/12/2018 12:00:00 AM   6       1               12      2               2018        6-12-2018               NULL
NULL                6/12/2018 12:00:00 AM   6       1               12      2               2018        6-12-2018               NULL
NULL                6/12/2018 12:00:00 AM   6       1               12      2               2018        6-12-2018               NULL
NULL                6/12/2018 12:00:00 AM   6       1               12      2               2018        6-12-2018               NULL
NULL                6/12/2018 12:00:00 AM   6       1               12      2               2018        6-12-2018               NULL
NULL                6/12/2018 12:00:00 AM   6       1               12      2               2018        6-12-2018               NULL
NULL                6/12/2018 12:00:00 AM   6       1               12      2               2018        6-12-2018               NULL
10/12/2018 14:45    10/16/2018 12:00:00 AM  10      2               16      2               2018        10-16-2018              10-12-2018

下面列出了两列[ActivateDate]和[ShipDate]数据类型;但是,每次我尝试将convert()或cast()用作日期类型时,都会发生转换错误。

SELECT
    [ActivateDate], -- '10/12/2018 14:45' nvarchar(100)
    [ShipDate], -- '6/12/2018 12:00:00 AM'   nvarchar(100)
    SUBSTRING(iedimpr.[ShipDate],CHARINDEX('/', iedimpr.[ShipDate])-2,2) as 'Month',
    LEN(SUBSTRING(iedimpr.[ShipDate],CHARINDEX('/', iedimpr.[ShipDate])-2,2)) as 'Month_Length',
    REPLACE(SUBSTRING(iedimpr.[ShipDate],CHARINDEX('/', iedimpr.[ShipDate])+1,2),'/','') as 'Day-2c',
    LEN(REPLACE(SUBSTRING(iedimpr.[ShipDate],CHARINDEX('/', iedimpr.[ShipDate])+1,2),'/','')) as 'Day-2c_Length',
    SUBSTRING(iedimpr.[ShipDate],CHARINDEX('/201', iedimpr.[ShipDate])+1,4) as 'YEAR-201x',
    SUBSTRING(iedimpr.[ShipDate],CHARINDEX('/', iedimpr.[ShipDate])-2,2)
+'-'+REPLACE(SUBSTRING(iedimpr.[ShipDate],CHARINDEX('/', iedimpr.[ShipDate])+1,2),'/','') 
+'-'+SUBSTRING(iedimpr.[ShipDate],CHARINDEX('/201', iedimpr.[ShipDate])+1,4) as 'SHIPDateConcatenate',
    SUBSTRING(iedimpr.[ActivateDate],CHARINDEX('/', iedimpr.[ActivateDate])-2,2)
+'-'+REPLACE(SUBSTRING(iedimpr.[ActivateDate],CHARINDEX('/', iedimpr.[ActivateDate])+1,2),'/','') 
+'-'+SUBSTRING(iedimpr.[ActivateDate],CHARINDEX('/201', iedimpr.[ActivateDate])+1,4) as 'ActivateDateConcatenate'

1 个答案:

答案 0 :(得分:0)

再次阅读问题后,我发现样本数据格式错误,因此我对其进行了编辑。在此编辑过程中,我发现您使用美国风格(mm/dd/yyyy)来表示日期的字符串。

要将具有这种格式的有效的DateTime字符串表示形式转换为日期,您需要在101方法中使用convert作为style参数。
但是,我强烈建议使用Try_convert而不是Convert,因为当无法转换值而不产生错误时,它将仅返回null

话虽如此,这是一个例子:

首先,创建并填充示例数据(在您以后的问题中为我们保存此步骤):

DECLARE @T AS TABLE
(
    ActivateDate nvarchar(100),
    ShipDate nvarchar(100)
)

INSERT INTO @T(ActivateDate, ShipDate) VALUES
(NULL,                  '6/12/2018 12:00:00 AM'),
('10/12/2018 14:45',    '10/16/2018 12:00:00 AM'),
('20/14/2018 14:45',    '10/16/2018 12:00:00 AM'), -- invalid ActivateDate
('2/4/2018 14:45',      '10/16/ZOIB 12:00:00 AM') -- invalid ShipDate

查询:

SELECT  ActivateDate, 
        ShipDate,
        TRY_CONVERT(datetime, ActivateDate, 101) As DateActiveDate,
        TRY_CONVERT(datetime, ShipDate, 101) As DateShipDate
FROM @T

结果:

ActivateDate        ShipDate                    DateActiveDate          DateShipDate
NULL                6/12/2018 12:00:00 AM       NULL                    12.06.2018 00:00:00
10/12/2018 14:45    10/16/2018 12:00:00 AM      12.10.2018 14:45:00     16.10.2018 00:00:00
20/14/2018 14:45    10/16/2018 12:00:00 AM      NULL                    16.10.2018 00:00:00
2/4/2018 14:45      10/16/ZOIB 12:00:00 AM      04.02.2018 14:45:00     NULL

如果您只需要Date(没有时间部分),则只需使用try_convert(date, ...)而不是try_convert(datetime, ...)