Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL SELECT everything after a certain character

I need to extract everything after the last '=' (http://www.domain.com?query=blablabla - > blablabla) but this query returns the entire strings. Where did I go wrong in here:

SELECT RIGHT(supplier_reference, CHAR_LENGTH(supplier_reference) - SUBSTRING('=', supplier_reference))  FROM ps_product 
like image 570
popkutt Avatar asked Oct 21 '13 13:10

popkutt


People also ask

How do I get text after a specific character in SQL?

The SUBSTRING() extracts a substring with a specified length starting from a location in an input string. In this syntax: input_string can be a character, binary, text, ntext, or image expression. start is an integer that specifies the location where the returned substring starts.

How do I extract text before a specific character in SQL?

Using the charindex function allows you to search for the @ sign and find its starting position and then the substring is used to extract the characters before the @ sign.


2 Answers

select SUBSTRING_INDEX(supplier_reference,'=',-1) from ps_product; 

Please use this for further reference.

like image 76
Gaurav Pant Avatar answered Oct 04 '22 04:10

Gaurav Pant


Try this (it should work if there are multiple '=' characters in the string):

SELECT RIGHT(supplier_reference, (CHARINDEX('=',REVERSE(supplier_reference),0))-1) FROM ps_product 
like image 36
BWS Avatar answered Oct 04 '22 04:10

BWS