if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[fn_Ord]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[fn_Ord]
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS OFF
GO
CREATE function fn_Ord(@str varchar(4000),@flg varchar(10))
returns varchar(4000)
as
begin
declare @a table (a nvarchar(2))
declare @s varchar(4000)
declare @i int
set @s=''
set @i=1
while @i<len(@str)
begin
insert @a select substring(@str,@i,1)
set @i=@i+1
end
if @flg='asc'
select @s=@s+a from @a order by a
else
select @s=@s+a from @a order by a desc
return @s
/*
select dbo.fn_Ord('gdadfasdf','asc')
*/
end
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
本文介绍了一个SQL函数fn_Ord的创建过程及其使用方法。该函数接收两个参数,字符串和排序标志,并返回按指定顺序排列的字符组成的字符串。通过示例展示了如何创建和调用此函数。
1828

被折叠的 条评论
为什么被折叠?



