|
34、字符串转成16进制函数
/****************************
字符串转成16进制
作者:不得闲
QQ: 75492895
Email: appleak46@yahoo.com.cn
****************************/
--创建函数(suiyunonghen(不得闲))
Create Function VarCharToHex(@Str Varchar(400))
returns varchar(800)
as
begin
declare @i int,@Asi int,@ModS int,@res varchar(800),@Len int,@Cres varchar(4),@tempstr varbinary(400)
select @i = 1,@res='',@len=datalength(@str),@tempStr = Convert(varbinary,@str)
while @i 9) then Char(Ascii('A')+@Mods-10)+@Cres else Cast(@Mods as varchar(4)) + @Cres end,
@Asi = @Asi/16
end
Select @res = @res + @Cres,@tempStr = substring(@tempStr,2,@len-1),@i = @i+1
end
return @res
end
go
--测试示例
select dbo.VarCharToHex('叶子')
--运行结果
/*
D2B6D7D3
*/
35、去掉字段中多个带区号电话号码前面的区号
--原帖地址:http://blog.csdn.net/htl258/archive/2010/04/28/5540795.aspx
---------------------------------------------------------------------
-- Author : htl258(Tony)
-- Date : 2010-04-28 23:22:15
-- Version:Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86)
-- Jul 9 2008 14:43:34
-- Copyright (c) 1988-2008 Microsoft Corporation
-- Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
-- Blog : http://blog.csdn.net/htl258
-- Subject: 应用实例:去掉字段中多个带区号电话号码前面的区号
--------------------------------------------------------------------
--需求贴:http://topic.csdn.net/u/20100428/20/f2572998-099c-463a-a530-707a40606c9c.html?53227
--> 生成测试数据表:tb
IF NOT OBJECT_ID('[tb]') IS NULL
DROP TABLE [tb]
GO
CREATE TABLE [tb]([name] NVARCHAR(10),[phone] NVARCHAR(40))
INSERT [tb]
SELECT 'a',N'010-12345678/0571-86919111' UNION ALL
SELECT 'b',N'020-23950423/0756-34972654/023-89732456'
GO
--SELECT * FROM [tb]
-->SQL查询如下:
--1.创建自定义函数
IF NOT OBJECT_ID('[f_getphone]') IS NULL
DROP FUNCTION [f_getphone]
GO
CREATE FUNCTION f_getphone(@s varchar(200))
RETURNS varchar(200)
AS
BEGIN
SET @s=@s+'/'
DECLARE @r VARCHAR(200)
WHILE CHARINDEX('/',@s)>0
SELECT @r=ISNULL(@r+'/','')
+LEFT(STUFF(@s,1,CHARINDEX('-',@s),'')
,CHARINDEX('/',@s)-CHARINDEX('-',@s)-1)
,@s=STUFF(@s,1,CHARINDEX('/',@s),'')
RETURN @r
END
GO
--2.查询
SELECT [name],dbo.f_getphone(phone) 'phone' FROM TB
/*
name phone
---------- ------------------------------------
a 12345678/86919111
b 23950423/34972654/89732456
(2 行受影响)
*/
--本文来自CSDN博客
--转载请标明出处:--http://blog.csdn.net/htl258/archive/2010/04/28/5540795.aspx
36、SQL2000/2005字符串拆分为列表通用函数
-- 原帖地址:http://blog.csdn.net/htl258/archive/2010/04/28/5537235.aspx
------------------------------------------------------------------
-- Author : htl258(Tony)
-- Date : 2010-04-28 02:00:28
-- Version:Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86)
-- Jul 9 2008 14:43:38
-- Copyright (c) 1988-2008 Microsoft Corporation
-- Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
-- Blog : http://blog.csdn.net/htl258
-- Subject: SQL2000/2005字符串拆分为列表通用函数
-------------------------------------------------------------------
--SQL2000/2005字符串拆分为列表通用函数
IF OBJECT_ID('f_getstr') IS NOT NULL
DROP FUNCTION f_getstr
GO
CREATE FUNCTION f_getstr(
@s NVARCHAR(4000), --待分拆的字符串
@flag NVARCHAR(10)='' --数据分隔符
)RETURNS @r TABLE(col NVARCHAR(1000))
AS
BEGIN
IF ISNULL(@flag,'')='' AND LEN(ISNULL(@flag,'')+'a')=1
INSERT @r
SELECT SUBSTRING(@s,number+1,1)
FROM master..spt_values
WHERE TYPE='p' and number /*
id col
----------- -----------
aa
bb
aaa
bbb
ccc
(5 行受影响)
*/
--本文来自CSDN博客
--转载请标明出处:
--http://blog.csdn.net/htl258/archive/2010/04/28/5537235.aspx
37、求字符串中汉字的个数
一、分解字符串法
首先创建这个函数:
/*将字符串分解*/
create function [dbo].[SplitChar]
(
@str_One Nvarchar(100)
)
returns @Result table (col nvarchar(1))
AS
BEGIN
declare @number_One int
select @number_One=1
while @number_One127
return @number_One
END
/*使用示例
select dbo.[ChineseCountOne] ('China中国Beijing北京Olympics奥林匹克')
*/
二、字符字节求差法
首先创建这个函数:
create function [dbo].[Chinesecount_Two]
(
@Str_One varchar(200)
)
RETURNS int AS
BEGIN
declare @number_One int
set @number_One=(datalength(@Str_One)-len(@Str_One))
return @number_One
END
/*使用示例
DECLARE @sql_one varchar(200)
SET @sql_one='China中国Beijing北京Olympics奥林匹克'
DECLARE @sql_two nvarchar(200)
SET @sql_two='China中国Beijing北京Olympics奥林匹克'
select dbo.[Chinesecount_Two] (@sql_one) '个数one' ,
dbo.[Chinesecount_Two] (@sql_two) '个数two'
--此例说明此方法不受数据类型限制
*/
38、得到条形码的校验位函数
二、SQL实现:
go
-- =============================================
-- Author:
-- Create date:
-- Description:
-- =============================================
create function [dbo].[Get_CheckCode]
(
@ActiveCode varchar(12)
)
returns varchar(13)
as
begin
declare @codelength int
set @codelength= len(@ActiveCode)
declare @curnum int;set @curnum=0
declare @temp1 int;set @temp1=0
declare @temp2 int;set @temp2=0
declare @locatnum int;set @locatnum=0
declare @code13 int
declare @i int;set @i=0
while(@i 0 )
BEGIN
IF ( SUBSTRING(REVERSE(@numstr), LEN(@numstr) - @i + 1, 1) = '1' )
SELECT @j = @j + '+2^' + CAST (@i-1 AS VARCHAR(10))
SET @i = @i - 1
END
return (CAST(@numc AS VARCHAR(100)) + '=' + STUFF(@j, 1, 1, ''))
END
go
--测试示例
select dbo.GetSumSequence(12)
select dbo.GetSumSequence(65)
select dbo.GetSumSequence(892)
select dbo.GetSumSequence(1919191)
--运行结果
/*
12=2^3+2^2
65=2^6+2^0
892=2^9+2^8+2^6+2^5+2^4+2^3+2^2
1919191=2^20+2^19+2^18+2^16+2^14+2^11+2^7+2^6+2^4+2^2+2^1+2^0
*/
41、SQL位移运算函数
-- =============================================
-- Author:
-- Create date:
-- Description:
-- =============================================
go
--创建函数
create function displacement(@n as bigint,@m as varchar(3))
returns int
as
begin
declare @maco varchar(50);set @maco=''
declare @i int
declare @x int
declare @s int
while (@n0)
begin
set @maco=@maco+convert(char(1),@n%2)
set @n=@n/2
end
set @maco=reverse(@maco)
set @maco=RIGHT('0000'+@maco,4)
set @s=LEN(@maco)
set @i=convert(int,RIGHT(@m,1))
set @x=1
if LEFT(@m,2)=''
begin
while(@x0)
begin
if SUBSTRING(@maco,LEN(@maco)-@i+1,1)='1'
begin
select @s=@s+POWER(2,convert(float,@i-1))
end
select @i=@i-1
end
return @s
end
--测试示例
select dbo.displacement(1,'1')
select dbo.displacement(12,'>>3')
--运行结果
/*
*/
42、得到汉字笔画函数
--===============================================
--功能:汉字笔画函数
--说明:以单个汉字汉字为参数返回每一个汉字的笔画数
--作者: J9988 --*/
--===============================================
create function [dbo].[fun_getbh](@char nchar(2))
returns int
as
begin
return(
case when unicode(@char) between 19968 and 40869 then(
select top 1 id from(
select id=1,ch=N'亅' union all select 2,N'阝'
union all select 3,N'马' union all select 4,N'风'
union all select 5,N'龙' union all select 6,N'齐'
union all select 7,N'龟' union all select 8,N'齿'
union all select 9,N'鸩' union all select 10,N'龀'
union all select 11,N'龛' union all select 12,N'龂'
union all select 13,N'龆' union all select 14,N'龈'
union all select 15,N'龊' union all select 16,N'龍'
union all select 17,N'龠' union all select 18,N'龎'
union all select 19,N'龐' union all select 20,N'龑'
union all select 21,N'龡' union all select 22,N'龢'
union all select 23,N'龝' union all select 24,N'齹'
union all select 25,N'龣' union all select 26,N'龥'
union all select 27,N'齈' union all select 28,N'龞'
union all select 29,N'麷' union all select 30,N'鸞'
union all select 31,N'麣' union all select 32,N'龖'
union all select 33,N'龗' union all select 35,N'齾'
union all select 36,N'齉' union all select 39,N'靐'
union all select 64,N'龘'
)a where ch>=@char collate Chinese_PRC_Stroke_CS_AS_KS_WS
order by id ASC) else 0 end)
end
--测试示例
select dbo.fun_getbh('晓')
--运行结果
/*
*/
43、SQL数字转英文函数
--晴天兄(qianjin036a)的发帖地址:
--http://topic.csdn.net/u/20080614/12/d26adea8-ac05-4b06-8b8a-f46a4b564e3b.html
-- 数字转英文
-- =============================================
-- Author: qianjin036a
-- Create date:06/14/2008 02:27:17
-- Description:Arabic numerals to English
-- =============================================
go
--创建函数
CREATE FUNCTION Digit2English
(
@arabia decimal(38,17)
)
RETURNS varchar(1000)
AS
BEGIN
declare @atoe table(a int,e varchar(10))
insert into @atoe select 0,'zero' union all select 1,'one'
union all select 2,'two' union all select 3,'three'
union all select 4,'four' union all select 5,'five'
union all select 6,'six' union all select 7,'seven'
union all select 8,'eight' union all select 9,'nine'
declare @integer bigint,@trillion int,@billion int,@million int,@thousand int,@hundred int,@english varchar(1000)
select @integer=@arabia,@english=''
select @trillion=@integer % 1000000000000000/1000000000000,@billion=@integer % 1000000000000/1000000000,
@million=@integer % 1000000000/1000000,@thousand=(@integer % 1000000)/1000,@hundred=(@integer % 1000)
if @trillion>0
set @english=@english + dbo.ThreeDigit(@trillion) + 'trillion '
if @billion>0
set @english=@english + dbo.ThreeDigit(@billion) + 'billion '
if @million>0
set @english=@english + dbo.ThreeDigit(@million) + 'million '
if @thousand>0
set @english=@english + dbo.ThreeDigit(@thousand) + 'thousand '
if @hundred>0
set @english=@english + dbo.ThreeDigit(@hundred)
if @english=''
set @english='zero '
if @arabia-@integer>0.000000000
begin
declare @decimal decimal(18,17)
select @english=@english+'point ',@decimal=@arabia-@integer
while @decimal>0.0
begin
select @english=@english+e+' ' from @atoe where cast(@decimal*10 as int)=a
set @decimal=@decimal*10-cast(@decimal*10 as int)
end
end
return @english
END
GO
-- =============================================
-- Author: qianjin036a
-- Create date: 06/14/2008 02:27:17
-- Description: Three Digit Arabic numerals to English
-- =============================================
CREATE FUNCTION ThreeDigit
(
@integer int
)
RETURNS varchar(100)
WITH EXECUTE AS CALLER
AS
BEGIN
declare @atoe table(a int,e varchar(10))
insert into @atoe select 0,'zero' union all select 1,'one'
union all select 2,'two' union all select 3,'three'
union all select 4,'four' union all select 5,'five'
union all select 6,'six' union all select 7,'seven'
union all select 8,'eight' union all select 9,'nine'
union all select 10,'ten' union all select 11,'eleven'
union all select 12,'twelve' union all select 13,'thirteen'
union all select 14,'fourteen' union all select 15,'fifteen'
union all select 16,'sixteen' union all select 17,'seventeen'
union all select 18,'eighteen' union all select 19,'nineteen'
union all select 20,'twenty' union all select 30,'thirty'
union all select 40,'forty' union all select 50,'fifty'
union all select 60,'sixty' union all select 70,'severty'
union all select 80,'eighty' union all select 90,'ninety'
declare @english varchar(100)
set @english=''
if @integer>99
begin
select @english=e+' hundred ' from @atoe where @integer/100=a
set @integer=@integer % 100
if @integer>0
set @english=@english+'and '
end
if @integer0
select @english=@english+e+' ' from @atoe where @integer=a
if @integer>20
begin
select @english=@english+e+' ' from @atoe where @integer/10*10=a
set @integer=@integer % 10
if @integer>0
select @english=@english+e+' ' from @atoe where @integer=a
end
RETURN @english
END
GO
select dbo.digit2english(123456789987654.321)
union all select dbo.digit2english(120045080045054.8412)
union all select dbo.digit2english(0.0102541)
go
/*
---------------------------------------------------------------------
one hundred and twenty three trillion four hundred and fifty six billion seven hundred and eighty nine million nine hundred and eighty seven thousand six hundred and fifty four point three two one
one hundred and twenty trillion forty five billion eighty million forty five thousand fifty four point eight four one two
zero point zero one zero two five four one
*/
44、全角半角转换函数
--(此函数部分思路参考了CSDN上大力的转换函数)
--邹建2005.01(引用请保留此信息)--*/
go
--创建函数
create function SBC2DBC
(
@str nvarchar(4000), --要转换的字符串
@flag bit --转换标志,0转换成半角,1转换成全角
)
returns nvarchar(4000)
as
begin
declare @pat nvarchar(8),@step int,@i int,@spc int
if @flag=0
select @pat=N'%[!-~]%',@step=-65248,@str=replace(@str,N' ',N' ')
else
select @pat=N'%[!-~]%',@step=65248,@str=replace(@str,N' ',N' ')
set @i=patindex(@pat collate latin1_general_bin,@str)
while @i>0
select @str=replace(@str,substring(@str,@i,1),nchar(unicode(substring(@str,@i,1))+@step))
,@i=patindex(@pat collate latin1_general_bin,@str)
return(@str)
end
--测试示例
select dbo.SBC2DBC('~~~~ca!b',1)
--运行结果
/*
~~~~ca!b
*/
--附半角全角表
/*
ASCII 全角字符 Unicode 半角字符 Unicode
0x20 " "空格U+3000 " "空格U+0020
0x21 !U+ff01 ! U+0021
0x22 "U+ff02 " U+0022
0x23 #U+ff03 # U+0023
0x24 $U+ff04 $ U+0024
0x25 %U+ff05 % U+0025
0x26 &U+ff06 & U+0026
0x27 'U+ff07 ' U+0027
0x28 (U+ff08 ( U+0028
0x29 )U+ff09 ) U+0029
0x2a *U+ff0a * U+002a
0x2b +U+ff0b + U+002b
0x2c ,U+ff0c , U+002c
0x2d -U+ff0d - U+002d
0x2e .U+ff0e . U+002e
0x2f /U+ff0f / U+002f
0x30 0U+ff10 0 U+0030
0x31 1U+ff11 1 U+0031
0x32 2U+ff12 2 U+0032
0x33 3U+ff13 3 U+0033
0x34 4U+ff14 4 U+0034
0x35 5U+ff15 5 U+0035
0x36 6U+ff16 6 U+0036
0x37 7U+ff17 7 U+0037
0x38 8U+ff18 8 U+0038
0x39 9U+ff19 9 U+0039
0x3a :U+ff1a : U+003a
0x3b ;U+ff1b ; U+003b
0x3c <U+ff1c < U+003c
0x3d =U+ff1d = U+003d
0x3e >U+ff1e > U+003e
0x3f ?U+ff1f ? U+003f
0x40 @U+ff20 @ U+0040
0x41 AU+ff21 A U+0041
0x42 BU+ff22 B U+0042
0x43 CU+ff23 C U+0043
0x44 DU+ff24 D U+0044
0x45 EU+ff25 E U+0045
0x46 FU+ff26 F U+0046
0x47 GU+ff27 G U+0047
0x48 HU+ff28 H U+0048
0x49 IU+ff29 I U+0049
0x4a JU+ff2a J U+004a
0x4b KU+ff2b K U+004b
0x4c LU+ff2c L U+004c
0x4d MU+ff2d M U+004d
0x4e NU+ff2e N U+004e
0x4f OU+ff2f O U+004f
0x50 PU+ff30 P U+0050
0x51 QU+ff31 Q U+0051
0x52 RU+ff32 R U+0052
0x53 SU+ff33 S U+0053
0x54 TU+ff34 T U+0054
0x55 UU+ff35 U U+0055
0x56 VU+ff36 V U+0056
0x57 WU+ff37 W U+0057
0x58 XU+ff38 X U+0058
0x59 YU+ff39 Y U+0059
0x5a ZU+ff3a Z U+005a
0x5b [U+ff3b [ U+005b
0x5c \U+ff3c / U+005c
0x5d ]U+ff3d ] U+005d
0x5e ^U+ff3e ^ U+005e
0x5f _U+ff3f _ U+005f
0x60 `U+ff40 ` U+0060
0x61 aU+ff41 a U+0061
0x62 bU+ff42 b U+0062
0x63 cU+ff43 c U+0063
0x64 dU+ff44 d U+0064
0x65 eU+ff45 e U+0065
0x66 fU+ff46 f U+0066
0x67 gU+ff47 g U+0067
0x68 hU+ff48 h U+0068
0x69 iU+ff49 i U+0069
0x6a jU+ff4a j U+006a
0x6b kU+ff4b k U+006b
0x6c lU+ff4c l U+006c
0x6d mU+ff4d m U+006d
0x6e nU+ff4e n U+006e
0x6f oU+ff4f o U+006f
0x70 pU+ff50 p U+0070
0x71 qU+ff51 q U+0071
0x72 rU+ff52 r U+0072
0x73 sU+ff53 s U+0073
0x74 tU+ff54 t U+0074
0x75 uU+ff55 u U+0075
0x76 vU+ff56 v U+0076
0x77 wU+ff57 w U+0077
0x78 xU+ff58 x U+0078
0x79 yU+ff59 y U+0079
0x7a zU+ff5a z U+007a
0x7b {U+ff5b { U+007b
0x7c |U+ff5c | U+007c
0x7d }U+ff5d } U+007d
0x7e ~U+ff5e ~ U+007e
*/
45、返回两个时间范围内的一个随机时间
/******************************
* 函数名:RandDateTime
* 作用: 返回两个时间范围内的一个随机时间
* Author: 兰习刚
* Date: 2009-11-30
*******************************/
go
--创建函数
create Function RandDateTime
(
@RandNum Decimal(38,18),--0-1之际随机数值建议Rand()
@StartTime DateTime, --第一个时间
@EndTime DateTime --第二个时间
)
Returns DateTime
As
Begin
Declare @HourDiff Decimal(38,18) --两个时间之间的小时差值
Declare @MsPartDiff Decimal(38,18) --毫秒部分的差值
Declare @SmallDate DateTime
Declare @ReturnDateTime DateTime
/*取各部分差值*/
Set @HourDiff = DateDiff(hh,@StartTime,@EndTime)
Set @MsPartDiff = Abs(DateDiff(ms,DateAdd(hh,@HourDiff,@StartTime),@EndTime))
Select @SmallDate=(Case When @HourDiff>0 Then @StartTime Else @EndTime End) --取较小的时间
Set @HourDiff = Abs(@HourDiff)
ActionLable:
Declare @HourDecimal Decimal(38,18) --小时的小数部分
Declare @HourString varchar(200)
Set @HourDiff = @HourDiff * @RandNum
Set @HourString = CONVERT(VARCHAR(200),@HourDiff)
Set @HourString = SubString(@HourString,CharIndex('.',@HourString)+1,Len(@HourString))
Set @HourString = '0.' + @HourString
Set @HourDecimal = Convert(Decimal(38,18),@HourString) --获得小时的小数部分
Set @MsPartDiff = (@MsPartDiff + @HourDecimal * 3600*1000) * @RandNum
/*毫秒差值
由于之前@MsPartDiff是两个时间小时之后的毫秒差值
@HourDecimal * 3600*1000 有小时的小数部分的毫秒差值不会大于小时
毫秒不会溢出
*/
Set @ReturnDateTime = DateAdd(hh,@HourDiff,@SmallDate)
Set @ReturnDateTime = DateAdd(ms,@MsPartDiff,@ReturnDateTime)
Return @ReturnDateTime
End
--测试示例
select dbo.RandDateTime(Rand(),'2011-03-21 00:00:00.000','2011-03-21 23:59:00.000')
go 10
--运行结果
/*
-----------------------
2011-03-21 16:44:58.990
(1 row(s) affected)
-----------------------
2011-03-21 00:00:33.313
(1 row(s) affected)
-----------------------
2011-03-21 15:04:58.777
(1 row(s) affected)
-----------------------
2011-03-21 06:32:21.347
(1 row(s) affected)
-----------------------
2011-03-21 15:11:51.047
(1 row(s) affected)
-----------------------
2011-03-21 14:39:23.597
(1 row(s) affected)
-----------------------
2011-03-21 07:24:17.247
(1 row(s) affected)
-----------------------
2011-03-21 06:15:49.653
(1 row(s) affected)
-----------------------
2011-03-21 02:06:14.757
(1 row(s) affected)
-----------------------
2011-03-21 10:49:18.370
(1 row(s) affected)
*/
46、获取元素个数的函数
go
-- 创建函数(作者:csdn邹建)
create function getstrarrlength (@str varchar(8000))
returns int
as
begin
declare @int_return int
declare @start int
declare @next int
declare @location int
select @str =','+ @str +','
select @str=replace(@str,',,',',')
select @start =1
select @next =1
select @location = charindex(',',@str,@start)
while (@location 0)
begin
select @start = @location +1
select @location = charindex(',',@str,@start)
select @next =@next +1
end
select @int_return = @next-2
return @int_return
end
-- 测试示例
SELECT [dbo].[getstrarrlength]('1,2,3,4,a,b,c,d')
--运行结果
/*
*/
/*
说明:
我开始考虑直接看逗号的个数,用replace替换逗号,求长度差就可以了,但是这里这个函数两个逗号相邻做了处理。
*/
47、获取指定索引的值的函数
go
--创建函数(作者:csdn邹建)
create function getstrofindex (@str varchar(8000),@index int =0)
returns varchar(8000)
as
begin
declare @str_return varchar(8000)
declare @start int
declare @next int
declare @location int
select @start =1
select @next =1 --如果习惯从开始则select @next =0
select @location = charindex(',',@str,@start)
while (@location 0 and @index > @next )
begin
select @start = @location +1
select @location = charindex(',',@str,@start)
select @next =@next +1
end
if @location =0 select @location =len(@str)+1 --如果是因为没有逗号退出,则认为逗号在字符串后
select @str_return = substring(@str,@start,@location -@start) --@start肯定是逗号之后的位置或者就是初始值
if (@index @next ) select @str_return = '' --如果二者不相等,则是因为逗号太少,或者@index小于@next的初始值。
return @str_return
end
--测试示例
SELECT [dbo].[getstrofindex]('1,2,3,4,a,b,c,d',4)
--运行结果
/*
*/
--备注:类似功能的函数happyflystone (无枪狙击手)也写过一个,参数上做了扩展,可以定义分隔符了,在【叶子函数分享十六】我曾经发过。
48、根据年得到所有星期日的日期
go
--创建函数
create function GetWeekDays(@year int)
returns @t table (星期天varchar(20))
as
begin
insert @t
select substring(convert(varchar,dateadd(day,x,col),120),1,10) from
( select cast(cast(@year as varchar(4))+'-1-1' as datetime) as col )a cross join
( select top 365 b8.i+b7.i + b6.i + b5.i + b4.i +b3.i +b2.i + b1.i + b0.i x
from(select 0 i union all select 1) b0
cross join(select 0 i union all select 2) b1
cross join(select 0 i union all select 4) b2
cross join(select 0 i union all select 8) b3
cross join(select 0 i union all select 16) b4
cross join(select 0 i union all select 32) b5
cross join(select 0 i union all select 64) b6
cross join(select 0 i union all select 128) b7
cross join(select 0 i union all select 256) b8
order by 1 )b where datepart(dw,dateadd(day,x,col))=1
return
end
--测试示例
select * from dbo.GetWeekDays(2011)
--运行结果
/*
星期天
--------------------
2011-01-02
2011-01-09
2011-01-16
2011-01-23
2011-01-30
2011-02-06
2011-02-13
2011-02-20
2011-02-27
2011-03-06
2011-03-13
2011-03-20
2011-03-27
2011-04-03
2011-04-10
2011-04-17
2011-04-24
2011-05-01
2011-05-08
2011-05-15
2011-05-22
2011-05-29
2011-06-05
2011-06-12
2011-06-19
2011-06-26
2011-07-03
2011-07-10
2011-07-17
2011-07-24
2011-07-31
2011-08-07
2011-08-14
2011-08-21
2011-08-28
2011-09-04
2011-09-11
2011-09-18
2011-09-25
2011-10-02
2011-10-09
2011-10-16
2011-10-23
2011-10-30
2011-11-06
2011-11-13
2011-11-20
2011-11-27
2011-12-04
2011-12-11
2011-12-18
2011-12-25
(52 row(s) affected)
*/
49、生成两个时间之间的所有日期
--改写liangCK的部分代码为函数
--创建函数
create function generateTime
(
@begin_date datetime,
@end_date datetime
)
returns @t table(date datetime)
as
begin
with maco as
(
select @begin_date AS date
union all
select date+1 from maco
where date+1 0
begin
set @Sub = left(@Str1, charindex(@Split, @Str1, @Len + 1) + @Len - 1)
if charindex(@Sub, @Str2) = 0 return(0)
while charindex(@Sub, @Str1) > 0 set @Str1 = replace(@Str1, @Sub, ',')
while charindex(@Sub, @Str2) > 0 set @Str2 = replace(@Str2, @Sub, ',')
if len(@Str1)len(@Str2) return(0)
end
return(1)
END
GO
--测试示例
select dbo.fn_CompareString('a,b,c', 'b,c,a', ',')
select dbo.fn_CompareString('a,b,c', 'b,c,c,a', ',')
--运行结果
/*
*/
51、在SQL SERVER中实现RSA加解密函数(第一版)
/***************************************************
作者:herowang(让你望见影子的墙)
日期:2010.1.1
注: 转载请保留此信息
更多内容,请访问我的博客:blog.csdn.net/herowang
****************************************************/
一、RSA算法原理
RSA算法非常简单,概述如下:
找两素数p和q
取n=p*q
取t=(p-1)*(q-1)
取任何一个数e,要求满足e from tb
4、如果选取的数字比较大,那么在进行加密的时候,生成的进制密文最好使用个字节或者更多。
52、在SQL SERVER中实现RSA加解密函数(第二版)
/***************************************************
作者:herowang(让你望见影子的墙)
日期:2010.1.5
注: 转载请保留此信息
更多内容,请访问我的博客:blog.csdn.net/herowang
****************************************************/
/*
本次修改增加了unicode的支持,但是加密后依然显示为进制数据,因为进行RSA加密后所得到的unicode编码是无法显示的,所以密文依然采用进制数据显示。
需要特别注意:如果要对中文进行加密,那么所选取的两个素数要比较大,两个素数的成绩最好要大于,即大于unicode的最大编码值
另外修改了第一个版本的部分函数名称
*/
在SQL SERVER中实现RSA加密算法
--判断是否为素数
if object_id('f_primeNumTest') is not null
drop function f_primeNumTest
go
create function [dbo].[f_primeNumTest]
(@p int)
returns bit
begin
declare @flg bit,@i int
select @flg=1, @i=2
while @i go
insert into tb values(dbo.f_RSAEncry('中国人',779,1163,59))
insert into tb values(dbo.f_RSAEncry('Chinese',779,1163,59))
select * from tb
--运行结果
/*
id col
----------- ---------------------------------------------
00359B00E6E000EAF5
01075300931B0010A4007EDC004B340074A6004B34
*/
select * ,解密后=dbo.f_RSADecry(col,35039,1163,59)from tb
--测试示例
/*
id col 解密后
----------- ------------------------------------------- -----------
00359B00E6E000EAF5 中国人
01075300931B0010A4007EDC004B340074A6004B34 Chinese
*/
53、输出指定格式的数据列
-- =============================================
-- Author: maco_wang
-- Create date: 2011-03-30
-- Description:
-- 需求贴:http://topic.csdn.net/u/20110330/10/dd155c82-e156-49df-9b5a-65bdbb0bf3ab.html
-- =============================================
前记:
Csdn上看到一帖子,要求如下:
编程一个函数实现功能,给出n,打印1-n,例如1 22 33 444 555 666 7777 8888 9999 10101010
就是要
1个1位: 1
2个2位: 22 33
3个3位: 444 555 666
4个4位: 7777 8888 9999 10101010
....
虽然是.NET技术-ASP.NET板块的帖子,但是思路都是一样的,用SQL写了一下:
create function PrintN(@n int)
returns @table table (id bigint)
as
begin
declare @i bigint;set @i=1
declare @j bigint;declare @k bigint;
while (@i AND xtype IN (N'FN', N'IF', N'TF')
)
DROP FUNCTION [dbo].[f_IP2Int]
GO
/*--字符型IP 地址转换成数字IP
--邹建 2004.08(引用请保留此信息)--*/
/*--调用示例
select dbo.f_IP2Int('255.255.255.255')
select dbo.f_IP2Int('12.168.0.1')
--*/
CREATE FUNCTION f_IP2Int
(
@ip CHAR(15)
)
RETURNS BIGINT
AS
BEGIN
DECLARE @re BIGINT
SET @re = 0
SELECT @re = @re+LEFT(@ip, CHARINDEX('.', @ip+'.')-1)*ID, @ip = STUFF(@ip, 1, CHARINDEX('.', @ip+'.'), '')
FROM (
SELECT> UNION ALL SELECT 65536
UNION ALL SELECT 256
UNION ALL SELECT 1
)A
RETURN(@re)
END
GO
IF EXISTS (
SELECT *
FROM dbo.sysobjects
WHERE> AND xtype IN (N'FN', N'IF', N'TF')
)
DROP FUNCTION [dbo].[f_Int2IP]
GO
/*--数字 IP 转换成格式化 IP 地址
--邹建 2004.08(引用请保留此信息)--
*/
/*--调用示例
select dbo.f_Int2IP(4294967295)
select dbo.f_Int2IP(212336641)
--*/
CREATE FUNCTION f_Int2IP
(
@IP BIGINT
)
RETURNS VARCHAR(15)
AS
BEGIN
DECLARE @re VARCHAR(16)
SET @re = ''
SELECT @re = @re+'.'+CAST(@IP/ID AS VARCHAR), @IP = @IP%ID
FROM (
SELECT> UNION ALL SELECT 65536
UNION ALL SELECT 256
UNION ALL SELECT 1
)a
RETURN(STUFF(@re, 1, 1, ''))
END
GO
select dbo.f_Int2IP(333444343)
/*
19.223.244.247
*/
56、对字符串进行加密解密
create view v_rand
as
select c=unicode(cast(round(rand()*255,0) as tinyint))
go
create function f_jmstr
(
@str varchar(8000),
@type bit
)
returns varchar(8000)
/*
*参数说明
*str:要加密的字符串或已经加密后的字符
*type:操作类型--0加密--解密
*返回值说明
*当操作类型为加密时(type--0):返回为加密后的str,即存放于数据库中的字符串
*当操作类型为解密时(type--1):返回为实际字符串,即加密字符串解密后的原来字符串
*/
As
begin
declare @re varchar(8000)--返回值
declare @c int--加密字符
declare @i int
/*
*加密方法为原字符异或一个随机ASCII字符
*/
if @type=0--加密
begin
select @c=c,@re='',@i=len(@str) from v_rand
while @i>0
select @re=nchar(unicode(substring(@str,@i,1))^@c^@i)+@re
,@i=@i-1
set @re=@re+nchar(@c)
end
else--解密
begin
select @i=len(@str)-1,@c=unicode(substring(@str,@i+1,1)),@re=''
while @i>0
select @re=nchar(unicode(substring(@str,@i,1))^@c^@i)+@re ,@i=@i-1
end
return(@re)
end
go
--测试
declare @tempstr varchar(20)
set @tempstr=' 1 2 3aA'
select '原始值:',@tempstr
select '加密后:',dbo.f_jmstr(@tempstr,0)
select '解密后:',dbo.f_jmstr(dbo.f_jmstr(@tempstr,0),1)
--输出结果
/*
原始值: 1 2 3aA
加密后: __0'15`'17__°{1
解密后: 1 2 3aA
*/
本文来自CSDN博客,转载请标明出处:
http://blog.csdn.net/callzjy/archive/2004/05/21/20071.aspx
57、计算个人所得税函数
-- =============================================
-- Author: Maco_wang
-- Create date: 2011-03-
-- Description: 参考htl258(Tony)的思路,改写的计算个税的函数
-- =============================================
create function TaxRateOfPersonal
(
@fvalue numeric(18,4)
)
returns numeric(18,4)
as
begin
declare @i numeric(18,4)
declare @basetable table(id int,
basemoney numeric(18,4),minvalue numeric(18,4),
maxvalue numeric(18,4),taxs numeric(18,4))
insert into @basetable
select 1,2000,0,1000,0.05 union all
select 2,2000,1000,3000,0.1 union all
select 3,2000,3000,6000,0.15 union all
select 4,2000,6000,10000,0.2 union all
select 5,2000,10000,15000,0.25
select @i=sum(case when @fvalue>basemoney+maxvalue
then maxvalue-minvalue else @fvalue-basemoney-minvalue end *taxs)
from @basetable where basemoney+minvalue |
|