Return UDF 中的前五个数字构成字符串
Return first five numbers form string within UDF
我正在研究 SQL Server
(2005、2008 和 2012)
我想通过使用 UDF
从 varchar 列中提取前五个数字
输入:
rrr123ddd4567ddd19828www2
123hhhsss124ss18762s
qq12349wsss12376ss
输出:
19828
18762
12349
我的足迹如下:
DECLARE
@myString VARCHAR(1000),
@temp VARCHAR(100),
@position INT,
@ExecuteInsert nvarchar (500),
@FirstChar bit
SET @myString = 'rrr123ddd4567ddd19828www2'
SET @position = 1
SET @FirstChar = 1
WHILE @position <= LEN(@myString)
BEGIN
IF (ISNUMERIC(SUBSTRING(@myString,@position,1))) = 1
BEGIN
SET @temp = isnull(@temp,'') + SUBSTRING(@myString,@position,1)
SET @FirstChar = 1
END
ELSE /* The char is alphabetical */
BEGIN
if (@FirstChar= 1)
BEGIN
SET @temp = isnull(@temp,'') + ','
SET @FirstChar = 0
END
END
SET @position = @position + 1
END
IF (RIGHT(@temp,1) <> ',')
BEGIN
SET @temp = @temp + ','
END
SELECT @temp = REPLACE(','+ @temp + ',',',,','')
SELECT @temp = Replace (@temp,',','''),(''')
Select @temp = '(''' + @temp + ''')'
Create table #temp
(
col1 varchar(100)
)
SET @ExecuteInsert = 'insert into #temp values ' + @temp
Execute sp_executesql @ExecuteInsert
select top 1 col1 from #temp
where LEN(col1) = 5
drop table #temp
-- Output >> 19828
之前的查询与字符串输入配合使用效果很好,但我想在 UDF
中使用此代码以将其与列一起使用。
如果我在 UDF 中使用之前的查询,则会引发以下错误:
Cannot access temporary tables from within a function.
编辑
如果我使用 Table 变量,我会得到下一个错误:
Only functions and some extended stored procedures can be executed
from within a function.
任何帮助将不胜感激。
CREATE FUNCTION udfTest
(
-- Add the parameters for the function here
)
RETURNS int
AS
BEGIN
-- Declare the return variable here
DECLARE
@Result int,
@myString VARCHAR(1000),
@temp VARCHAR(100),
@position INT,
@ExecuteInsert nvarchar (500),
@FirstChar bit
SET @myString = 'rrr123ddd4567ddd19828www2'
SET @position = 1
SET @FirstChar = 1
WHILE @position <= LEN(@myString)
BEGIN
IF (ISNUMERIC(SUBSTRING(@myString,@position,1))) = 1
BEGIN
SET @temp = isnull(@temp,'') + SUBSTRING(@myString,@position,1)
SET @FirstChar = 1
END
ELSE /* The char is alphabetical */
BEGIN
if (@FirstChar= 1)
BEGIN
SET @temp = isnull(@temp,'') + ','
SET @FirstChar = 0
END
END
SET @position = @position + 1
END
IF (RIGHT(@temp,1) <> ',')
BEGIN
SET @temp = @temp + ','
END
SELECT @temp = REPLACE(','+ @temp + ',',',,','')
SELECT @temp = Replace (@temp,',','''),(''')
Select @temp = '(''' + @temp + ''')'
Declare @tempTable TABLE
(
col1 varchar(100)
)
insert into @tempTable SELECT @temp
select top 1 @Result=col1 from @tempTable
where LEN(col1) = 5
return @Result
END
GO
这是我对问题的回答,希望对大家有帮助。
objective 正在创建 UDF
函数以将其用于列,而不仅仅是固定值。
该方法使用 SplitString 而不是 sp_executesql
用于拆分逗号分隔的字符串并在 table 中循环它的值。
演示:-
Create table DummyTable
( col1 varchar (100))
go
Insert into DummyTable values ('rrr123ddd4567ddd19828www2')
Insert into DummyTable values ('123hhhsss124ss18762s')
Insert into DummyTable values ('qq12349wsss12376ss')
go
/*
SplitString via Mudassar Khan
http://www.aspsnippets.com/Articles/Split-and-convert-Comma-Separated-Delimited-String-to-Table-in-SQL-Server.aspx
*/
Create FUNCTION SplitString
(
@Input NVARCHAR(MAX),
@Character CHAR(1)
)
RETURNS @Output TABLE (
Item NVARCHAR(1000)
)
AS
BEGIN
DECLARE @StartIndex INT, @EndIndex INT
SET @StartIndex = 1
IF SUBSTRING(@Input, LEN(@Input) - 1, LEN(@Input)) <> @Character
BEGIN
SET @Input = @Input + @Character
END
WHILE CHARINDEX(@Character, @Input) > 0
BEGIN
SET @EndIndex = CHARINDEX(@Character, @Input)
INSERT INTO @Output(Item)
SELECT SUBSTRING(@Input, @StartIndex, @EndIndex - 1)
SET @Input = SUBSTRING(@Input, @EndIndex + 1, LEN(@Input))
END
RETURN
END
GO
-------------------------------------
-------------------------------------
-------------------------------------
/*
My Own Function
*/
Create FUNCTION udfGetFirstFiveNumbers
(
@myString VARCHAR(1000)
)
RETURNS varchar(100)
AS
BEGIN
DECLARE
@temp VARCHAR(100),
@result Varchar (100),
@position INT,
@ExecuteInsert nvarchar (500),
@FirstChar bit
SET @position = 1
SET @FirstChar = 1
WHILE @position <= LEN(@myString)
BEGIN
IF (ISNUMERIC(SUBSTRING(@myString,@position,1))) = 1
BEGIN
SET @temp = isnull(@temp,'') + SUBSTRING(@myString,@position,1)
SET @FirstChar = 1
END
ELSE /* The char is alphabetical */
BEGIN
if (@FirstChar= 1)
BEGIN
SET @temp = isnull(@temp,'') + ','
SET @FirstChar = 0
END
END
SET @position = @position + 1
END
IF (RIGHT(@temp,1) <> ',')
BEGIN
SET @temp = @temp + ','
END
SELECT @temp = REPLACE(','+ @temp + ',',',,','')
SELECT @result = Item
FROM dbo.SplitString(@temp, ',')
where len(Item) = 5
return @result
END
GO
-- Test
select col1, dbo.udfGetFirstFiveNumbers(col1) as result
from DummyTable
结果:-
我正在研究 SQL Server
(2005、2008 和 2012)
我想通过使用 UDF
输入:
rrr123ddd4567ddd19828www2
123hhhsss124ss18762s
qq12349wsss12376ss
输出:
19828
18762
12349
我的足迹如下:
DECLARE
@myString VARCHAR(1000),
@temp VARCHAR(100),
@position INT,
@ExecuteInsert nvarchar (500),
@FirstChar bit
SET @myString = 'rrr123ddd4567ddd19828www2'
SET @position = 1
SET @FirstChar = 1
WHILE @position <= LEN(@myString)
BEGIN
IF (ISNUMERIC(SUBSTRING(@myString,@position,1))) = 1
BEGIN
SET @temp = isnull(@temp,'') + SUBSTRING(@myString,@position,1)
SET @FirstChar = 1
END
ELSE /* The char is alphabetical */
BEGIN
if (@FirstChar= 1)
BEGIN
SET @temp = isnull(@temp,'') + ','
SET @FirstChar = 0
END
END
SET @position = @position + 1
END
IF (RIGHT(@temp,1) <> ',')
BEGIN
SET @temp = @temp + ','
END
SELECT @temp = REPLACE(','+ @temp + ',',',,','')
SELECT @temp = Replace (@temp,',','''),(''')
Select @temp = '(''' + @temp + ''')'
Create table #temp
(
col1 varchar(100)
)
SET @ExecuteInsert = 'insert into #temp values ' + @temp
Execute sp_executesql @ExecuteInsert
select top 1 col1 from #temp
where LEN(col1) = 5
drop table #temp
-- Output >> 19828
之前的查询与字符串输入配合使用效果很好,但我想在 UDF
中使用此代码以将其与列一起使用。
如果我在 UDF 中使用之前的查询,则会引发以下错误:
Cannot access temporary tables from within a function.
编辑
如果我使用 Table 变量,我会得到下一个错误:
Only functions and some extended stored procedures can be executed from within a function.
任何帮助将不胜感激。
CREATE FUNCTION udfTest
(
-- Add the parameters for the function here
)
RETURNS int
AS
BEGIN
-- Declare the return variable here
DECLARE
@Result int,
@myString VARCHAR(1000),
@temp VARCHAR(100),
@position INT,
@ExecuteInsert nvarchar (500),
@FirstChar bit
SET @myString = 'rrr123ddd4567ddd19828www2'
SET @position = 1
SET @FirstChar = 1
WHILE @position <= LEN(@myString)
BEGIN
IF (ISNUMERIC(SUBSTRING(@myString,@position,1))) = 1
BEGIN
SET @temp = isnull(@temp,'') + SUBSTRING(@myString,@position,1)
SET @FirstChar = 1
END
ELSE /* The char is alphabetical */
BEGIN
if (@FirstChar= 1)
BEGIN
SET @temp = isnull(@temp,'') + ','
SET @FirstChar = 0
END
END
SET @position = @position + 1
END
IF (RIGHT(@temp,1) <> ',')
BEGIN
SET @temp = @temp + ','
END
SELECT @temp = REPLACE(','+ @temp + ',',',,','')
SELECT @temp = Replace (@temp,',','''),(''')
Select @temp = '(''' + @temp + ''')'
Declare @tempTable TABLE
(
col1 varchar(100)
)
insert into @tempTable SELECT @temp
select top 1 @Result=col1 from @tempTable
where LEN(col1) = 5
return @Result
END
GO
这是我对问题的回答,希望对大家有帮助。
objective 正在创建 UDF
函数以将其用于列,而不仅仅是固定值。
该方法使用 SplitString 而不是 sp_executesql
用于拆分逗号分隔的字符串并在 table 中循环它的值。
演示:-
Create table DummyTable
( col1 varchar (100))
go
Insert into DummyTable values ('rrr123ddd4567ddd19828www2')
Insert into DummyTable values ('123hhhsss124ss18762s')
Insert into DummyTable values ('qq12349wsss12376ss')
go
/*
SplitString via Mudassar Khan
http://www.aspsnippets.com/Articles/Split-and-convert-Comma-Separated-Delimited-String-to-Table-in-SQL-Server.aspx
*/
Create FUNCTION SplitString
(
@Input NVARCHAR(MAX),
@Character CHAR(1)
)
RETURNS @Output TABLE (
Item NVARCHAR(1000)
)
AS
BEGIN
DECLARE @StartIndex INT, @EndIndex INT
SET @StartIndex = 1
IF SUBSTRING(@Input, LEN(@Input) - 1, LEN(@Input)) <> @Character
BEGIN
SET @Input = @Input + @Character
END
WHILE CHARINDEX(@Character, @Input) > 0
BEGIN
SET @EndIndex = CHARINDEX(@Character, @Input)
INSERT INTO @Output(Item)
SELECT SUBSTRING(@Input, @StartIndex, @EndIndex - 1)
SET @Input = SUBSTRING(@Input, @EndIndex + 1, LEN(@Input))
END
RETURN
END
GO
-------------------------------------
-------------------------------------
-------------------------------------
/*
My Own Function
*/
Create FUNCTION udfGetFirstFiveNumbers
(
@myString VARCHAR(1000)
)
RETURNS varchar(100)
AS
BEGIN
DECLARE
@temp VARCHAR(100),
@result Varchar (100),
@position INT,
@ExecuteInsert nvarchar (500),
@FirstChar bit
SET @position = 1
SET @FirstChar = 1
WHILE @position <= LEN(@myString)
BEGIN
IF (ISNUMERIC(SUBSTRING(@myString,@position,1))) = 1
BEGIN
SET @temp = isnull(@temp,'') + SUBSTRING(@myString,@position,1)
SET @FirstChar = 1
END
ELSE /* The char is alphabetical */
BEGIN
if (@FirstChar= 1)
BEGIN
SET @temp = isnull(@temp,'') + ','
SET @FirstChar = 0
END
END
SET @position = @position + 1
END
IF (RIGHT(@temp,1) <> ',')
BEGIN
SET @temp = @temp + ','
END
SELECT @temp = REPLACE(','+ @temp + ',',',,','')
SELECT @result = Item
FROM dbo.SplitString(@temp, ',')
where len(Item) = 5
return @result
END
GO
-- Test
select col1, dbo.udfGetFirstFiveNumbers(col1) as result
from DummyTable
结果:-