views:

4411

answers:

5

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?

+3  A: 

Well, how about SUBSTRING?

Promit
+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
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
+1  A: 

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

Damovisa
A: 

I prefer:

SUBSTRING (my_column, 1, 1)

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

onedaywhen
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