Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (20.3k points)

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?

2 Answers

0 votes
by (40.7k points)

You can use MYSQL SUBSTRING () Function in this way:

Have a look at this video to understand where to use String Functions with syntax and examples.

LEFT (Col_Name, 1)

Or

SELECT SUBSTRING(Col_Name, 1, 1) AS ExtractString;

Parameters used in the SUBSTRING method is as follows:

        1.  Col_Name: This is required to extract the string.

       2.  1: This is required for start position. Where 1st position in the string is 1.

       3. 1: This is nothing but length which is required to extract the no of characters. It must be greater than equal to 1(or positive no).

Note: LEFT() and SUBSTRING() methods are equal.

You can master these queries and become proficient in SQL queries by enrolling in an industry-recognized SQL certification.

0 votes
by (3.1k points)

In SQL, we can use left operator which can help me to get the first character of any column.

For an example here is one code

SELECT LEFT(your_column, 1) AS first_char

FROM your_table;

Description:

LEFT(your_column, 1): Retrieves the first letter from your column.

AS first_char: This helps to give a name to the resultant column when displayed.

1.2k questions

2.7k answers

501 comments

693 users

Browse Categories

...