sql >> Database >  >> RDS >> Sqlserver

Voornaam en achternaam extraheren

Declare @t table ( [Name] varchar(100) )

insert into @t ( Name )
VALUES ( 'dennis hopper' ), ('keanu reaves'), ('thatgirl') 

SELECT
    [Name],
    CHARINDEX(' ', [Name]),
    CASE WHEN CHARINDEX(' ', [Name]) > 0 THEN
        LEFT([Name],CHARINDEX(' ',[Name])-1)
    ELSE
        [Name]
    END as FIRST_NAME,
    CASE WHEN CHARINDEX(' ', [Name]) > 0 THEN
        SUBSTRING([Name],CHARINDEX(' ',[Name])+1, ( LEN([Name]) - CHARINDEX(' ',[Name])+1) )
    ELSE
        NULL
    END as LAST_NAME
FROM @t


  1. Netwerkbelasting testen met iPerf

  2. Een MySQL-tabelkolom substringen

  3. Vervangen in querysyntaxis

  4. Kan argument niet binden aan index 2 omdat de index buiten bereik is