Sqlserver
 sql >> Base de Dados >  >> RDS >> Sqlserver

Como obter substring com a função interna do T-sql


Aproveite PARSENAME ?
SELECT
   PARSENAME(REPLACE(name, ' ', '.'), 2),
   PARSENAME(REPLACE(name, ' ', '.'), 1)
FROM
    MyTable;

EDITAR exemplo de trabalho:
DECLARE @MyTable table (name varchar(25))
INSERT @MyTable 
    SELECT 'Joe Smith' 
    UNION SELECT 'Bill Jones' 
    UNION SELECT 'Billy Bob Braxton' 
    UNION SELECT 'Mark'

SELECT
   PARSENAME(REPLACE(name, ' ', '.'), 2),
   PARSENAME(REPLACE(name, ' ', '.'), 1)
FROM
    @MyTable;

Resultado:
------ --------
Bill   Jones
Bob    Braxton
Joe    Smith
NULL   Mark    

(4 row(s) affected)