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

como dividir uma string por espaço no SQL

DECLARE @siteIds VARCHAR(255)
DECLARE @strngLen int
DECLARE @split TABLE(siteId VARCHAR(100))

SET @siteIds = 'My name is Jason Fernandez'

SET @strngLen = CHARINDEX(' ', @siteIds)

WHILE CHARINDEX(' ', @siteIds) > 0
BEGIN
    SET @strngLen = CHARINDEX(' ', @siteIds);

    INSERT INTO @split
    SELECT SUBSTRING(@siteIds,1,@strngLen - 1);

    SET @siteIds = SUBSTRING(@siteIds, @strngLen + 1, LEN(@siteIds));
END

INSERT INTO @split
SELECT @siteIds

SELECT * FROM @split