I have a SQL column with length of 6. Now want to take only first char of that column. Is the any string function in SQL to do this?
+9
A:
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.
Cheers,
Eric
Eric
2009-04-27 05:07:21
I don't know about SQL server, but logically a database server may be able to optimise LEFT better than SUBSTRING when it is using an index.
thomasrutter
2009-04-27 05:26:09
+1
A:
SUBSTRING ( MyColumn, 1 , 1 )
for the first character and SUBSTRING ( MyColumn, 1 , 2 )
for the first two.
Damovisa
2009-04-27 05:08:58
A:
I prefer:
SUBSTRING (my_column, 1, 1)
because it is Standard SQL-92 syntax and therefore more portable.
onedaywhen
2009-04-27 14:37:29
A:
how in the world is SUBSTRING considered portable? It doesn't run in Oracle. That's a pretty sizeable codebase to be incompatible with...
Miles O'Toole
2010-06-10 17:37:54