0

我有一个这样的字符串:

","","","41","20120627063343-0210600-41"

我这样写了我的查询来拆分上面的字符串

declare @s varchar(max)
set @s = '","","","41","20120627063343-0210600-41"'
select right(replace(@s,',',''),26) as output

我得到以下输出

output
-------
20120627063343-0210600-41"

我想要上面的字符串这样的输出

YEAR         TIME         NO        ID
----         -----       ----      ---- 
2012-06-27    06:33:43    0210600   41

帮我!

谢谢和问候,阿南德

4

3 回答 3

1

我怀疑这将与其他人建议的子字符串方法一样,但它可能更具可读性,无论它是另一种选择。

DECLARE @parsedString VARCHAR(255)
DECLARE @inputString VARCHAR(255)

SET @inputString = '","","","41","20120627063343-0210600-41"' 
SET @parsedString = REPLACE(REPLACE(REPLACE(REPLACE(@inputString, '-', '.'), '",', '.'), '"', ''), '...', '')

SELECT  PARSENAME(@parsedString, 1) as [Id],
        PARSENAME(@parsedString, 2) as [No],
        CAST(LEFT(PARSENAME(@parsedString, 3), 8) AS DATE) as [Year],
        STUFF(STUFF(RIGHT(PARSENAME(@parsedString, 3), 6), 3, 0, ':'), 6, 0, ':') as [Time] 
于 2012-06-29T10:26:24.513 回答
0

一种可能的解决方案:

declare @s varchar(max), @test varchar(26)
set @s = '","","","41","20120627063343-0210600-41"'
select @test = left(right(replace(@s,',',''),26),25)


select SUBSTRING(@test, 1, 4) + '-' + SUBSTRING(@test, 5, 2) + '-' + SUBSTRING(@test, 7, 2) as 'YEAR',
       SUBSTRING(@test, 9, 2) + ':' + SUBSTRING(@test, 11, 2) + ':' + SUBSTRING(@test, 13, 2) as 'TIME',
       SUBSTRING(@test, 16, 7) as 'NO',
       SUBSTRING(@test, 24, 2) as 'ID'
于 2012-06-29T10:07:10.667 回答
0

使用SUBSTRING函数,并假设字符串示例20120627063343-0210600-41总是相同的固定长度,您可以提取所需的所有部分。尝试这个:

declare @s varchar(max)
declare @t varchar(max)
set @s = '","","","41","20120627063343-0210600-41"'
set @t = right(@s, 26) 

select left(@t, 4) + '-' + substring(@t, 5, 2) + '-' + substring(@t, 7, 2) AS [YEAR],
    substring(@t, 9, 2) + ':' + substring(@t, 11, 2) + ':' + substring(@t, 13, 2) AS [HOUR],
    substring(@t, 16, 7) AS [NO], 
    substring(@t, 24, 2) AS [ID]
于 2012-06-29T10:02:59.287 回答