日期:2014-05-16 浏览次数:20698 次
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/*
创建一函数,用来判断一个数字@n是否为3位数,并且根据@type来返回这个3位数的百位数或十位数或个位数
B:百位数
S:十位数
G:个位数
其他情况则返回0
比如uf1(234,'b')返回2
*/
CREATE FUNCTION uf1(@n int,@type varchar(2))
RETURNS int
AS
BEGIN
if @n >0 and @n <1000
begin
case
when @type = 'B' or @type = 'b' THEN return left(cast(@n as varchar(4)),1)
when @type = 'S' or @type = 's' THEN return substring(cast(@n as varchar(4)),2,1)
when @type = 'G' or @type = 'g' THEN return right(cast(@n as varchar(4)),1)
else return 0
end
end
else
return 0
END
GO
CREATE FUNCTION dbo.uf1 ( @n INT, @type VARCHAR(2) )
RETURNS INT
AS
BEGIN
DECLARE @i INT = 0
IF @n > 0
AND @n < 1000
BEGIN
SELECT @i = CASE UPPER(@type)
WHEN 'B'
THEN CONVERT(INT, LEFT(CAST(@n AS VARCHAR(4)),
1))
WHEN 'S'
THEN CONVERT(INT, SUBSTRING(CAST(@n AS VARCHAR(4)),
&nb