¿Cómo obtener el primer carácter de una cadena en SQL?


Tengo una columna SQL con una longitud de 6. Ahora quiero tomar sólo el primer char de esa columna. ¿Hay alguna función de cadena en SQL para hacer esto?

Author: slartidan, 2009-04-27

7 answers

LEFT(colName, 1) también hará esto, también. Es equivalente a SUBSTRING(colName, 1, 1).

Me gusta LEFT, ya que me parece un poco más limpio, pero en realidad, no hay diferencia de ninguna manera.

 326
Author: Eric,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-12-30 09:40:23

Prefiero:

SUBSTRING (my_column, 1, 1)

Porque es la sintaxis estándar de SQL-92 y por lo tanto más portable.


Estrictamente hablando, la versión estándar sería

SUBSTRING (my_column FROM 1 FOR 1)

El punto es, transformar de uno a otro, por lo tanto a cualquier variación de proveedor similar, es trivial.

P. S. Solo recientemente se me señaló que las funciones en SQL estándar son deliberadamente contrarias, al tener listas de parámetros que no son los commalistas convencionales, con el fin de hacerlas fácilmente identificable como siendo de la norma!

 33
Author: onedaywhen,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-03-07 15:25:11

SUBSTRING ( MyColumn, 1 , 1 ) para el primer carácter y SUBSTRING ( MyColumn, 1 , 2 ) para los dos primeros.

 12
Author: Damovisa,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-04-27 05:08:58

Es sencillo lograr esto siguiendo:

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

Y bien

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

@ Resultado: his

O

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

@ Resultado: This i

 5
Author: C JC,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-07-19 07:56:39
SELECT SUBSTR(thatColumn, 1, 1) As NewColumn from student
 4
Author: Devendra Verma,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-11-19 19:56:58

ENTRADA

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
 1
Author: Shiv,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-11-19 06:59:05

Si busca el primer carácter de string en Sql string

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

=> return 4
 0
Author: LittleJC,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-05-11 15:07:07