Bishoy Ezzat Bishoy Ezzat - 2 months ago 15
SQL Question

How to substring a phrase in SQL?

I have a Phrase like this:

DECLARE @helloworld NVARCHAR(100),@partHello NVARCHAR(10)
SET @helloworld='HelloWorld=10&HelloSQL=20'


I want to take the number between '=' and '&' , I used this statement but it didn't work ... it returns:

10&


here is the code I used:

DECLARE @helloworld NVARCHAR(100),@partHello NVARCHAR(10)
SET @helloworld='HelloWorld=10&HelloSQL=20'

SET @partHello =
SUBSTRING(@helloworld,CHARINDEX('=',@helloworld,0),CHARINDEX('&',@helloworld,0)-CHARINDEX('=',@helloworld,0))
PRINT @partHello


I want it to be dynamic.. If there is a number bigger than 10, then it will take all the number. I want it to start from '=' and end at '&'

any help?

Answer

Try this:

SET @partHello = 
SUBSTRING(@helloworld,CHARINDEX('=',@helloworld,0)+1,CHARINDEX('&',@helloworld,0)-(CHARINDEX('=',@helloworld,0))-1)
PRINT @partHello
Comments