tsql 函数拆分字符串

tsql function split string(tsql 函数拆分字符串)
本文介绍了tsql 函数拆分字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我想知道是否有人可以帮助我.

I wonder if anyone can help me.

我需要一个 tsql 函数来分割给定的值,例如:

I need a tsql function to split a given value such as:

1)    00 Not specified
3)    01-05 Global WM&BB | Operations
2)    02-05-01 Global WM&BB | Operations | Operations n/a

我需要得到这样的结果:

I need to get a result like this:

cat1  cat1descr       cat2    cat2descr     cat3   cat3descr
----------------------------------------------------------------
00    Not especified  null    null          null   null
01    Global WM&BB    05      Operations    null   null
01    Global WM&BB    05      Operations    01     Operations n/a

结果将始终有 6 列

select funcX('00 未指定');

select funcX('00 Not specified');

cat1  cat1descr      cat2  cat2descr      cat3  cat3descr
----------------------------------------------------------------
00    Not especified  null  null          null   null

推荐答案

这将适用于 SQL Server 2005 和 SQL Server 2008.我假设您的第一个数字序列固定为 1、2、或 3. 您可以使用较少的级联 CTE 来完成此操作,但我发现 SUBSTRING/CHARINDEX/LEN 语法很快就会变得非常难以阅读和调试.

This will work on SQL Server 2005 and SQL Server 2008. I have assumed that your first sequence of digits is fixed to 2-digit groups of 1, 2, or 3. You can do this with fewer cascading CTEs but I find the SUBSTRING/CHARINDEX/LEN syntax can quickly become very difficult to read and debug.

DECLARE @foo TABLE
(
    bar VARCHAR(4000)
);

INSERT @foo(bar) SELECT '00 Not specified'
UNION ALL SELECT '01-05 Global WM&BB | Operations'
UNION ALL SELECT '02-05-01 Global WM&BB | Operations | Operations n/a';


WITH split1 AS
(
    SELECT 
        n = SUBSTRING(bar, 1, CHARINDEX(' ', bar)-1),
        w = SUBSTRING(bar, CHARINDEX(' ', bar)+1, LEN(bar)),
        rn = ROW_NUMBER() OVER (ORDER BY bar)
    FROM
        @foo
),
split2 AS
(
    SELECT
        rn,
        cat1 = LEFT(n, 2),
        wl = RTRIM(SUBSTRING(w, 1, 
             COALESCE(NULLIF(CHARINDEX('|', w), 0)-1, LEN(w)))),
        wr = LTRIM(SUBSTRING(w, NULLIF(CHARINDEX('|', w),0) + 1, LEN(w))),
        cat2 = NULLIF(SUBSTRING(n, 4, 2), ''),
        cat3 = NULLIF(SUBSTRING(n, 7, 2), '')
    FROM
        split1
),
split3 AS
(
    SELECT
        rn,
        cat1descr = wl,
        cat2descr = RTRIM(SUBSTRING(wr, 1, 
              COALESCE(NULLIF(CHARINDEX('|', wr), 0)-1, LEN(wr)))),
        cat3descr = LTRIM(SUBSTRING(wr, 
              NULLIF(CHARINDEX('|', wr),0) + 1, LEN(wr)))
    FROM 
        split2
)
SELECT
    s2.cat1, s3.cat1descr,
    s2.cat2, s3.cat2descr,
    s2.cat3, s3.cat3descr
 FROM split2 AS s2
INNER JOIN split3 AS s3
ON s2.rn = s3.rn;

这篇关于tsql 函数拆分字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致
SQL query to group by day(按天分组的 SQL 查询)
What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)
MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)
MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)
Include missing months in Group By query(在 Group By 查询中包含缺失的月份)