Skip to content
Advertisement

Parse String and Insert Results Into 3 Fields

I have a field in a SQL table that looks like this.

dbfs:/mnt/rawvtogetdata/2019/06/30/placemt/CZZZ0630.M.00308286.txt

It’s literally a file name. I’d need to create 3 new fields and split the string shown above into this.

Mth     Dy    Fl
06      30    CZZZ0630.M.00308286.txt

How can I do that?

I have this basic SQL that does the select and parsing, but I’m not sure how to do the insert into.

Select *,
   SUBSTRING(filename, 30, 2) AS Mth,
   SUBSTRING(filename, 33, 2) AS Dy,
   SUBSTRING(filename, 44, 99) AS Fl
from REUTERS_CPDG_2019

I am in SQL Server 2019.

Advertisement

Answer

Assuming these are the names of the new columns, I would just go for:

UPDATE
    REUTERS_CPDG_2019
SET
    Mth = SUBSTRING(filename, 30, 2),
    Dy = SUBSTRING(filename, 33, 2),
    Fl = SUBSTRING(filename, 44, 99);
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement