How to get first character of a string in SQL?

SqlSql ServerSql Server-2005

Sql Problem Overview


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?

Sql Solutions


Solution 1 - Sql

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.

Solution 2 - Sql

I prefer:

SUBSTRING (my_column, 1, 1)

because it is Standard SQL-92 syntax and therefore more portable.


Strictly speaking, the standard version would be

SUBSTRING (my_column FROM 1 FOR 1)

The point is, transforming from one to the other, hence to any similar vendor variation, is trivial.

p.s. It was only recently pointed out to me that functions in standard SQL are deliberately contrary, by having parameters lists that are not the conventional commalists, in order to make them easily identifiable as being from the standard!

Solution 3 - Sql

SUBSTRING ( MyColumn, 1 , 1 ) for the first character and SUBSTRING ( MyColumn, 1 , 2 ) for the first two.

Solution 4 - Sql

It is simple to achieve by the following

DECLARE @SomeString NVARCHAR(20) = 'This is some string'
DECLARE @Result NVARCHAR(20)

Either

SET @Result = SUBSTRING(@SomeString, 2, 3)
SELECT @Result

@Result = his

or

SET @Result = LEFT(@SomeString, 6)
SELECT @Result

@Result = This i

Solution 5 - Sql

SELECT SUBSTR(thatColumn, 1, 1) As NewColumn from student

Solution 6 - Sql

If you search the first char of string in Sql string

SELECT CHARINDEX('char', 'my char')

=> return 4

Solution 7 - Sql

INPUT

STRMIDDLENAME
--------------
Aravind Chaterjee
Shivakumar
Robin Van Parsee

SELECT STRMIDDLENAME, 
CASE WHEN INSTR(STRMIDDLENAME,' ',1,2) != 0 THEN SUBSTR(STRMIDDLENAME,1,1) || SUBSTR(STRMIDDLENAME,INSTR(STRMIDDLENAME,' ',1,1)+1,1)||
SUBSTR(STRMIDDLENAME,INSTR(STRMIDDLENAME,' ',1,2)+1,1)
WHEN INSTR(STRMIDDLENAME,' ',1,1) != 0 THEN SUBSTR(STRMIDDLENAME,1,1) || SUBSTR(STRMIDDLENAME,INSTR(STRMIDDLENAME,' ',1,1)+1,1)
ELSE SUBSTR(STRMIDDLENAME,1,1)
END AS FIRSTLETTERS
FROM Dual;

OUTPUT
STRMIDDLENAME                    FIRSTLETTERS
---------                        -----------------
Aravind Chaterjee                AC           
Shivakumar                       S
Robin Van Parsee                 RVP

Solution 8 - Sql

Select First two Character in selected Field with Left(string,Number of Char in int)

SELECT LEFT(FName, 2) AS FirstName FROM dbo.NameMaster

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVinodView Question on Stackoverflow
Solution 1 - SqlEricView Answer on Stackoverflow
Solution 2 - SqlonedaywhenView Answer on Stackoverflow
Solution 3 - SqlDamovisaView Answer on Stackoverflow
Solution 4 - Sqljet_choongView Answer on Stackoverflow
Solution 5 - SqlDevendra VermaView Answer on Stackoverflow
Solution 6 - SqlLittleJCView Answer on Stackoverflow
Solution 7 - SqlShivView Answer on Stackoverflow
Solution 8 - SqlDarshanView Answer on Stackoverflow