Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get first character of a string in SQL?

I have a SQL column with a length of 6. Now want to take only the first char of that column. Is there any string function in SQL to do this?

like image 321
Vinod Avatar asked Apr 27 '09 05:04

Vinod


People also ask

How do I get the first 10 characters of a string in SQL?

SQL Server LEFT() Function The LEFT() function extracts a number of characters from a string (starting from left).

How do I get the first 5 characters in SQL?

You can use LEN() or LENGTH()(in case of oracle sql) function to get the length of a column. SELECT LEN(column_name) FROM table_name; And you can use SUBSTRING or SUBSTR() function go get first three characters of a column.


1 Answers

LEFT(colName, 1) will also do this, also. It's equivalent to SUBSTRING(colName, 1, 1).

I like LEFT, since I find it a bit cleaner, but really, there's no difference either way.

like image 116
Eric Avatar answered Sep 20 '22 13:09

Eric