Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Substring SQL Select statement

Tags:

substring

I have a number of references with a length of 20 and I need to remove the 1st 12 numbers, replace with a G and select the next 7 numbers

An example of the format of the numbers being received

50125426598525412584

I then need to remove first 12 digits and select the next 7 (not including the last)

2541258

Lastly I need to put a G in front of the number so I'm left with

G25412584

My SQL is as follows:

SELECT SUBSTRING(ref, 12, 7) AS ref 
FROM mytable
WHERE ref LIKE '5012%'

The results of this will leave me with

25412584

But how do I insert the G in front of the number in the same SQL statement?

Many thanks

like image 440
Gselect Avatar asked Jan 20 '26 16:01

Gselect


2 Answers

SELECT 'G'+SUBSTRING(ref, 12, 7) AS ref FROM mytable where ref like '5012%'
like image 123
Gabriel Avatar answered Jan 23 '26 21:01

Gabriel


SELECT CONCAT( 'G', SUBSTRING('50125426598525412584', 13,7)) from dual;
like image 26
Rajesh Avatar answered Jan 23 '26 20:01

Rajesh



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!