3

I have a query as below, that selects Description from table where description contains 'legal:'

At the moment it extracts everything in Description field. What I want to do is only extract 50 characters from 'legal:' keyword on.

SELECT Description
FROM Issues
WHERE
Description like '%legal:%'

Any help appreciated.

1
  • What database are you using? Do you want to output legal: as part of the 50 characters? Commented Sep 4, 2013 at 14:24

1 Answer 1

6

MySQL

SELECT SUBSTRING(SUBSTRING_INDEX(Description, 'legal:', -1), 1, 50)
FROM Issues
WHERE Description LIKE '%legal:%'

See a demo

SQL Server

SELECT SUBSTRING(Description, CHARINDEX('legal:', Description) + 6, CHARINDEX('legal:', Description) + 56)
FROM Issues
WHERE Description LIKE '%legal:%'
  AND CHARINDEX('legal:', Description) > 0

See a demo

Sign up to request clarification or add additional context in comments.

3 Comments

How about if I have a column type XML?
@SiKni8 You should write a new question on it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.