Septiana Fajrin
Septiana Fajrin

Reputation: 115

How to searching and extracting substring from text in SQL Server?

I have formula in excel and i want use it in SQL Server. What queries i can use?

enter image description here

formula in Excel:

=IFERROR(MID(A2,FIND("=",A2,8)+1,FIND("Logic",A2,8)-4-FIND("=",A2,8)+1),MID(A2,FIND("=",A2,8)+1,5))

whats formula in SQL server?

thanks for your attention, and sorry for my bad English.

Upvotes: 0

Views: 179

Answers (2)

Serkan Arslan
Serkan Arslan

Reputation: 13403

You can try this.

SELECT CASE WHEN CHARINDEX('Logic',A2, 8) > 0 
    THEN SUBSTRING( A2, CHARINDEX( '=',A2, 8) + 1 , CHARINDEX('Logic',A2, 8) - 4 - CHARINDEX( '=',A2,8) + 1 ) 
    ELSE SUBSTRING( A2, CHARINDEX( '=',A2, 8) + 1 , 5 ) 
    END

Upvotes: 1

GeorgiG
GeorgiG

Reputation: 1101

If you just want the same result, this query might work for you. It uses Try-Catch logic:

declare @NCELL VARCHAR(200) = 'LABEL=BDGblablabalba,CellID=24539, LogicRNCID=382'

begin try
select isnull((SUBSTRING(@NCELL, CHARINDEX('CellID', @NCELL) + 7, CHARINDEX(', Logic', @NCELL) - (CHARINDEX('CellID', @NCELL) + 7))), 'N/A') AS Result
end try
begin catch
select SUBSTRING(@NCELL, CHARINDEX('CellID', @NCELL) + 7, 200) AS Result
end catch

Upvotes: 0

Related Questions