--获得每天的日期流水(20160507001)
CREATE FUNCTION [dbo].[f_GetDateFlowNo]
(
---日期流水(20160507001)
@dateFlowNo VARCHAR(30) ,
--流水长度len(0001)
@noLen BIGINT ,
@showtype VARCHAR(8) --yymmdd,yyyymmdd,yymm,yyyymm
)
RETURNS VARCHAR(30)
AS
BEGIN
--定义当天字符串
DECLARE @DateStr VARCHAR(10);
--定义序号字符串
DECLARE @No VARCHAR(20);
--得到序号No字符串
DECLARE @NoStr VARCHAR(20)= ‘00000000000000000000‘;
SET @showtype=LOWER(@showtype) ;
SET @DateStr = CASE WHEN @showtype = ‘yyyymmdd‘
THEN CONVERT(VARCHAR(8), GETDATE(), 112)
WHEN @showtype = ‘yymmdd‘
THEN CONVERT(VARCHAR(6), GETDATE(), 12)
WHEN @showtype = ‘yymm‘
THEN CONVERT(VARCHAR(4), GETDATE(), 12)
WHEN @showtype = ‘yyyymm‘
THEN CONVERT(VARCHAR(6), GETDATE(), 112)
ELSE CONVERT(VARCHAR(8), GETDATE(), 112)
END
--判断传入的字符串是否为‘‘
IF ISNULL(@dateFlowNo, ‘‘) = ‘‘
BEGIN
SET @dateFlowNo = @DateStr + RIGHT(@NoStr + ‘1‘, @noLen);
END;
ELSE
BEGIN
--去到当前序号+1
SET @No = CAST(CAST(REPLACE(@dateFlowNo, @DateStr, ‘‘) AS BIGINT)
+ 1 AS VARCHAR(20));
--判断长度是否超过序号最大字符串(不超过补)
IF LEN(@No) < @noLen
BEGIN
SET @No = RIGHT(@NoStr + @No, @noLen);
END;
--返回值赋值
SET @dateFlowNo = @DateStr + @No;
END;
RETURN @dateFlowNo;
END;