Reputation: 99
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?
Upvotes: 0
Views: 92
Reputation: 14669
Try this:
SET @partHello =
SUBSTRING(@helloworld,CHARINDEX('=',@helloworld,0)+1,CHARINDEX('&',@helloworld,0)-(CHARINDEX('=',@helloworld,0))-1)
PRINT @partHello
Upvotes: 3
Reputation: 9143
I would use following:
SELECT SUBSTRING(@helloWorld,
CHARINDEX('=',@helloworld,0)+1,
CHARINDEX('&',@helloworld,0)-CHARINDEX('=',@helloworld,0)-1)
Upvotes: 3
Reputation: 444
There is the solution :
DECLARE @helloworld NVARCHAR(100),@partHello NVARCHAR(10)
SET @helloworld='HelloWorld=320&HelloSQL=20'
SET @partHello =
SUBSTRING(@helloworld,CHARINDEX('=',@helloworld,0)+1,CHARINDEX('&',@helloworld,0) -(CHARINDEX('=',@helloworld,0)+1))
PRINT @partHello
Upvotes: 0