Remove everything but the first character of a string in a MySQL query

I am trying to run a query directly in MySQL to execute a series of records and replace the field value with only the string field character.

for example based on a column named formname

row 1 - Renault
row 2 - Citreon
row 3 - Jaguar

      

It will come back ..

row 1 - R
row 2 - C
row 3 - J

      

I can do this easily using native PHP functions, but not sure how to do it using SUBSTR function of MYSQL function?

+3


source to share


2 answers


You can use left

update table
set column = left(column,1)

      

or for selection you can write



select col1,left(col2,1)
from table

      

Demo

+4


source


To get this with the SUBSTR () function as you ask, you should use:

SELECT Field1, SUBSTR(field2, 1, 1) 
FROM MyTable

      



Function definition: SUBSTR(str,pos,len)

+1


source







All Articles