Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

all substrings of a specified length

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Results:

|     |
|-----|
| 094 |
| 947 |
| 474 |
| 740 |
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading