SQL常用小总结【不断更新】

1.去除重复【DISTINCT】

select distinct NAME from T_USER 

2.判断是否是数字【ISNUMERIC】  

3.模糊查询PATINDEX

4.向SQL Server 一次插入多条记录

  使用的是“insert into [table] select values() uinon all ”语句

        方法一:

  CREATE TABLE [学生表] (Sno INT,Sname VARCHAR(4),Ssex VARCHAR(2),Sage INT,sdept VARCHAR(2))
	INSERT INTO [学生表]
	SELECT 95001,'李勇','男',20,'CS' UNION ALL
	SELECT 95002,'刘晨','女',19,'IS' UNION ALL
	SELECT 95003,'王敏','女',18,'IS' UNION ALL
	SELECT 95004,'张立','男',19,'MA' UNION ALL
	SELECT 96001,'徐一','男',20,'IS' UNION ALL
	SELECT 96002,'张三','女',21,'CS' UNION ALL
	SELECT 96003,'李四','男',18,'IS'

  方法二:(所见版,个人推荐)

Insert into Student(Sno,Sname,Ssex,Sage,sdept) 
	select '95001','李勇','男','20','CS'union all
	select '95001','李勇','男','20','CS'
	.
	.
	.
	select '95001','李勇','男','20','CS'  

  提醒:最后的一条语句是没有 union all 的。

5.获取时间的格式如2012-06-15不要后面的时间

select convert(varchar(10),getdate(),120) 

  

--昨天  
select convert(varchar(10),getdate() - 1,120) 
--明天 
select convert(varchar(10),getdate() + 1,120) 
--最近七天 
select * from tb where 时间字段 >= convert(varchar(10),getdate() - 7,120) 
--随后七天 
select * from tb where 时间字段 <= convert(varchar(10),getdate() + 7,120) and 时间字段 >= 时间字段 
--上月 
select * from tb where month(时间字段) = month(getdate()) - 1 
--本月 
select * from tb where month(时间字段) = month(getdate()) 
--下月 
select * from tb where month(时间字段) = month(getdate()) + 1 

  

原文地址:https://www.cnblogs.com/ruicky/p/2550687.html