Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

My sql server 2008 query returns a very long strings such as:

GET /maker/www/jsp/ HTTP/1.1

I know I can do things like left(columnname, 10) to return the first 10 characters, but what I am really interested in is the part


More specifically, I just want the value getid is equal to e.g.


Any tips?

share|improve this question
Read up on SUBSTRING and CHARINDEX. There are plenty of string functions and operators in SQL Server that can help you accomplish these tasks. – Aaron Bertrand May 17 '12 at 11:39
That's a great pointer thanks. I'd accept that as answer. – dublintech May 17 '12 at 11:47
Also your body says SQL Server 2008 and the tag says SQL Server 2008 R2. In this case it is largely irrelevant but in some cases it might be important to be accurate. – Aaron Bertrand May 17 '12 at 12:13

SET @s = 'GET ...whatever...KZHg8&getid=WS00D3000asdasdasd&menu=...whatever...';

  FROM (SELECT s = SUBSTRING(@s, CHARINDEX('getid=', @s)+6, 8000)) AS x;

This will also handle the case where getid is the last name/value pair.



share|improve this answer

Play with this:

DECLARE @MyString as NVARCHAR(max) = 'GET /maker/www/jsp/ HTTP/1.1',
@MySubstring as NVARCHAR(max) = '',
@StartString as NVARCHAR(max) = '&getid=',
@EndString as NVARCHAR(max) = '&menu=',
@SubStringFrom as INT,
@SubStringFor as INT

-- find the starting position
SET @SubStringFrom = CHARINDEX(@StartString, @MyString) + LEN(@StartString)

-- find the offset
SET @SubStringFor = CHARINDEX(@EndString, @MyString) - CHARINDEX(@StartString, @MyString) - LEN(@StartString)

SELECT @MySubstring = SUBSTRING(@MyString, @SubStringFrom, @SubStringFor)

SELECT @MySubstring

Then go and do what Aaron says and readup on CHARINDEX and SUBSTRING

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.