Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all i want to read some specific data from string in sql server 2008 i need help let me know what is efficient way to read it.my string also contains that specific word and also end with new line.
Declare @string as nvarchar(max)
declare @Head as nvarchar(100)
declare @HeadDetail as nvarchar(500)
Declare @Host as nvarchar(500)
Declare @Connection as nvarchar(500)
Declare @Referer as nvarchar(500)
set @string=
'OPTIONS /Ads/PageTerms/GetTerms HTTP/1.1
Host: apps.developermedia.com
Connection: keep-alive
Cache-Control: max-age=0
Access-Control-Request-Method: POST
Origin: http://www.codeproject.com
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML. like Gecko) Chrome/38.0.2125.111 Safari/537.36
Access-Control-Request-Headers: content-type
Accept: */*
Referer: http://www.codeproject.com/Articles/25600/Triggers-SQL-Server
Accept-Encoding: gzip.deflate.sdch
Accept-Language: en-US.en;q=0.8.ru;q=0.6

'

print 'Host: apps.developermedia.com'
print 'Referer: http://www.codeproject.com/Articles/25600/Triggers-SQL-Server'

can i print data in this way from this input @string and all other necessary fields.
Any Help will be really appreciated.
Posted
Comments
Shweta N Mishra 25-Nov-14 8:22am    
do mean to extract
'Host: apps.developermedia.com'
& 'Referer: http://www.codeproject.com/Articles/25600/Triggers-SQL-Server

from given string ?
Jawad Ahmed Tanoli 25-Nov-14 8:27am    
yes like host, Referer, Origin etc
Shweta N Mishra 25-Nov-14 8:32am    
Is the string format is fixed, I mean that After host line connection would come ?
and after Referer like Accept-Encoding line would appear
Jawad Ahmed Tanoli 25-Nov-14 9:29am    
no it may be come any where in string but each field must contains char(13) new line at the end.

1 solution

check this

SQL
Declare @string as nvarchar(max)
declare @Head as nvarchar(100)
declare @HeadDetail as nvarchar(500)
Declare @Host as nvarchar(500)
Declare @Connection as nvarchar(500)
Declare @Referer as nvarchar(500)
set @string=
'OPTIONS /Ads/PageTerms/GetTerms HTTP/1.1
Host: apps.developermedia.com
Connection: keep-alive
Cache-Control: max-age=0
Access-Control-Request-Method: POST
Origin: http://www.codeproject.com
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML. like Gecko) Chrome/38.0.2125.111 Safari/537.36
Access-Control-Request-Headers: content-type
Accept: */*
Referer: http://www.codeproject.com/Articles/25600/Triggers-SQL-Server
Accept-Encoding: gzip.deflate.sdch
Accept-Language: en-US.en;q=0.8.ru;q=0.6
 
'
declare @charindexloc int,@charindexlocNext int,@lengthOfString int

select @charindexloc=CHARINDEX('Host:',@string)
select @lengthOfString=LEN(@string)
select @charindexlocNext=CHARINDEX(CHAR(10),Substring(@string,@charindexloc,@lengthOfString))

select SUBSTRING(@string,@charindexloc,@charindexlocNext) As Host
 
select @charindexloc=CHARINDEX('Referer:',@string)
select @lengthOfString=LEN(@string)
select @charindexlocNext=CHARINDEX(CHAR(10),Substring(@string,@charindexloc,@lengthOfString))

select SUBSTRING(@string,@charindexloc,@charindexlocNext) As Referer
 
Share this answer
 
Comments
Jawad Ahmed Tanoli 25-Nov-14 10:36am    
Thank you very much :) now i have a way to go on :P but if any one of field is missing like Referer or host then it will not work .any how in my case all fields must be there :)
Shweta N Mishra 25-Nov-14 10:39am    
np; if any field is missing then obviously it wouldn't work, as it does not know where to start and which line represents referer etc.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900