在SQL Server中的^之后取正确的字符串

时间:2019-02-19 17:41:08

标签: sql-server tsql

我在SQL Server中有一些数据:

test1^test2^test3^test4  
test5  
test6^test7  
null  

所需的输出:

test4
test5
test7
null

6 个答案:

答案 0 :(得分:2)

您可以将right()charindex()reverse()结合使用:

select *, right(col, charindex('^', reverse(col) + '^')-1)
from ( values ('test1^test2^test3^test4'), ('test5'), ('test6^test7'), (null)  
     ) t(col);

Demo

答案 1 :(得分:1)

一种方法是使用DelimitedSplit8k_LEAD分割值,然后取“最后一个”值(从高到低排序)。此方法使用TOP 1 WITH TIES 我使用此方法是因为此处的顺序似乎无关紧要

WITH VTE AS (
    SELECT Col
    FROM (VALUES('test1^test2^test3^test4'),
                ('test5'),
                ('test6^test7'),
                ('test9^test10'),
                (null)) V(col))
SELECT TOP 1 WITH TIES
       V.Col,
       DS.item
FROM VTE V
     CROSS APPLY dbo.delimitedsplit8k_LEAD(V.Col,'^') DS
ORDER BY ROW_NUMBER() OVER (PARTITION BY V.Col ORDER BY DS.ItemNumber DESC);

这避免了昂贵的REVERSE

答案 2 :(得分:0)

使用charindex并反向:

我把所有功能都留在那里了,这样您就可以看到过程了。

declare @t table (test varchar(100))

insert into @t
values
('test1^test2^test3^test4')
,('test5')
,('test6^test7')  
,(null  )

select test
    ,revTest
    ,charInd
    ,reverse(left(revTest,case when charind = 0 then len(revTest) else charInd-1 end))
from @t t
cross apply (select reverse(rtrim(ltrim(test)))) a(revTest)
cross apply (select CHARINDEX('^',revTest)) b(charInd)

结果:

**test                  revTest                 charInd (Result)**
test1^test2^test3^test4 4tset^3tset^2tset^1tset 6       test4
test5                   5tset                   0       test5
test6^test7             7tset^6tset             6       test7
NULL                    NULL                    NULL    NULL

答案 3 :(得分:0)

尝试

declare 
@col varchar(500), @reveCol varchar(500);
set @col='test133^test2^test3^test4';
set @reveCol=reverse(@col)


Select charindex('^', @revecol )
select right( @col ,charindex('^', @revecol )-1)

答案 4 :(得分:0)

假设您使用的是 SQL Server 2016 +

还要假设此架构和数据:

library(data.table)

mydata <- fread("
#    ID  wave score sex age edu 
#1  1001 1   28     1 69  12
#2  1001 2   27     1 70  12
#3  1001 3   28     1 71  12
#4  1001 4   26     1 72  12
#5  1002 1   30     2 78   9
#6  1002 3   30     2 80   9
#7  1003 1   33     2 65  16
#8  1003 2   32     2 66  16
#9  1003 3   31     2 67  16
#10 1003 4   24     2 68  16
#11 1004 1   22     2 85   4
#12 1005 1   20     2 60   9
#13 1005 2   18     1 61   9
#14 1006 1   22     1 74   9
#15 1006 2   23     1 75   9
#16 1006 3   25     1 76   9
#17 1006 4   19     1 77   9
#18 1007 1   33     2 65  16
#19 1007 2   32     2 66  16
#20 1007 3   31     2 67  16
#21 1007 4   31     2 68  16
", drop = 1L)

查询:

DROP TABLE IF EXISTS #Data;
CREATE TABLE #Data(StringValue NVARCHAR(MAX));
INSERT INTO #Data(StringValue)VALUES
    ('test1^test2^test3^test4')
    ,('test5')
    ,('test6^test7')
    ,(NULL)
;

答案 5 :(得分:0)

如果您使用的是 SQL Server 2017 +

SELECT MAX(CASE WHEN d.StringValue LIKE '%'+s.value THEN s.[value] ELSE NULL END) AS [Result]
FROM ( VALUES
     ('test1^test2^test3^test4')
    ,('test5')
    ,('test6^test7')
    ,(NULL)
) d(StringValue)
CROSS APPLY STRING_SPLIT(COALESCE(d.StringValue,''),'^') s
GROUP BY d.StringValue
;