Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL query to extract part of column

Tags:

sql

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.

like image 890
user2371384 Avatar asked Jan 27 '26 23:01

user2371384


1 Answers

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

like image 91
Kermit Avatar answered Jan 29 '26 12:01

Kermit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!