I know there is already some of the same questions out there, but this one is confusing me.
I have this query :
SELECT
CASE
WHEN COALESCE(substring(location_name FROM '[0-9]+'), location_name) != ''
THEN COALESCE(substring(location_name FROM '[0-9]+'), location_name)
ELSE '1'
END AS sequence
FROM LOCATION
What I would like to get from that query is :
- If the
location_namedoesn't contains any numeric values, then return1 - If the
location_namecontains numeric values, get only the last numeric values (after string), i.e. c2 carousel 10, should return only10 - If the
location_namecontains only numeric values, then return1
But what I get is something like :
location_name expected result what I get
using [0-9] using [0-9]+
carousel 1 1 1 1
carousel 2 2 2 2
carousel 3 3 3 3
carousel 12 12 1 12
bottom banner 1 bottom banner bottom banner
c2 carousel 1 1 2 2
c2 carousel 3 3 2 2
59977 1 5 59977
Is it possible to do this in sql?
regexreturn onlyBooleanyou can't replace/filter text using regex, you should create custom string manipulation function to extract result