Home > Mobile >  GET particular value from split string result in sql
GET particular value from split string result in sql

Time:09-17

I FIRE BELOVED QUERY

SELECT value
FROM STRING_SPLIT('3G-4RPE-L250-S80-27K-UNV-DIM-WT-RG1-CP-S(9)', '-') 

I GOT FOLLLOWING RESULT

VALUE
3G
4RPE
L250
S80
27K
UNV
DIM
WT
RG1
CP
S(9)

IS IT POSSSIBLE TO GET PERTICULAR WORD LIKE 4RPE, RG1 FROM ABOVE REULT SET IN SQL?

CodePudding user response:

One option is with a bit of JSON. Here we convert your string into a JSON Array. Then it becomes a small matter to extract the values.

Example

Declare @S varchar(max) =  '3G-4RPE-L250-S80-27K-UNV-DIM-WT-RG1-CP-S(9)'
Set @S = '["' replace(string_escape(@S,'json'),'-','","') '"]'

Select Pos1 = JSON_VALUE(@S,'$[1]')
      ,Pos8 = JSON_VALUE(@S,'$[8]')

Results

Pos1    Pos8
4RPE    RG1

2nd Option

Select *
 From  OpenJSON(@S)
 Where [key] in (1,8)

Results

key value   type
1   4RPE    1
8   RG1     1
  • Related