常用SQL SERVER语句

记录一下自己经常用的SQL语句

查询表中字段数据重复:

 select * from table(表名) a where (a.字段) in  (select 字段 from table(表名) group by 字段 having count(*) > 1)

判断字段是否为16进制:

select*from table(表名) A WHERE A.字段   LIKE '%[^0123456789ABCDEF]%' 这个可以找出小写的abcdef

判断字段是否为10进制:

select*from table(表名) A WHERE A.字段   LIKE '%[^0-9]%'

找出字段中为null的数据:

select*from table(表名) A WHERE A.字段  IS NULL

找出字段中为空的数据:

select*from table(表名) A WHERE A.字段 =''

两张表中相同数据和不同数据:

 --1.select distinct A.ID from  A where A.ID not in (select ID from B)
 --2.select A.ID from A left join B on A.ID=B.ID where B.ID is null
 --3.select * from  B  where (select count(1) as num from A where A.ID = B.ID) = 0
 --4.SELECT * FROM b WHERE NOT EXISTS(SELECT 1 FROM a WHERE tel_no=b.tel_no)
 --5.EXCEPT  intersect

删除字段:

 alter table table(表名)  
 drop  column 字段

往一个表中插入另一个表的数据:

INSERT INTO table1(字段)
 SELECT  字段  from  table2

查询连续流水号中缺失的数据:

create table #tmp(缺失数据 nvarchar(10))
declare @i int
set @i = 4000001

while @i <= 4002500
begin
   insert into #tmp  select  cast(@i as nvarchar)
   set @i = @i + 1
end

select a.缺失数据
from #tmp a
left join table b  on a.缺失数据=b.字段
where b.字段 is null

select a.字段
from table a
left join #tmp b  on a.字段=b.缺失数据
where b.缺失数据 is null

drop table #tmp

这段SQL语句是创建一个临时表和原先的表比较来找出4000001- 4002500缺失的数据

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值