我有这样的输入4541
或45
或124
我想将输入分成这样的数字:
如果输入为1235
,我想采用1
,2
,3
,5
我试图在google上搜索,但我找到的所有内容都是将数字从float中分割出来或将字符串更改为数字。
你可以帮忙吗?答案 0 :(得分:2)
方法1:
试试这个
<强>功能:强>
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SplitIntoIndividualLetters]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[SplitIntoIndividualLetters]
GO
CREATE FUNCTION [dbo].[SplitIntoIndividualLetters](@Str varchar(8000))
Returns @Lettertable TABLE (letters varchar(8000))
AS
Begin
;With NumCte As
(
Select Number = 1 Union All
Select Number +1 From NumCte
Where Number < 1000
)
Insert Into @Lettertable(letters)
Select Substring(@str,Number,1)
From NumCte
where Number Between 1 And Len(@str)
Option (Maxrecursion 0)
Return
End
使用方法:
Declare @str Varchar(50) = '12345'
DECLARE @listStr VARCHAR(MAX)
SELECT @listStr = COALESCE(@listStr+',' ,'') + letters
FROM dbo.SplitIntoIndividualLetters(@str)
SELECT @listStr
<强>输出:强>
1,2,3,4,5
方法2:
我想出了Donal解决方案:
DECLARE @str VARCHAR(50),@Inc INT,@len INT,@char VARCHAR(50)
SET @str = '12345'
SET @Inc = 1
SET @len = LEN(@str)
WHILE @Inc<= @len
BEGIN
SET @char = COALESCE(@char+',' ,'') + SUBSTRING(@str, @Inc, 1)
SET @Inc=@Inc+1
END
SELECT @char
输出:
1,2,3,4,5
数字到Word
CREATE FUNCTION fnIntegerToWords(@Number as BIGINT)
RETURNS VARCHAR(1024)
AS
BEGIN
DECLARE @Below20 TABLE (ID int identity(0,1), Word varchar(32))
DECLARE @Below100 TABLE (ID int identity(2,1), Word varchar(32))
INSERT @Below20 (Word) VALUES
( 'Zero'), ('One'),( 'Two' ), ( 'Three'),
( 'Four' ), ( 'Five' ), ( 'Six' ), ( 'Seven' ),
( 'Eight'), ( 'Nine'), ( 'Ten'), ( 'Eleven' ),
( 'Twelve' ), ( 'Thirteen' ), ( 'Fourteen'),
( 'Fifteen' ), ('Sixteen' ), ( 'Seventeen'),
('Eighteen' ), ( 'Nineteen' )
INSERT @Below100 VALUES ('Twenty'), ('Thirty'),('Forty'), ('Fifty'),
('Sixty'), ('Seventy'), ('Eighty'), ('Ninety')
declare @belowHundred as varchar(126)
if @Number > 99 begin
select @belowHundred = dbo.fnIntegerToWords( @Number % 100)
end
DECLARE @English varchar(1024) =
(
SELECT Case
WHEN @Number = 0 THEN ''
WHEN @Number BETWEEN 1 AND 19
THEN (SELECT Word FROM @Below20 WHERE ID=@Number)
WHEN @Number BETWEEN 20 AND 99
THEN (SELECT Word FROM @Below100 WHERE ID=@Number/10)+ '-' +
dbo.fnIntegerToWords( @Number % 10)
WHEN @Number BETWEEN 100 AND 999
THEN (dbo.fnIntegerToWords( @Number / 100)) +' Hundred '+
Case WHEN @belowHundred <> '' THEN 'and ' + @belowHundred else @belowHundred end
WHEN @Number BETWEEN 1000 AND 999999
THEN (dbo.fnIntegerToWords( @Number / 1000))+' Thousand '+
dbo.fnIntegerToWords( @Number % 1000)
WHEN @Number BETWEEN 1000000 AND 999999999
THEN (dbo.fnIntegerToWords( @Number / 1000000))+' Million '+
dbo.fnIntegerToWords( @Number % 1000000)
WHEN @Number BETWEEN 1000000000 AND 999999999999
THEN (dbo.fnIntegerToWords( @Number / 1000000000))+' Billion '+
dbo.fnIntegerToWords( @Number % 1000000000)
ELSE ' INVALID INPUT' END
)
SELECT @English = RTRIM(@English)
SELECT @English = RTRIM(LEFT(@English,len(@English)-1))
WHERE RIGHT(@English,1)='-'
RETURN (@English)
END
<强>用法:强>
SELECT dbo.fnIntegerToWords(5) As Word
Word
Five
以逗号分隔列表
获取答案试试这个
Declare @str Varchar(50) = '12345'
DECLARE @listStr VARCHAR(MAX)
SELECT @listStr = COALESCE(@listStr+',' ,'') + dbo.fnIntegerToWords(letters)
FROM dbo.SplitIntoIndividualLetters(@str)
SELECT @listStr
输出:
One,Two,Three,Four,Five
答案 1 :(得分:1)
您需要将其视为字符串。然后你可以使用SUBSTRING获取每个角色。
DECLARE @str VARCHAR(50)
DECLARE @i INT
DECLARE @len INT
DECLARE @char VARCHAR(50)
DECLARE @result VARCHAR(50)
SET @str = '1235'
SET @i = 1
SET @len = LEN(@str)
WHILE @i<= @len
BEGIN
SET @char = SUBSTRING(@str, @i, 1)
@result = @char + ", "
SET @i=@i+1
END
答案 2 :(得分:0)
没有必要像其他人那样声明这么多变量,它很简单。这对我有用:试试吧
declare @intvalue int
declare @incremnt int
declare @data varchar(20)
set @intvalue = 1234
set @data = CAST(@intvalue as varchar(10))
set @incremnt = 1
while(LEN(@data) >= @incremnt)
begin
select SUBSTRING(@data,@incremnt,1)
set @incremnt = @incremnt + 1
end