My First post here.!
I would like to get the String which is between the Nth Hypens of the given string.
Ex: 123_234_234_345_34A_456 The hypen count may vary, but I would like to get exactly the string within the 4th and 5th Hypen i.e as per example we want 34A .! Will be helpful if someone can guide me to achieve this.! Trying to achieve it in SQL Server.
asked Apr 18, 2017 at 09:00 AM in Default
A quick way is to use the delimited split function written by Jeff Moden on SQLServerCentral.
If you've got that created, then the query is as simple as:
(5, because you want the fifth string in the set).
This method will work on many versions of SQL Server, but is limited to the size of input string it'll accept.
If you've got SQL Server 2016, you could just use the in-built 'STRING_SPLIT' function, but there are no guarantees that data will be returned in the right order...
answered Apr 19, 2017 at 02:16 PM
If it is always going to be 3 digits then something simple like SUBSTRING would work
o/w you probably want to create a function - Or you could bodge it by nesting CHARINDEX. The example below should give you an idea of how to do it:
SELECT CHARINDEX('',@item, CHARINDEX('',@item,CHARINDEX('_',@item)+1) +1 )
answered Apr 18, 2017 at 11:51 AM