如何获取前六个字符,然后将它们转换为MSSQL中的日期

时间:2015-03-04 17:57:28

标签: sql-server sql-server-2012

如何拆分以下三个varchar列。并抓取某些部分,然后将它们转换为不同的字段,其中一个转换为日期时间格式。在SQL 2012中

我希望结果如此。

-   docid, FileName, DealerNo, InvoiceNo, WeekEnding, Doctype, date

1,123InvoiceIn-044632-15021507129,044632,15021507129, 02/15/2015 ,123InvoiceIn,

如果您注意到#1。我能够分割字段,但现在我还需要转换invoiceno的前6个字符并将它们格式化为日期字段,该字段应为 02/15/2015 < / strong>并把它放在周末。

- docid, FileName, DealerNo, InvoiceNo, WeekEnding, Doctype, date

2,123InvoiceOut-15022238842-150222,47158,15022238842, 02/22/2015 ,123InvoiceOut,

如果您在#2上注意到格式有点不同。我能够分割字段,但现在我还需要转换invoiceno的前6个字符并将它们格式化为日期字段,该字段应为 02/22/2015 并将其放在周末。< / p>

- docid, FileName, DealerNo, InvoiceNo, WeekEnding, Doctype, date

3,123WrapIn-205251-806-10_04_2013,205251,806,123WrapIn, 2013-10-04 00:00.000

如果您注意到#3,我唯一需要的是从文件名列转换下划线附带的日期,我需要将其作为日期时间列在日期列上

CREATE TABLE TEST 
(
docId int,
FileName NVARCHAR(64),
DealerNo NVARCHAR(64),
InvoiceNo NVARCHAR(64),
WeekEnding NVARCHAR(64),
DocType NVARCHAR(64),
Date DATETIME
);

Insert into TEST (docId, FileName, DealerNo, InvoiceNo, WeekEnding, Doctype, date)
VALUES 
(1, '123InvoiceIn-044632-15021507129', '', '', '', '', ''),
(2, '123InvoiceOut-15022238842-150222', '', '', '', '', ''),
(3, '123WrapIn-205251-806-10_04_2010', '', '', '', '', '');
GO

2 个答案:

答案 0 :(得分:0)

试试这个:

SELECT  *,
        CASE 
            WHEN t.[FileName] LIKE N'%[-]' + REPLICATE('[0-9]', 11) 
                THEN CONVERT(DATE, '20' + LEFT(RIGHT(t.[FileName], 11), 6)) -- For <SQL2008 use [SMALL]DATETIME instead of DATE
            WHEN t.[FileName] LIKE N'%[-]' + REPLICATE('[0-9]', 6)  
                THEN CONVERT(DATE, '20' + RIGHT(t.[FileName], 6))           
            WHEN t.[FileName] LIKE N'%[-][0-9][0-9][_][0-9][0-9][_][0-9][0-9][0-9][0-9]' 
                THEN CONVERT(DATE, REPLACE(RIGHT(t.[FileName], 10), '_', '/'), 101)
            --ELSE NULL -- Unknow format
        END AS Date
FROM    dbo.TEST t

注意:我假设所有日期都是&gt; = 2000-01-01

答案 1 :(得分:0)

我能够使用自定义函数

在SQL中拆分列
USE [Test]
GO
/****** Object:  UserDefinedFunction [dbo].[SplitStrings_CTE]    Script Date: 05/02/2013 11:40:24 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[SplitStrings_CTE](@List nvarchar(max), @Delimiter nvarchar(1))
RETURNS @returns TABLE(val nvarchar(max), [level] int, PRIMARY KEY CLUSTERED([level]))
AS
BEGIN
;WITH cte AS
 (
  SELECT SUBSTRING(@List, 0, CHARINDEX(@Delimiter, @List)) AS val,
         CAST(STUFF (@List + @Delimiter, 1, CHARINDEX(@Delimiter, @List), '') AS nvarchar(max)) AS stval,
         1 AS [level]
  UNION ALL
  SELECT SUBSTRING(stval, 0, CHARINDEX(@Delimiter, stval)),
         CAST(STUFF (stval, 1, CHARINDEX(@Delimiter, stval), '') AS nvarchar(max)),
         [level] + 1
  FROM cte
  WHERE stval != ''
  )
  INSERT @returns
  SELECT REPLACE(val, ' ', '') AS val, [level]
  FROM cte
  RETURN
END

然后使用调用该函数的SQL触发器,使用SUBSTRING

拆分并格式化日期
;WITH cte AS
  (  
  SELECT t.docid, t.FILENAME, t.DEALERNO, t.INVOICENO, t.WEEK_ENDING, t.DOCTYPE, 
         NewDOCTYPE = MAX(CASE WHEN o.level = 1 THEN o.val END) OVER(),
         NewDEALERNO = MAX(CASE WHEN o.level = 2 THEN o.val END) OVER(),
         NewWEEK_ENDING = MAX(CASE WHEN o.level = 3 THEN o.val END) OVER(),
         NewINVOICENO = MAX(CASE WHEN o.level = 4 THEN o.val END) OVER()
  FROM dbo.test t CROSS APPLY dbo.SplitStrings_CTE(t.FILENAME, '-') o where t.DOCID=@varDOCID
  )

Update cte
SET DOCTYPE=NewDOCTYPE,
DEALERNO=NewDEALERNO,
WEEK_ENDING=(SELECT
(
    substring(LEFT([NewWEEK_ENDING],6) ,3,2) + '/' + 
       substring(LEFT([NewWEEK_ENDING],6) ,5,2) + '/' + 
       substring(LEFT([NewWEEK_ENDING],6) ,1,2) 

 ) )