Question

I have a field with the following "formatted" data: "XYZ100320C00027500". In this:

  • XYZ - Symbol (up to 6 characters)
  • 10 - Year (2 characters)
  • 03 - Month (2 characters)
  • 20 - Day (2 characters)
  • C - Call/Put (C or P)
  • 00027 - Strike Dollar (5 characters)
  • 500 - Strike Decimal (3 characters)

From this field I need to extract the symbol, date (month/day/year), call/put, strike (strike dollar+strike decimal). I need to extract these and put them in their own columns in one table.

Any help or direction with how to do this with a sql query would be much appreciated.

Was it helpful?

Solution

This should work, although it might not be too efficient...

SQL Fiddle

MS SQL Server 2012 Schema Setup:

CREATE TABLE Table1
    (the_string varchar(21))
;

INSERT INTO Table1
    (the_string)
VALUES
    ('XYZ100320C00027500'),
    ('ABCDEF120410C00022540')
;

Query 1:

SET DATEFORMAT ymd
SELECT 

    SUBSTRING(the_string, 0, PATINDEX('%[0-9]%', the_string)) AS [Symbol], 
    CAST( SUBSTRING(the_string, PATINDEX('%[0-9]%', the_string), 2) + '-' + SUBSTRING(the_string, PATINDEX('%[0-9]%', the_string)+2, 2) + '-' + SUBSTRING(the_string, PATINDEX('%[0-9]%', the_string)+4, 2) AS DATE) AS [Date],
    SUBSTRING(the_string, PATINDEX('%[0-9]%', the_string)+6, 1) AS [Call/Put], 
    CAST(SUBSTRING(the_string, PATINDEX('%[0-9]%', the_string)+7, 5) AS FLOAT) + CAST(SUBSTRING(the_string, PATINDEX('%[0-9]%', the_string)+12, 3) AS FLOAT)/1000 AS [Strike]

FROM table1

Results:

| SYMBOL |       DATE | CALL/PUT | STRIKE |
|--------|------------|----------|--------|
|    XYZ | 2010-03-20 |        C |   27.5 |
| ABCDEF | 2012-04-10 |        C |  22.54 |
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top