TSQL:如何交换字符串中的字符?

时间:2016-03-31 13:34:53

标签: sql-server tsql sql-server-2014

我已将日期作为字符串存储在测试数据库中,文本存储时与mm和dd交换。我有很多记录,如果可能的话我想自动化。 SQL中是否有一种方法可以根据位置交换字符?

enter image description here

以下是代码:

CREATE TABLE #D (
DateAsString varchar (10)
)

INSERT INTO #D (DateAsString)

VALUES 
('20160601'),
('20160127'),
('20160129')

SELECT 
DataAsString

FROM #D

谢谢。

5 个答案:

答案 0 :(得分:3)

试试这个 -

Update #D 
    SET SwapDateAsString = FORMAT(CAST(Dateasstring as DATE), 'yyyyddMM')

<强>结果

DateAsString    SwapDateAsString
20160601        20160106
20160127        20162701
20160129        20162901

答案 1 :(得分:3)

试试这个,

DECLARE @MyTable TABLE (DateAsString VARCHAR(10))

INSERT INTO @MyTable (DateAsString)
VALUES ('20160601')
    ,('20162701')
    ,('20162901')

SELECT DateAsString
    ,substring(DateAsString, 1, 4) + substring(DateAsString, 7, 2) + + substring(DateAsString, 5, 2) AS DesiredResultAsString
    ,Convert(DATE, substring(DateAsString, 1, 4) + substring(DateAsString, 7, 2) + + substring(DateAsString, 5, 2)) AS DesiredResultAsDate
FROM @MyTable

答案 2 :(得分:1)

由于SQL Server does not allow arbitrarily字符串日期/时间解析(如.NET中的TryParse或Parse),您必须从片段构建日期:

declare @dateStr VARCHAR(8) = '20162701'
select DATEFROMPARTS(substring(@dateStr, 1, 4), substring(@dateStr, 7, 2), substring(@dateStr, 5, 2)) 

答案 3 :(得分:1)

我在这个LINK找到了一个名为fnFormatDate的函数,因为你没有说明你使用的SQL Server版本,这个解决方案应该适用于2005年及更新版本的所有SQL版本。顺便说一下,我将添加到我的ETL函数库中。这是我的解决方案:

IF OBJECT_ID(N'dbo.fnFormatDate', 'FN') IS NOT NULL
    DROP FUNCTION dbo.fnFormatDate
GO

CREATE FUNCTION dbo.fnFormatDate (@Datetime DATETIME, @FormatMask VARCHAR(32))
    RETURNS VARCHAR(32)
AS
    BEGIN
        DECLARE @StringDate VARCHAR(32)
        SET @StringDate = @FormatMask
        IF (CHARINDEX ('YYYY',@StringDate) > 0)
            SET @StringDate = REPLACE(@StringDate, 'YYYY',
                                DATENAME(YY, @Datetime))
        IF (CHARINDEX ('YY',@StringDate) > 0)
            SET @StringDate = REPLACE(@StringDate, 'YY',
                                RIGHT(DATENAME(YY, @Datetime),2))
        IF (CHARINDEX ('Month',@StringDate) > 0)
            SET @StringDate = REPLACE(@StringDate, 'Month',
                                DATENAME(MM, @Datetime))
        IF (CHARINDEX ('MON',@StringDate COLLATE SQL_Latin1_General_CP1_CS_AS)>0)
            SET @StringDate = REPLACE(@StringDate, 'MON',
                                LEFT(UPPER(DATENAME(MM, @Datetime)),3))
        IF (CHARINDEX ('Mon',@StringDate) > 0)
            SET @StringDate = REPLACE(@StringDate, 'Mon',
                                            LEFT(DATENAME(MM, @Datetime),3))
        IF (CHARINDEX ('MM',@StringDate) > 0)
            SET @StringDate = REPLACE(@StringDate, 'MM',
                        RIGHT('0'+CONVERT(VARCHAR,DATEPART(MM, @Datetime)),2))
        IF (CHARINDEX ('M',@StringDate) > 0)
            SET @StringDate = REPLACE(@StringDate, 'M',
                                CONVERT(VARCHAR,DATEPART(MM, @Datetime)))
        IF (CHARINDEX ('DD',@StringDate) > 0)
            SET @StringDate = REPLACE(@StringDate, 'DD',
                                RIGHT('0'+DATENAME(DD, @Datetime),2))
        IF (CHARINDEX ('D',@StringDate) > 0)
            SET @StringDate = REPLACE(@StringDate, 'D',
                                            DATENAME(DD, @Datetime))   
        RETURN @StringDate
    END
GO

IF OBJECT_ID(N'Tempdb..#D', 'U') IS NOT NULL
    DROP TABLE #D

CREATE TABLE #D(DateAsString varchar (10))
GO

INSERT INTO #D (DateAsString)
    VALUES 
            ('20160601')
            ,('20160127')
            ,('20160129')
            ,('20160229')
            ,('20161231')
    GO

SELECT 
    DateAsString
    ,NewDateAsString = dbo.fnFormatDate(dbo.fnFormatDate(DateAsString, 'YYYYMMDD'), 'YYYYDDMM') 
 FROM #D

enter image description here

答案 4 :(得分:0)

例如

DECLARE @d VARCHAR(8) = '20160601'
SELECT  @d as BasdDate, SUBSTRING(@d, 1, 4) + SUBSTRING(@d, 7, 2) + SUBSTRING(@d, 5, 2) as GoodDate