April 26, 2011 at 4:15 am
HI,
I HAVE STRING FOR EXMPLE LIKE THIS :
nhtinhfsgjesalgjdmexSUM=190SUMdsfsdfsdfsdsdsdf
i need only the value between SUM= TO SUM
in this case i need to extract value of 120
thanks sharon
April 26, 2011 at 4:31 am
Are you asking like this
declare @String varchar(1000)
select @String='nhtinhfsgjesalgjdmexSUM=190SUMdsfsdfsdfsdsdsdf'
Select SUBSTRING(@String, 1, CHARINDEX('SUM',@String)-1),REVERSE(SUBSTRING(REVERSE(@String), 1, CHARINDEX('MUS',REVERSE(@String))-1))
Thanks
Parthi
April 26, 2011 at 4:47 am
Is this something you are looking for?
declare @Find1 varchar(20)
declare @Find2 varchar(20)
declare @search varchar(50)
select @Find1='SUM', @Search='nSUM=123123190SUMdsfsdfsdfsdsdsdf'
select @Find2='SUM='
Select SUBSTRING(@Search,
case charindex(reverse(@Find2),reverse(@search)) when 0 then 0 else len(@search) - ( charindex(reverse(@Find2),reverse(@search)) + (len(@find2)-1) ) + 1 end + 4,
case charindex(reverse(@Find1),reverse(@search)) when 0 then 0 else len(@search) - ( charindex(reverse(@Find1),reverse(@search)) + (len(@find1)-1) ) + 1 end -6)
April 26, 2011 at 4:52 am
little crazy....
try with this...changed as dynamic...for the number considering the word SUM
declare @Find1 varchar(20)
declare @Find2 varchar(20)
declare @search varchar(50)
select @Find1='SUM', @Search='nhtinhfsgjesrtyrtywqeqweqwalgjdmexSUM=192340SUMdsfsdfsdfsdsdsdf'
select @Find2='SUM='
Select SUBSTRING(@Search,
case charindex(reverse(@Find2),reverse(@search)) when 0 then 0 else len(@search) - ( charindex(reverse(@Find2),reverse(@search)) + (len(@find2)-1) ) + 1 end + 4,
case charindex(reverse(@Find1),reverse(@search)) when 0 then 0 else len(@search) - ( charindex(reverse(@Find1),reverse(@search)) + (len(@find1)-1) ) + 1 end -
case charindex(reverse(@Find2),reverse(@search)) when 0 then 0 else len(@search) - ( charindex(reverse(@Find2),reverse(@search)) + (len(@find2)-1) ) + 1 end - 4
)
April 26, 2011 at 5:02 am
One more way
declare @String varchar(1000)
select @String='nhtinhfsgjsdereSalgjdmexSUM=1290SUMddssfsdfsdfsdsdsdf'
Select SUBSTRING(@String, 1, CHARINDEX('SUM',@String)-1),REVERSE(SUBSTRING(REVERSE(@String), 1, CHARINDEX('MUS',REVERSE(@String))-1))
select substring(@String,
charindex('SUM',@String)+len('SUM='),
charindex('SUM',@String,charindex('SUM',@String)+1)-charindex('SUM',@String)-len('SUM='))
Adi
--------------------------------------------------------------
To know how to ask questions and increase the chances of getting asnwers:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
April 26, 2011 at 5:07 am
Try This if you have only one "SUM=" per string
SELECT Substring (Substring(@String, Charindex('SUM=', @String) + 4, Len(@String) - Charindex('SUM=', @String)), 1, Patindex('%[a-z][A-Z]%', Substring(@String, Charindex('SUM=', @String) + 4, Len(@String) - Charindex('SUM=', @String))) - 1)
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply