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:
- ID:
SELECT LEFT(OriginalFileName, charindex('_', OriginalFileName) -1) from IMPORT
- 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
- 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.