Skip to content
Advertisement

Split strings in a column based on text values and numerical values such as patindex

I have a column that displays stock market options data like below:

I want to be able to split these up so they show up like:

I was able to split the first portion with the ticker name by using the code below, but I don’t know how to split the rest of the strings.

edit: for anyone who is wondering, I used Dale’s solution below to get my desired outcome. I edited the query he provided to make the parts show up as individual columns

Advertisement

Answer

Just keep doing what you started doing by using SUBSTRING. So as you did find the first number and actually in your case, based on the data provided, everything else is fixed length, so you don’t have to search anymore, just split the string.

Returns:

Data
GME|240119|C|00020000
QQQ|240119|C|00305000
NFLX|240119|P|00455000

If one can assume that all but the first column are fixed width then a simple SUBSTRING solution would suffice e.g.

Note: CROSS APPLY is just a fancy way to use a sub-query to avoid needing to repeat a calculation.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement