Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to substring a MySQL table column

Tags:

database

mysql

I want to select a field from table and substring it.

For example:

VAN1031 --> 1031

I tried this, but is improper syntax:

SELECT SUBSTR(R.regnumber,3,3) from registration R

How can this be done?

like image 372
Rakesh Avatar asked Oct 07 '12 17:10

Rakesh


People also ask

How do I substring in MySQL?

SUBSTRING() :function in MySQL is used to derive substring from any given string . It extracts a string with a specified length, starting from a given location in an input string. The purpose of substring is to return a specific portion of the string.

Which function is used to extract substring from column of the table?

SUBSTRING() is a function that enables us to extract subparts of strings, which are known as substrings. The strings we want to extract from can be specified in the function itself, or they can be a part of a table's columns. Using this function, we can extract as many substrings as we want from a single string.

How do I print the first letter of a string in MySQL?

MySQL query to display the first alphabet from strings in a separate column. To fetch the first alphabet from the strings, use LEFT(). This method allows you to return characters from the left of the string.


3 Answers

You don't need the third argument (length) if you want to select all the characters to the right of a specific index:

SELECT SUBSTR(R.regnumber, 4)
FROM registration AS R

I also changed the start index to 4 because in SQL strings are 1-indexed and not 0-indexed as they are in many popular programming languages.

like image 91
Mark Byers Avatar answered Oct 24 '22 01:10

Mark Byers


You can use:

SUBSTR(string,position)
SUBSTR(string,position,length)
SUBSTRING_INDEX(string, delimiter, count)

Examples:

command                                      prints
-------------------------------------------  -----------
select substr("abcd", 1, 1)                  #a
select substr("abcd", 1, 2)                  #ab
select substr("abcd", 2, 1)                  #b
select substr("abcd", 2, 2)                  #bc
select substr("abcd", -2, 1)                 #c
select substr("abcd", -2, 2)                 #cd

select substring_index('ababab', 'b', 1);    #a
select substring_index('ababab', 'b', 2);    #aba
select substring_index('ababab', 'b', 3);    #ababa
select substring_index('ababab', 'b', -1);   #
select substring_index('ababab', 'b', -2);   #ab
select substring_index('ababab', 'b', -3);   #abab

select substr("abcd", 2)                     #bcd
select substr("abcd", 3)                     #cd
select substr("abcd", 4)                     #d
select substr("abcd", -2)                    #cd
select substr("abcd", -3)                    #bcd
select substr("abcd", -4)                    #abcd

From this link.

like image 44
Anil Limbani Avatar answered Oct 24 '22 01:10

Anil Limbani


You can use SUBSTRING():

select substring(col1, 4)
from table1

See SQL Fiddle with Demo. So your query would be:

SELECT substring(R.regnumber,4) 
from registration R

Of if you want to specify the number of characters to return:

select substring(col1, 4, 4)
from table1
like image 7
Taryn Avatar answered Oct 24 '22 01:10

Taryn