Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Recursive Cte Help

$
0
0

Hi, 

I am trying to parse a string based on the ":" identifier . Basically from the single string I need to parse data appended as follows:

While Executing I get the error :

Msg 537, Level 16, State 3, Line 1
Invalid length parameter passed to the LEFT or SUBSTRING function.
Warning: Null value is eliminated by an aggregate or other SET operation.

Please guide .

WITH cte
AS (
	SELECT   
			 convert (varchar(max),substring('TRANSACTION:DEV:ff4520bb-038a-4ea6-b14a-1d308fb4214a:0:6140366', 1, charindex(':', substring('TRANSACTION:DEV:ff4520bb-038a-4ea6-b14a-1d308fb4214a:0:6140366', 2, len('TRANSACTION:DEV:ff4520bb-038a-4ea6-b14a-1d308fb4214a:0:6140366') - 1)) - 0))  AS word
			,convert (varchar(max),substring('TRANSACTION:DEV:ff4520bb-038a-4ea6-b14a-1d308fb4214a:0:6140366', charindex(':', substring('TRANSACTION:DEV:ff4520bb-038a-4ea6-b14a-1d308fb4214a:0:6140366', 2, len('TRANSACTION:DEV:ff4520bb-038a-4ea6-b14a-1d308fb4214a:0:6140366') - 1)) + 2, len('TRANSACTION:DEV:ff4520bb-038a-4ea6-b14a-1d308fb4214a:0:6140366'))) AS rest 
			,0 AS [Level]

	UNION ALL
	
    SELECT  

		     substring(Rest, 1, charindex(':', rest) - 1) AS word
			,substring(Rest, charindex(':', rest) + 1, len(rest)) AS Rest 
			,[Level] + 1
	FROM cte
	)
SELECT 
	max(CASE 
			WHEN [Level] = 0 
				THEN word
			END) AS Name
	,ISNULL(max(CASE 
			WHEN [Level] = 1
				THEN word 
			END),-98) AS Enironment
	,ISNULL(max(CASE 
			WHEN [Level] = 2
				THEN word
			END),-1) AS ID
	,ISNULL(max(CASE 
			WHEN [Level] = 3
				THEN word
			END),-1) AS Flag
	,ISNULL(max(CASE 
			WHEN [Level] = 4
				THEN word
			END),-99) AS sourceId
FROM cte


Thanks


EVA05


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>