Sqlserver
 sql >> Database >  >> RDS >> Sqlserver

come dividere una stringa per spazio in 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