sql - tsql function split string -
I wonder if someone can help me.
I need a tsql function to split a fixed value as:
1) 00 is not specified 3) 01-05 Global WM and BB | Operations 2) 02-05-01 Global WM & BB | Operations | Operation n / a
I need to get a result like this:
cat1 cat1descr cat2 cat2descr cat3 cat3descr ------- --- ----------------------------------------------- ------ 00 not mandatory redundant nonsense nonsense 01 Global WM & BB 05 Operations Blank blank 01 Global WM & BB 05 Operations 01 Operations N / A
The result is always 6 columns
Choose funcX ('00 is not specified ');
cat1 cat1descr cat2 cat2descr cat3 cat3descr ------------------ ---------------- ------------------------------ 00 Not Specified Null Null Null
This will work on SQL Server 2005 and SQL Server 2008. I have assumed that the sequence 1, 2, or 3 of the first digits of points has been set in groups. You can do it with less Cascading CTE but I think reading and debugging SUBSTRING / CHARINDEX / LEN syntax has become very difficult.
DECLARE @ FE TABLE (Bar VARCHAR (4000)); INSERT @foo (bar) SELECT '00 is not specified 'Select Unions All '01 -05 Global WM & BB | Operations 'Union All Select' 02-05-01 Global WM & BB Operations. Operation N / A '; With split1 as (SELECT n = SUBSTRING (bar, 1, CHARINDEX ('', bar) -1), w = SUBSTRING (bar, CHARINDEX ('', bar) +1, LAN (bar)), RN = ROW_NUMBER (From order to free order), Split 2 AS (Select RN, Cat 1 = Left (N, 2), WL = RTRIM (Sublrastring (W, 1, Coleys (Nalleif (Charundex ('' '', W ) 0) -1, LAN (W))), wr = LTRIM (SUBSTRING (w, NULLIF (CHARINDEX ('|', w), 0) + 1, LAN (W)), cat 2 = Nalif N, 4, 2), '', cat3 = NULLIF (SUBSTRING (n, 7, 2), '') 1) split3 as (SELECT rn, cat1descr = wl, cat2descr = RTRIM (SUBSTRING (wr, 1) C OALESCE (NULLIF (CHARINDEX ('|', wr), 0) -1, LAN), cat 3 dec = LTRIM (SUBSTRING (wr, NULLIF (CHARINDEX ('|', wr), 0) + 1 , 2) Selection of s2.cat1, s3.cat1descr, s2.cat2, s3.cat2descr, s2.cat3, s3.cat3descr from split2 as S2 inder split 3 as S3 ON S2.rn = s3.rn;
Comments
Post a Comment