-1

I have a column called OriginalFileName in table [Import]. The data looks like below. The format is ID_LastName_FirstName_etc. I am trying to write a statement that will pull the ID, LastName, and FirstName into their own columns.

2900411_DOE_JOHN_1013273960_SMITH_012018-122018_TEST_1023194929605635701.pdf 

I was able to get the ID and last name using this:

  1. ID:

SELECT LEFT(OriginalFileName, charindex('_', OriginalFileName) -1) from IMPORT

  1. LASTNAME (underscore value keeps disappearing when I copy and paste so I have replaced the underscore symbol with the actual word):

SELECT SubString(OriginalFileName, (CHARINDEX('underscore', OriginalFileName, 0) + 1),(CharIndex('underscore', RIGHT(OriginalFileName, (LEN(OriginalFileName) - (CharIndex('underscore', OriginalFileName, 0)))), 0) - 1)) from IMPORT

  1. FIRSTNAME:

?????????????????????????????

However, I am having a lot of trouble getting the first name out. Please assist. The length of the data separated by underscores will vary. I got those two other statements from other posts but was unable to figure out how to modify in order to get the first name value.

3 Answers3

1

This is one method, assuming that surname and forename are the 2nd and 3rd parts respectively, and there are more parts afterwards:

SELECT V.Filename,
       SUBSTRING(V.Filename, SN.CI+1, FNs.CI - SN.CI -1 ) AS Surname,
       SUBSTRING(V.Filename, FNs.CI+1, FNe.CI - FNs.CI -1 ) AS Forename
FROM (VALUES('2900411_DOE_JOHN_1013273960_SMITH_012018-122018_TEST_1023194929605635701.pdf')) V(Filename)
     CROSS APPLY (VALUES(CHARINDEX('_',V.Filename)))SN(CI)
     CROSS APPLY (VALUES(CHARINDEX('_',V.Filename,SN.CI+1)))FNs(CI)
     CROSS APPLY (VALUES(CHARINDEX('_',V.Filename,FNs.CI+1)))FNe(CI);
Thom A
  • 88,727
  • 11
  • 45
  • 75
  • If I want to do this for all data in column OriginalFileName, how would this be modified? – David Smith Feb 26 '19 at 13:41
  • Just use your column's name instead, @DavidSmith . – Thom A Feb 26 '19 at 13:52
  • Thanks. I tried that but received an error "OriginalFileName is not a recognized table hints option." Is this an issue with my SQL server version? I do not have the ability to edit the compatibility level. The error message indicated it should be set to 90. – David Smith Feb 26 '19 at 17:23
  • Huh? Why are you specifying a table hint, I don't use them in my solution at all. I'm guessing you don't understand the above solution, what about it are you struggling with? Thev`CHARINDEX` or the `SUBSTRING`? What have you tried using my solution? Could you post a DB<>Fiddle? – Thom A Feb 26 '19 at 17:52
  • Yes sorry quite a SQL beginner. SELECT V.OriginalFilename, SUBSTRING(V.OriginalFilename, SN.CI+1, FNs.CI - SN.CI -1 ) AS Surname, SUBSTRING(V.OriginalFilename, FNs.CI+1, FNe.CI - FNs.CI -1 ) AS Forename FROM MedicalRecordRetrievalImportDetail V(OriginalFilename) CROSS APPLY (VALUES(CHARINDEX('_',V.OriginalFilename)))SN(CI) CROSS APPLY (VALUES(CHARINDEX('_',V.OriginalFilename,SN.CI+1)))FNs(CI) CROSS APPLY (VALUES(CHARINDEX('_',V.OriginalFilename,FNs.CI+1)))FNe(CI) – David Smith Feb 26 '19 at 17:57
  • What is `V(OriginalFilename)`? Tables don't require aliases for columns (and I doubt your table only have one column) – Thom A Feb 26 '19 at 17:59
  • `(VALUES(...) V(Filename)` is the sample data in my query (as we had but a single string to work with here), you need to replace that with your table, and the column name in the rest of the query – Thom A Feb 26 '19 at 18:03
1

Just another option.

Not clear if the positions are fixed to Pos2 and Pos3

Example

Declare @YourTable table (ID int,OrginalFileName varchar(500))
Insert Into @YourTable values
(1,'2900411_DOE_JOHN_1013273960_SMITH_012018-122018_TEST_1023194929605635701.pdf')

Select A.ID
      ,B.*
 From  @YourTable A
 Cross Apply (
                Select Pos2 = xDim.value('/x[2]','varchar(max)')
                      ,Pos3 = xDim.value('/x[3]','varchar(max)')
                From  (Select Cast('<x>' + replace(OrginalFileName,'_','</x><x>')+'</x>' as xml) as xDim) as A 
             ) B

Returns

enter image description here

John Cappelletti
  • 79,615
  • 7
  • 44
  • 66
0

Another option to consider, doing this with a series of update statements to parse column by column. Less elegant than @Larnu's, but maybe more flexible.

DECLARE @str VARCHAR(1000) = '2900411_DOE_JOHN_1013273960_SMITH_012018-122018_TEST_1023194929605635701.pdf'
DROP TABLE #results
CREATE TABLE #results (origstring VARCHAR(MAX), col1 INT, lnm VARCHAR(10), fnm VARCHAR(10), col4 VARCHAR(20), col5 VARCHAR(20), col6 VARCHAR(20), col7 VARCHAR(20), col8 VARCHAR(20))

INSERT INTO #results (origstring)
SELECT @str

UPDATE #results
SET col1 = LEFT(origstring, CHARINDEX('_', origstring) - 1)
, origstring = right(origstring, len(origstring) - CHARINDEX('_', origstring))

UPDATE #results
SET lnm = LEFT(origstring, CHARINDEX('_', origstring) - 1)
, origstring = right(origstring, len(origstring) - CHARINDEX('_', origstring))

UPDATE #results
SET fnm = LEFT(origstring, CHARINDEX('_', origstring) - 1)
, origstring = right(origstring, len(origstring) - CHARINDEX('_', origstring))

UPDATE #results
SET col4 = LEFT(origstring, CHARINDEX('_', origstring) - 1)
, origstring = right(origstring, len(origstring) - CHARINDEX('_', origstring))

--etc, for the rest of  the columns

Each step basically cuts the first part off of the remaining original string and inserts it into a column.

APH
  • 4,109
  • 1
  • 25
  • 36
  • How can this be modified so I can get the substring out of an entire column of data called OriginalFileName? – David Smith Feb 26 '19 at 13:42
  • It shouldn't need to be modified at all - the `update` statements will run on the whole table if you replace my `#results` table with the actual table of data you have. You may need to add column into which to place your substrings if they do not already exist. – APH Feb 26 '19 at 17:32
  • Alternatively, you can replace `select @str` with the actual column of your original substring, in which case the code would create the `#results` temp table, drop your data in, and run the updates on all of your original strings. – APH Feb 26 '19 at 17:33