文档库 最新最全的文档下载
当前位置:文档库 › sqlserver 自定义函数

sqlserver 自定义函数

--计算当前月的实际天数
Create FUNCTION dbo.CalcDaysOfMonth (@time varchar(6))
RETURNS int
AS
BEGIN
DECLARE @Days int
DECLARE @Month int
DECLARE @Year int
SET @Year=SUBSTRING(@time,1,4)
SET @Month=SUBSTRING(@time,5,6)
if(
@Month='1'
OR @Month='3'
OR @Month='5'
OR @Month='7'
OR @Month='8'
OR @Month='10'
OR @Month='12'
)
set @Days=31
else if(
@Month='4'
OR @Month='6'
OR @Month='9'
OR @Month='11'
)
set @Days=30;

else
if(@Year%400=0 OR (@Year%4=0 AND @Year%100<>0))
set @Days=29
else
set @Days=28
RETURN(@Days)
END




--确定某年某月有多少天
Create FUNCTION DaysInMonth ( @date datetime ) Returns int
AS
BEGIN
RETURN Day(dateadd(mi,-3,DATEADD(m, DATEDIFF(m,0,@date)+1,0)))
END

--哪一天是输入时间的星期一
Create FUNCTION MondayInDate ( @date datetime ) RETURNS DATETIME
AS
BEGIN
RETURN DATEADD(week, DATEDIFF(week,0,@date),0)
END


--输入时间的季度的第一天
Create FUNCTION QuarterInDate ( @date datetime ) RETURNS DATETIME
AS
BEGIN
RETURN DATEADD(quarter, DATEDIFF(quarter,0,@date), 0)
END


--输入时间的季度的天数
Create FUNCTION QuarterDaysInDate ( @date datetime ) RETURNS INT
AS
BEGIN
declare @m tinyint,@time SMALLDATETIME

select @m=month(@date)
select @m=case when @m between 1 and 3 then 1
when @m between 4 and 6 then 4
when @m between 7 and 9 then 7
else 10 end

select @time=datename(year,@date)+'-'+convert(varchar(10),@m)+'-01'
return datediff(day,@time,dateadd(mm,3,@time))
END

--按指定符号分割字符串,返回分割后的元素个数,方法很简单,就是看字符串中存在多少个分隔符号,然后再加一,就是要求的结果。
Create function Get_StrArrayLength
(
@str varchar(1024), --要分割的字符串
@split varchar(10) --分隔符号
)
returns int
as
begin
declare @location int
declare @start int
declare @length int

set @str=ltrim(rtrim(@str))
set @location=charindex(@split,@str)
set @length=1
while @location<>0
begin
set @start=@location+1
set @location=charindex(@split,@str,@start)
set @length=@length+1
end
return @length
END

--按指定符号分割字符串,返回分割后指定索引的第几个元素,象数组一样方便
Create function Get_StrArrayStrOfIndex
(
@str varchar(1024), --要分割的字符串
@split varchar(10), --分隔符号
@index int --取第几个元素
)
returns varchar(1024)
as
begin
declare @location int
declare @start int
declare @next int
declare @seed int

set @str=ltrim(rtrim(@str))
set @start=1
set @next=1
set @seed=len(@split)

set @location=charindex(@split,@str)
while @location<>0 and @index>@next
begin
set @start=@location+@seed
set @location=charindex(@split,@str,@start)
set @next=@next+1
end
if @location =0 select @location =len(@str)+1
--这儿存在两种情况:1、字符串不存在分隔符号 2、字符串中存在分隔符号,跳出while循环后,@location为0,那默认为字

符串后边有一个分隔符号。

return substring(@str,@start,@location-@start)
END

select dbo.Get_StrArrayStrOfIndex('8,9,4','',4)


--结合上边两个函数,象数组一样遍历字符串中的元素
create function f_splitstr(@SourceSql varchar(8000),@StrSeprate varchar(100))
returns @temp table(F1 varchar(100))
as
begin
declare @ch as varchar(100)
set @SourceSql=@SourceSql+@StrSeprate
while(@SourceSql<>'')
begin
set @ch=left(@SourceSql,charindex(',',@SourceSql,1)-1)
insert @temp values(@ch)
set @SourceSql=stuff(@SourceSql,1,charindex(',',@SourceSql,1),'')
end
return
end

