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?
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?
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.
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!
SUBSTRING ( MyColumn, 1 , 1 ) for the first character and SUBSTRING ( MyColumn, 1 , 2 ) for the first two.
If you search the first char of string in Sql string
SELECT CHARINDEX('char', 'my char')
=> return 4
Select First two Character in selected Field with Left(string,Number of Char in int)
SELECT LEFT(FName, 2) AS FirstName FROM dbo.NameMaster