(1) try catch使用
--打开try catch功能 set xact_abort on begin try begin tran insert into tableName(ID) values(1) commit tran print 'commited' end try begin catch rollback print 'rolled back' end catch
(2)获取当前月前一个月有多少天(DatePart( date , datetime) DateAdd(date , 数字 ,datetime) Cast(exp as datetime))
select DatePart (day,DateAdd(DAY,-1, Cast(cast(YEAR(getdate()) as nvarchar)+'-'+ cast(month(getdate()) as nvarchar)+'-01' as datetime)) )
(3)取到小数第二位四舍五入(类型转换)
Select Convert(Numeric(20,2), IsNull(50.01634,0))
结果:50.02
(4)获取时间
[1]一个月第一天:select DateAdd(mm,DateDiff(mm,0,getdate()),0)
--DateAdd当添加的值和初试值均为0时,返回的时间为1900-01-01 00:00:000
select DATEADD(mm,0,0);
[2]本年:year(getdate())
[3]本月:month(getdate())
[4]本日:Day(getDate())
[5]昨天的记录:datediff(day,[Datetime],getdate())=1 把Datetime换为你的相应字段,getdate()-Datetime即为时间差。
[6]本月/周/日记录:DateDiff(month/week/day,[dateadd],getdate())=0
[7]本周的星期一:Select DateAdd(wk, DateDiff(wk,0,getdate()), 0)
[8]季度的第一天:Select DateAdd(qq, DateDiff(qq,0,getdate()), 0)
[9]当天的凌晨(半夜):Select DateAdd(dd, DateDiff(dd,0,getdate()), 0)
[10]本年的第一天::select DateAdd(YY,DateDiff(yy,0,getdate()),0)
[11]去年的最后一天(今年的第一天减去3毫秒):select DateAdd(ms,-3,DateAdd( yy, DateDiff( yy, 0, getdate()), 0))
[12]本月的第一天:select DateAdd( mm, DateDiff( mm, 0 , GetDate()), 0);
[13]本月的最后一天:select DateAdd(ms,-3, DateAdd(mm, DateDiff(mm,0,getdate()), 0))
[14]本年的最后一天:Select DateAdd(ms,-3, DateAdd(yy, DateDiff(yy, 0, getdate()) +1, 0))
[15]本月的第一个星期一:select DateAdd(wk, DateDiff(wk,0, DateAdd(dd,6 - Datepart(day,getdate()) ,getdate()) ), 0)
[16]显示星期几:select DateName(weekday , getdate() );
[17]显示本月的天数:select DateDiff(dd , getdate() , DateAdd(mm , 1 , getdate()) );
[18]显示某年某月有多少天
create Function DaysInMonth1(@datetime datetime) returns int as begin return Day( Datepart( dd, DateAdd( ms, -3, DateAdd(mm ,DateDiff(mm,0,@datetime)+1 , 0) ) )) end 调用select dbo.DaysInMonth1('2012-2-13');
(5)获取当前数据库中所有用户的表
select * from sysobjects where xtype='U'
(6)随机取出3条数据
Select top 3 * from Table order by NewId()
(7)查找表中多余的重复记录
Select * from Table where ID in( select SID from Table group by ID having Count(Name)>1)
(8)关键字waitfor 主要有waitfor delay(推迟执行),waitfor time (特定时间执行)
waitfor delay '00:00:08'
print('Hello ,Im waitfrom delay')
waitfor time '20:49:50'
print('Hello,Im waitfor time')
(9)检测数据是否存在
[1]检测数据库是否存在
if( Exists(select * from sys.databases where name='School'))
select * from Student
go
[2]检测数据表是否存在
if( exists(select * from sys.objects where name='Student'))
select * from Student order by SID desc
go
[3]检测数据列是否存在
if(exists(select * from sys.all_columns where object_id=object_id('Student') and name='Sname'))
select * from Student
go
(10)表连接
--同时无条件的查询两个不相关的表,所得新表字段为两表字段之和,信息条数为两个信息条数的乘积 (即为:两表的笛卡尔积)
select T.* , Student.* from T , Student
--交叉连接即为上边所讲的那种连接
select T.*,Student.* from T Cross Join Student
--随机数均可以产生0-1之间的随机数
Select Rand(); select Rand( CheckSum(NewId()) )
--随机数0-9
select ABS(Rand()); select ABS(CheckSum(NewId()))%10
--随机数a-b之间
select a+ABS(checksum(newid()))%(b-a+1)
select 1+ABS(checksum(newid()))%100
字符串操作
--SubString字符串截取(str ,StartIndex , Length)
select SubString( 'Olive116', 1, 5)
--left/right从左边截取字符串
select Left/Right ( 'Olive', 3 )
--字符串替换Replace( str, str1, str2)
select Replace('OOLive','o','1')
--反转排序
select Reverse('Olive')
--删除指定的长度并在指定的起点处插入另一组字符
select Stuff('OliveOOO',6,3,'Hello')
--以指定的次数重复字符串的值
select Replicate('Olive',3)
--返回表达式中第一次出现的位置
select PatIndex('%Olive%','Hello!Olive')
--返回字符串中指定表达式的开始位置
select CharIndex('O','Hello,Olive')
--返回输入表达式的第一个字符的整数值
select UniCode('o')
--返回由数字数据转换来的字符数据
select STR(123.1314,4),LEN(STR(123.3212,321))
select STR(123.1334,4,4)
--发音匹配度
select Sname,SOUNDEX(Sname) from Student
--Difference()简化两个字符串发音相似度,返回0-4之间的值来反应两个字符的相似度,值越大就越相似
select Sname,SOUNDEX(Sname), Difference (Sname,'Olive') from Student
--查询重复记录
select * from Table where SID in (select SID from Table group by SID having COUNT(SID)>0)
--删除重复的记录(并保留SID最小的记录)
delete from Table where
SID in (select SID from Table group by SID having COUNT(SID)>0)
and SID not in(select MIN(SID) from Student group by SID having count(SID)>0)
--双引号
select '''''' as result
--isnumeric()判断是否是数字是为1否为0
select IsNumeric('a')
--isdate()判断是否为日期是为1否为0
select IsDate('2012-3-2')