作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有两列,我想创建一个拆分函数,它将接受一个字符串并将每两个连续数字放入我的列中。
例如:
如果我的字符串是(1,5,2,20,3,9).
结果应该是:
size quantity
1 | 5
2 | 20
3 | 9
这是我一直在尝试的代码:
create FUNCTION [dbo].[getSizesAndQuantity1](@input AS Varchar(4000) )
RETURNS
@Result TABLE(Size BIGINT, Quantity BIGINT)
AS
BEGIN
DECLARE @str int
DECLARE @strQ int
DECLARE @ind Int
IF(@input is not null)
BEGIN
SET @ind = CharIndex(',',@input)
WHILE @ind > 0
BEGIN
SET @str = SUBSTRING(@input,1,@ind-1)
SET @input = SUBSTRING(@input,@ind+1,LEN(@input)-@ind)
SET @strQ = SUBSTRING(@input,1,@ind-1)
SET @input = SUBSTRING(@input,@ind+1,LEN(@input)-@ind)
INSERT INTO @Result(Size,Quantity) values (@str,@strQ)
SET @ind = CharIndex(',',@input)
END
END
RETURN
END
感谢您的帮助。
最佳答案
select size = max(case when ItemNumber % 2 = 1 then Item end),
quantity = max(case when ItemNumber % 2 = 0 then Item end)
from DelimitedSplit8K('1,5,2,20,3,9', ',')
group by (ItemNumber - 1) / 2
关于sql - 如何将单个字符串拆分为两个不同的列 sql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37647815/
我是一名优秀的程序员,十分优秀!