I have a table with a string column with variable length. I need to get all the substrings of the cell value with a specified length
Example:
094740
Statement:
select ???(Column, 3) from table
Result to be:
094
947
474
740
Can this be achieved without a stored procedure
>Solution :
You can try to use CTE recursive with a simple math formula to make it.
CTE recursive will get a result set to represent how many row with offset by the statement
Query 1:
declare @offset int = 3
;WITH CTE AS (
SELECT 0 startIndex, len(col) totalLen,col
FROM T
UNION ALL
SELECT startIndex + 1,totalLen,col
FROM CTE
WHERE startIndex <= totalLen - @offset
)
SELECT substring(col,startIndex, @offset)
FROM CTE
WHERE startIndex > 0
| |
|-----|
| 094 |
| 947 |
| 474 |
| 740 |