相当简单的问题,但似乎无法得到答案。
如果我有这个:
declare @Agent nvarchar(4000) = '2131235,334225';
我是否可以编写一个select语句来拆分昏迷所在的字符串,而不必编写函数?
类似的东西:
SELECT SOME_SPLIT_LOGIC(@Agent)
我希望它返回:
Column
--------
2131235
334225
请注意我使用的是MS-SQL 2012
答案 0 :(得分:0)
DECLARE @LeftPart TABLE
(Agent int
)
DECLARE @RightPart TABLE
(Agent1 int
)
declare @Agent nvarchar(4000) = '2131235,334225';
INSERT INTO @LeftPart
SELECT LEFT(@Agent,7)
INSERT INTO @RightPart
SELECT RIGHT(@Agent,6)
SELECT * INTO #Temp
FROM
(
SELECT * FROM @LeftPart
UNION ALL
SELECT * FROM @RightPart
)A
SELECT * FROM #Temp
答案 1 :(得分:0)
怎么样:
DECLARE @Agent VARCHAR(4000) = '2131235,334225'
DECLARE @Delimiter VARCHAR(1) = ','
;WITH CTE
AS
(
SELECT 1 AS ID
UNION ALL
SELECT ID + 1 FROM CTE
WHERE ID < 4000
)
SELECT
SUBSTRING(@Agent, t.ID, CHARINDEX(@Delimiter, @Agent + @Delimiter, t.ID) - t.ID) AS Agent
FROM CTE t
WHERE t.ID <= DATALENGTH(@Agent)+1
AND SUBSTRING(@Delimiter + @Agent, t.ID, 1) = @Delimiter
OPTION (MAXRECURSION 4000)
可在此处找到更多信息:http://www.sqlservercentral.com/articles/Tally+Table/72993/