SELECT substring(NameValue, 1, charindex('_', NameValue)-1) AS Names,
substring(NameValue, charindex('_', NameValue)+1, LEN(NameValue)) AS Values
FROM Table
REDIGERA :Något liknande detta i en funktion eller lagrad procedur kombinerat med en temporär tabell bör fungera för mer än en rad, beroende på radavgränsaren bör du också ta bort CHAR(13)
innan du börjar:
DECLARE @helper varchar(512)
DECLARE @current varchar(512)
SET @helper = NAMEVALUE
WHILE CHARINDEX(CHAR(10), @helper) > 0 BEGIN
SET @current = SUBSTRING(@helper, 1, CHARINDEX(CHAR(10), NAMEVALUE)-1)
SELECT SUBSTRING(@current, 1, CHARINDEX('_', @current)-1) AS Names,
SUBSTRING(@current, CHARINDEX('_', @current)+1, LEN(@current)) AS Names
SET @helper = SUBSTRING(@helper, CHARINDEX(CHAR(10), @helper)+1, LEN(@helper))
END
SELECT SUBSTRING(@helper, 1, CHARINDEX('_', @helper)-1) AS Names,
SUBSTRING(@helper, CHARINDEX('_', @helper)+1, LEN(@helper)) AS Names