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