CREATE FUNCTION [dbo].[f_AddYears]
(
---增加年
@currentDay DATETIME ,
@years INT
)
RETURNS DATETIME
AS
BEGIN
RETURN DATEADD(year,@years,@currentDay)
END
--增加月
CREATE FUNCTION [dbo].[f_AddMonths]
(
---增加月
@currentDay DATETIME ,
@months INT
)
RETURNS DATETIME
AS
BEGIN
RETURN DATEADD(month,@months,@currentDay)
END
--增加日
CREATE FUNCTION [dbo].[f_AddDays]
(
---增加日
@currentDay DATETIME ,
@days INT
)
RETURNS DATETIME
AS
BEGIN
RETURN DATEADD(day,@days,@currentDay)
END
----增加秒数
CREATE FUNCTION [dbo].[f_AddSeconds]
(
---增加秒数
@currentDay DATETIME ,
@seconds INT
)
RETURNS DATETIME
AS
BEGIN
RETURN DATEADD(ss,@seconds,@currentDay)
END
---返回日期部分
CREATE FUNCTION [dbo].[f_getDate] ( ---返回日期部分
@currentDay DATETIME )
RETURNS DATETIME
AS
BEGIN
RETURN CONVERT(DATETIME,CONVERT(CHAR(08),@currentDay,112))
END
--返回日期的结束时间
CREATE FUNCTION [dbo].[f_getEndTime] ( ---返回日期的结束时间
@currentDay DATETIME )
RETURNS DATETIME
AS
BEGIN
RETURN dbo.[f_AddSeconds](dbo.f_getDate(@currentDay),24*60*60-1)
END
--字符串拆分
CREATE FUNCTION [dbo].[f_split]
(
@string VARCHAR(MAX) ,
@separator CHAR
)
RETURNS @return TABLE ( value VARCHAR(200) )
AS
BEGIN
DECLARE @len INT
SET @len = LEN(@string)
IF ( SUBSTRING(@string, @len, 1) <> @separator )
SET @string = @string + @separator
DECLARE @strtemp VARCHAR(200)
DECLARE @index1 INT
DECLARE @index2 INT
SET @index1 = 0
SET @index2 = CHARINDEX(@separator, @string, 1)
WHILE @index2 <> 0
BEGIN
SET @strtemp = SUBSTRING(@string, @index1 + 1,
@index2 - @index1 - 1)
INSERT @return
( [value] )
VALUES ( @strtemp )
SET @index1 = @index2
SET @index2 = CHARINDEX(@separator, @string, @index1 + 1)
END
RETURN
END
--MD5加密
CREATE FUNCTION [dbo].[f_md5]
(
@string VARCHAR(MAX), --加密字符串
@WhatBit INT --位数
)
RETURNS VARCHAR(50)
AS
BEGIN
DECLARE @order VARCHAR(50)
SET @order = RIGHT(sys.fn_VarBinToHexStr(hashbytes(‘MD5‘, CAST(@string AS VARCHAR(MAX)))), @WhatBit);
RETURN (@order)--分割的后字符串
END
---获取后字符串
CREATE FUNCTION [dbo].[f_splitString]
(
@string VARCHAR(MAX) ,--要分割的字符串
@splitChar VARCHAR(10)--分割字符
)
RETURNS VARCHAR(20)
AS
BEGIN
DECLARE @order VARCHAR(20)
SET @order = SUBSTRING(@string,
CHARINDEX(@splitChar, @string) + LEN(@splitChar),
LEN(@string) - CHARINDEX(@splitChar, @string)
- ( LEN(@splitChar) - 1 )) ;
RETURN (@order)--分割的后字符串
END
--转义字符转换
CREATE FUNCTION [dbo].[fun_ReplaceKey] ( @key VARCHAR(MAX) )
RETURNS VARCHAR(MAX)
AS
BEGIN
SET @key = REPLACE(@key, ‘<‘, ‘<‘)
SET @key = REPLACE(@key, ‘<=‘, ‘<=‘)
SET @key = REPLACE(@key, ‘>‘, ‘>‘)
SET @key = REPLACE(@key, ‘>=‘, ‘>=‘)
SET @key = REPLACE(@key, ‘&‘, ‘‘‘‘)
SET @key = REPLACE(@key, ‘"‘, ‘\"‘)
SET @key = REPLACE(@key, ‘‘, ‘\r\n‘)
RETURN @key
END
---获取分割字符串个数
CREATE FUNCTION [dbo].[GetSplitLength]
(
@String NVARCHAR(MAX) , --要分割的字符串
@Split NVARCHAR(10) --分隔符号
)
RETURNS INT
AS
BEGIN
DECLARE @location INT
DECLARE @start INT
DECLARE @length INT
SET @String = LTRIM(RTRIM(@String))
SET @location = CHARINDEX(@split, @String)
SET @length = 1
WHILE @location <> 0
BEGIN
SET @start = @location + 1
SET @location = CHARINDEX(@split, @String, @start)
SET @length = @length + 1
END
RETURN @length
END
--获取分割字符串的定位字符
CREATE FUNCTION [dbo].[GetSplitOfIndex]
(
@String NVARCHAR(MAX) , --要分割的字符串
@split NVARCHAR(10) , --分隔符号
@index INT --取第几个元素
)
RETURNS NVARCHAR(1024)
AS
BEGIN
DECLARE @location INT
DECLARE @start INT
DECLARE @next INT
DECLARE @seed INT
SET @String = LTRIM(RTRIM(@String))
SET @start = 1
SET @next = 1
SET @seed = LEN(@split)
SET @location = CHARINDEX(@split, @String)
WHILE @location <> 0
AND @index > @next
BEGIN
SET @start = @location + @seed
SET @location = CHARINDEX(@split, @String, @start)
SET @next = @next + 1
END
IF @location = 0
SELECT @location = LEN(@String) + 1
RETURN SUBSTRING(@String,@start,@[email protected])
END
原文地址:https://www.cnblogs.com/zengtianli/p/8625290.html