select * from dbo.f_splitstr('1,2,3,4',',')


--全角和半角转换函数
Create FUNCTION f_Convert(
@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
GO

declare @s1 varchar(8000)
select @s1='中 2-3456a78STUVabn中国opwxyz'
select dbo.f_convert(@s1,0),dbo.f_convert(@s1,1)

函数返回值是表
create table test(id int primary key,name char(10))



insert into test values(1,'test1')

insert into test values(2,'test2')

insert into test values(3,'test3')

insert into test values(4,'test4')



1、标量函数

create function return_count()

returns int

as

begin

declare @count int

select @count=count(*) from test

return @count

end

--调用

select dbo.return_count() cont --count为显示的列头

--运行结果

--count

--4





2、内嵌表值函数

create function return_test()

returns table

as

--begin 内联表值函数不能用begin-end

return select name from test

--end

--调用

select * from return_test()

--运行结果

--name

--test1

--test2

--test3

--test4


3、多语句表值函数

create function return_test_multi()

returns @temp table(id int,name char(10))

as

begin

insert into @temp select * from test where id in(1,2)

return --记住,一定不要忘记写return

end

--调用

select * from dbo.return_test_multi()

--运行结果

--id name

--1 test1

--2 test2







在查询结果中增加一个自动增长的ID
select id=identity(int, 1, 1), * into #T from testTable

select * from #T

drop table #T

sql删除重复的记录
打开测试数据库test,并以表w01为例,将下面的SQL语句放入sql2000查询分析器中,一段一段执行即可看

到效果
---在sql2000下创建测试数据表
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[w01]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[w01]
---在sql2005下创建测试数据表,如果是sql2005则用本段来判断数据表是否存在
---if exists(select 1 from sys.tables where name='w01')
---drop table w01
----开始创建测试数据库
GO
create table w01(gs903 varchar(32),gs1002 varchar(32))
insert into w01
select '1','a'
union all select '1','a'
union all select '1','a'
union all select '2','a'
union all select '2','e'
union all select '3','b'
union all select '3','d'
go
select * from w01
go

---为表w01添加一个可以表示唯一标示的自增字段ID
alter table w01 add [ID] [int] IDENTITY (1, 1) NOT FOR REPLICATION NOT NULL

---查询删除前的数据和记录数:7
select * from w01
select count(*) from w01
---查询具有重复记录的所有记录;3
select gs903,gs1002,count(*) as count from w01 group by gs903,gs1002
having count(*)>1
order by count desc
---删除重复的数据:2行
delete from w01 where id not in (select max(id) from w01 group by gs903,gs1002)

---看看删除后还有没有重复记录:0
select gs903,gs1002,count(*) as count from w01 group by gs903,gs1002
having count(*)>1
order by count desc
---删除后的数据和记录数:7-2=5
select * from w01
select count(*) from w01

用SQL语句添加删除修改字段

增加字段
alter table docdsp add dspcode char(200)
删除字段
Alter TABLE table_NAME Drop COLUMN column_NAME
修改字段类型
Alter TABLE table_name Alter COLUMN column_name new_data_type
改名
sp_rename
更改当前数据库中用户创建对象(如表、列或用户定义数据类型)的名称。
语法
sp_rename [ @objname = ] 'object_name' ,
[ @newname = ] 'new_name'
[ , [ @objtype = ] 'object_type' ]


--假设要处理的表名为: tb
--判断要添加列的表中是否有主键
if exists(select 1 from sysobjects where parent_obj=object_id('tb') and xtype='PK')
begin
print '表中已经有主键,列只能做为普通列添加'
--添加int类型的列,默认值为0
alter table tb add 列名 int default 0
end
else
begin
print '表中无主键,添加主键列'
--添加int类型的列,默认值为0
alter table tb add 列名 int primary key default 0
end

相关文档