Skip to content
Advertisement

SUBSTRING / CHARINDEX ADVICE

I have the following field in my database:

|     FileName     |
+------------------+ 
| 4_20191118113041 |

What I am trying to achieve is just return the 4, so anything before the underscore _. That 4 though could be 100 or 1000 or 1000000 and so on – but I need to always return anything before the _.

I’m guessing it’s a combination of SUBSTRING / CHARINDEX but I can’t get it to do what I need – any help would be greatly appreciated.

Advertisement

Answer

Just:

substring(FileName, 1, charindex('_', FileName) - 1)
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement