转自:http://www.cnblogs.com/accumulater/p/6178083.htmlhtml
--SQL Server:
Select TOP N * From TABLE Order By NewID()
--开头到N条记录
Select Top N * From 表
--N到M条记录(要有主索引ID)
Select Top M-N * From 表Where ID in (Select Top M ID From 表) Order by ID Desc
--选择10从到15的记录
select top 5 * from (select top 15 * from table order by id asc) A order by id desc
--N到结尾记录
Select Top N * From 表Order by ID Desc
统计比例:0.11node
select cast( (sum( case when id=2 then 1 else 0 end) +0.00)/Count(1) as decimal(18,2)) as 比例 from @rtb
--显示最后5条记录,可是显示的顺序必须为5,6,7,8,9,10,而不是10,9,8,7,6,5 以下解决方法:
select top 5 from test where id in(select top 5 from test orderby id desc) orderby id asc
--经过这个问题也能总结出4-10条,5-100条这种限定必定范围内的sql语句的写法:
selecttop<末端ID-顶端ID+1>*from<表名>where ID notin(selecttop<顶端ID-1>) ID from<表名>)
--例如:4-10条就应该写成
selecttop10-4+1*from test where id notin(selecttop4-1 id from test)sql
上一篇: select top 1 * from [news_table] where [新闻标识列]<当前id号 where ......
下一篇: select top 1 * from [news_table] where [新闻标识列]>当前id号 where ...... order by [新闻标识列] desc数据库
create table t (name varchar(20))express
--大数据找差集
select
t.ip
into
iptemp
from
(
SELECT
b.ip
FROM
tb1 a
JOIN
tb2 b
on
b.num
between
a.startnum
and
a.endnum
) t
SELECT
ip
FROM
tb2
except
SELECT
ip
FROM
iptemp
|
清理日志:c#
一个表中的Id有多个记录,把全部这个id的记录查出来,并显示共有多少条记录数。
------------------------------------------
select id, Count(*) from tb group by id having count(*)>1
app
--两条记录彻底相同,如何删除其中一条
set rowcount=1
delete from thetablename where id=@duplicate_id--@duplicate_id为重复值的id
--模糊查询
select * from product where detail like '%123.jpg%'
--替换字段里面部份内容
update product set detail=replace(cast(detail as varchar(8000)),'abc.jpg','efg.jpg') dom
--SQL 替换掉 括号部份内容 select reverse(stuff(reverse(ServiceShopName), 1, charindex('(', reverse(ServiceShopName)), '')) from dbo.[签名] update dbo.签名 set ServiceShopName=reverse(stuff(reverse(ServiceShopName), 1, charindex('(', reverse(ServiceShopName)), '')) select distinct('【'+ServiceShopName+'】') from dbo.签名
--日期转换参数,值得收藏
select CONVERT(varchar, getdate(), 120 )
2004-09-12 11:06:08
select replace(replace(replace(CONVERT(varchar, getdate(), 120 ),'-',''),' ',''),':','')
20040912110608
select CONVERT(varchar(12) , getdate(), 111 )
2004/09/12
select CONVERT(varchar(12) , getdate(), 112 )
20040912
select CONVERT(varchar(12) , getdate(), 102 )
2004.09.12
--一个月第一天
SELECT DATEADD(mm, DATEDIFF(mm,0,getdate()), 0) -- 2009-06-01 00:00:00.000
--当天
select * from product where DateDiff(day,modiDate,GetDate())>1
--如何查询本日、本月、本年的记录SQL
本年:
select * from loanInfo where year(date)=year(getdate())
本月:
select * from loanInfo where year(date)=year(getDate()) And month(date)=month(getdate())
本日:
select * from loanInfo where year(date)=year(getDate()) And month(date)=month(getdate()) and Day(date)=Day(getDate())
昨天的记录:
datediff(day,[Datetime],getdate())=1 把Datetime换为你的相应字段,getdate()-Datetime即为时间差。
本月记录:
SELECT * FROM 表 WHERE datediff(month,[dateadd],getdate())=0
本周记录:
SELECT * FROM 表 WHERE datediff(week,[dateadd],getdate())=0
本日记录:
SELECT * FROM 表 WHERE datediff(day,[dateadd],getdate())=0
本周的星期一 SELECT DATEADD(wk, DATEDIFF(wk,0,getdate()), 0)
一年的第一天 SELECT DATEADD(yy, DATEDIFF(yy,0,getdate()), 0)
季度的第一天 SELECT DATEADD(qq, DATEDIFF(qq,0,getdate()), 0)
当天的半夜 SELECT DATEADD(dd, DATEDIFF(dd,0,getdate()), 0)
上个月的最后一天
这是一个计算上个月最后一天的例子。它经过从一个月的最后一天这个例子上减去毫秒来得到。有一点要记住,在Sql Server中时间是精确到毫秒。这就是为何我须要减去毫秒来得到我要的日期和时间。
SELECT dateadd(ms,-3,DATEADD(mm, DATEDIFF(mm,0,getdate()), 0))
计算出来的日期的时间部分包含了一个Sql Server能够记录的一天的最后时刻(“:59:59:997”)的时间。
去年的最后一天
链接上面的例子,为了要获得去年的最后一天,你须要在今年的第一天上减去毫秒。
SELECT dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate()), 0))
本月的最后一天
如今,为了得到本月的最后一天,我须要稍微修改一下得到上个月的最后一天的语句。修改须要给用DATEDIFF比较当前日期和“-01-01”返回的时间间隔上加。经过加个月,我计算出下个月的第一天,而后减去毫秒,这样就计算出了这个月的最后一天。这是计算本月最后一天的SQL脚本。
SELECT dateadd(ms,-3,DATEADD(mm, DATEDIFF(m,0,getdate())+1, 0))
本年的最后一天
你如今应该掌握这个的作法,这是计算本年最后一天脚本
SELECT dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate())+1, 0))。
本月的第一个星期一
好了,如今是最后一个例子。这里我要计算这个月的第一个星期一。这是计算的脚本。
select DATEADD(wk, DATEDIFF(wk,0,
dateadd(dd,6-datepart(day,getdate()),getdate())
), 0)
在这个例子里,我使用了“本周的星期一”的脚本,并做了一点点修改。修改的部分是把原来脚本中“getdate()”部分替换成计算本月的第天,在计算中用本月的第天来替换当前日期使得计算能够得到这个月的第一个星期一。
--删除一个月前,三个月前, 6个月前,一年前的数据
DELETE FROM 表名WHERE datediff(MM, AddTime,GETDATE()) > 1
DELETE FROM 表名WHERE datediff(MM, AddTime,GETDATE()) > 3
DELETE FROM 表名WHERE datediff(MM, AddTime,GETDATE()) > 6
DELETE FROM 表名WHERE datediff(YY, AddTime,GETDATE()) > 1
---------------------------------------------------------------
附录,其余日期处理方法
1)去掉时分秒
declare @ datetime
set @ = getdate() --'2003-7-1 10:00:00'
SELECT @,DATEADD(day, DATEDIFF(day,0,@), 0)
2)显示星期几
select datename(weekday,getdate())
3)如何取得某个月的天数
declare @m int
set @m=2 --月份
select datediff(day,'2003-'+cast(@m as varchar)+'-15' ,'2003-'+cast(@m+1 as varchar)+'-15')
另外,取得本月天数
select datediff(day,cast(month(GetDate()) as varchar)+'-'+cast(month(GetDate()) as varchar)+'-15' ,cast(month(GetDate()) as varchar)+'-'+cast(month(GetDate())+1 as varchar)+'-15')
任意月份的最大天数
select day(dateadd(dd,-1,dateadd(mm,1,Dateadd(mm,datediff(mm,0,getdate()),0))))
或者使用计算本月的最后一天的脚本,而后用DAY函数区最后一天
SELECT Day(dateadd(ms,-3,DATEADD(mm, DATEDIFF(m,0,getdate())+1, 0)))
4)判断是否闰年:
SELECT case day(dateadd(mm, 2, dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate()), 0)))) when 28 then '平年' else '闰年' end
或者
select case datediff(day,datename(year,getdate())+'-02-01',dateadd(mm,1,datename(year,getdate())+'-02-01'))
when 28 then '平年' else '闰年' end
5)一个季度多少天
declare @m tinyint,@time smalldatetime
select @m=month(getdate())
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,getdate())+'-'+convert(varchar(10),@m)+'-01'
select datediff(day,@time,dateadd(mm,3,@time))
一、肯定某年某月有多少天
实现原理:先利用DATEDIFF取得当前月的第一天,再将月份加一取得下月第一天,而后减去分钟,再取日期的天数部分,即为当月最大日期,也即当月天数
CREATE FUNCTION DaysInMonth ( @date datetime ) Returns int
AS
BEGIN
RETURN Day(dateadd(mi,-3,DATEADD(m, DATEDIFF(m,0,@date)+1,0)))
END
调用示例:
select dbo.DaysInMonth ('2006-02-03')
(2)计算哪一天是本周的星期一
SELECT DATEADD(week, DATEDIFF(week,'1900-01-01',getdate()), '1900-01-01') --返回-11-06 00:00:00.000
或
SELECT DATEADD(week, DATEDIFF(week,0,getdate()),0)
(3)当前季度的第一天
SELECT DATEADD(quarter, DATEDIFF(quarter,0,getdate()), 0)—返回-10-01 00:00:00.000
(4)一个季度多少天
declare @m tinyint,@time smalldatetime
select @m=month(getdate())
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,getdate())+'-'+convert(varchar(10),@m)+'-01'
select datediff(day,@time,dateadd(mm,3,@time)) —返回
1.按姓氏笔画排序:
Select * From TableName Order By CustomerName Collate Chinese_PRC_Stroke_ci_as
2.分页SQL语句
select * from(select (row_number() OVER (ORDER BY tab.ID Desc)) as rownum,tab.* from 表名As tab) As t where rownum between 起始位置And 结束位置
8.如何修改数据库的名称:
sp_renamedb 'old_name', 'new_name'
3.获取当前数据库中的全部用户表
select * from sysobjects where xtype='U' and category=0
4.获取某一个表的全部字段
select name from syscolumns where id=object_id('表名')
5.查看与某一个表相关的视图、存储过程、函数
select a.* from sysobjects a, syscomments b where a.id = b.id and b.text like '%表名%'
6.查看当前数据库中全部存储过程
select name as 存储过程名称from sysobjects where xtype='P'
7.查询用户建立的全部数据库
select * from master..sysdatabases D where sid not in(select sid from master..syslogins where name='sa')
或者
select dbid, name AS DB_NAME from master..sysdatabases where sid <> 0x01
8.查询某一个表的字段和数据类型
select column_name,data_type from information_schema.columns where table_name = '表名'
9.使用事务
在使用一些对数据库表的临时的SQL语句操做时,能够采用SQL SERVER事务处理,防止对数据操做后发现误操做问题
开始事务
Begin tran
Insert Into TableName Values(…)
SQL语句操做不正常,则回滚事务。
回滚事务
Rollback tran
SQL语句操做正常,则提交事务,数据提交至数据库。
提交事务
Commit tran
计算执行SQL语句查询时间
declare @d datetime
set @d=getdate()
select * from SYS_ColumnProperties select [语句执行花费时间(毫秒)]=datediff(ms,@d,getdate()) 函数
【关闭SQL Server 数据库全部使用链接】
use master
go
create proc KillSpByDbName(@dbname varchar(20))
as
begin
declare @sql nvarchar(500),@temp varchar(1000)
declare @spid int
set @sql='declare getspid cursor for
select spid from sysprocesses where dbid=db_id('''+@dbname+''')'
exec (@sql)
open getspid
fetch next from getspid into @spid
while @@fetch_status <>-1
begin
set @temp='kill '+rtrim(@spid)
exec(@temp)
fetch next from getspid into @spid
end
close getspid
deallocate getspid
end
--举例使用,关闭数据库下的全部链接操做
Use master
Exec KillSpByDbName '数据库名称'
(一)挂起操做
在安装Sql或sp补丁的时候系统提示以前有挂起的安装操做,要求重启,这里每每重启无用,解决办法:
到HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlSession Manager
删除PendingFileRenameOperations
(二)收缩数据库
--重建索引
DBCC REINDEX
DBCC INDEXDEFRAG
--收缩数据和日志
DBCC SHRINKDB
DBCC SHRINKFILE
(三)压缩数据库
dbcc shrinkdatabase(dbname)
(四)转移数据库给新用户以已存在用户权限
exec sp_change_users_login update_one,newname,oldname
go
(五)检查备份集
RESTORE VERIFYONLY from disk=Evbbs.bak
(六)修复数据库
ALTER DATABASE [dvbbs] SET SINGLE_USER
GO
DBCC CHECKDB(dvbbs,repair_allow_data_loss) WITH TABLOCK
GO
ALTER DATABASE [dvbbs] SET MULTI_USER
GO
fetch
and Ip in (SELECT Ip FROM PhoneVcode WHERE Ip='127.0.0.1' GROUP BY Ip having count(Ip)>2)
select top m * from tablename where id not in (select top n id from tablename)
select top m * into 临时表(或表变量) from tablename order by columnname -- 将top m笔插入
set rowcount n
select * from 表变量order by columnname desc
select top n * from
(select top m * from tablename order by columnname) a
order by columnname desc
复制表(只复制结构,源表名:a 新表名:b) (Access可用)
法一:select * into b from a where 1 <>1
法二:select top 0 * into b from a
拷贝表(拷贝数据,源表名:a 目标表名:b) (Access可用)
insert into b(a, b, c) select d,e,f from b;
跨数据库之间表的拷贝(具体数据使用绝对路径) (Access可用)
insert into b(a, b, c) select d,e,f from b in ‘具体数据库’where 条件
例子:..from b in '"&Server.MapPath(".")&"\data.mdb" &"' where..
子查询(表名:a 表名:b)
select a,b,c from a where a IN (select d from b ) 或者: select a,b,c from a where a IN (1,2,3)
显示文章、提交人和最后回复时间
select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b
外链接查询(表名:a 表名:b)
select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c
在线视图查询(表名:a )
select * from (SELECT a,b,c FROM a) T where t.a > 1;
between的用法,between限制查询数据范围时包括了边界值,not between不包括
select * from table1 where time between time1 and time2
select a,b,c, from table1 where a not between 数值and 数值
in 的使用方法
select * from table1 where a [not] in (‘值’,’值’,’值’,’值’)
两张关联表,删除主表中已经在副表中没有的信息
delete from table1 where not exists ( select * from table2 where table1.field1=table2.field1 )
四表联查问题:
select * from a left inner join b on a.a=b.b right inner join c on a.a=c.c inner join d on a.a=d.d where ..
日程安排提早五分钟提醒
SQL: select * from 日程安排where datediff('minute',f开始时间,getdate())>5
一条sql 语句搞定数据库分页
select top 10 b.* from (select top 20 主键字段,排序字段from 表名order by 排序字段desc) a,表名b where b.主键字段= a.主键字段order by a.排序字段
选择在每一组b值相同的数据中对应的a最大的记录的全部信息(相似这样的用法能够用于论坛每个月排行榜,每个月热销产品分析,按科目成绩排名,等等.)
select a,b,c from tablename ta where a=(select max(a) from tablename tb where tb.b=ta.b)
包括全部在TableA 中但不在TableB和TableC 中的行并消除全部重复行而派生出一个结果表
(select a from tableA ) except (select a from tableB) except (select a from tableC)
随机取出条数据
select top 10 * from tablename order by newid()
随机选择记录
select newid()
declare @x xml select @x=cast('<A>'+replace(@Ids,',','</A><A>')+'</A>' as xml) begin --插入 INSERT dbo.tb select 111,t.value('.','int'),getdate() from @x.nodes('/A') as x (t)
--float字段保留一位小数,四舍五入
SELECT CONVERT(DECIMAL(18,1),1024.791454) ------- 1024.8 (所影响的行数为1 行)
<%# Eval("字段")==null?"":Eval("字段").toString("0.0") %>
--事务(进程 ID 77)与另外一个进程被死锁在 锁 | 通讯缓冲区 资源上,而且已被选做死锁牺牲品。请从新运行该事务。 --优先选择3,2,1 --1.表后面加 with (nolock) --2.会话前面加 update的时候,能select SET TRANSACTION ISOLATION LEVEL Read uncommitted --3.整个数据库隔离级别改成行版本 ALTER DATABASE SMMM SET SINGLE_USER WITH ROLLBACK IMMEDIATE ALTER DATABASE SMMM SET read_committed_snapshot ON --行版本隔离 ALTER DATABASE SMMM set MULTI_USER
select object_name(id) as 表名, rows as 使用行数, rtrim(8*reserved/1024)+'Mb' as 分配空间, rtrim(8*dpages/1024) as 使用空间, rtrim(8*(reserved-dpages)/1024) as 未使用空间, rtrim(8*dpages/1024-rows/1024*minlen/1024) as 空闲空间 from sysindexes order by rows desc