Frank

导航

SQL 2000 实战积累

守则:
1、如果可以,要加条件判断,为了不必要的错误


use hferpdb -- 选择数据库

select * from hr_person -- 查数据的时候最好写明字段,这样会提高效率

select departname,nam,sex,age,identityno,workdate,duty,educationlevel,tel,telbak from hr_person


select * into hr_personbackup from hr_person -- 复制表,只用于测试


update hr_person set tel=b.phone,telbak=b.duan from hr_person ,phone b
where hr_person.nam=b.nam -- 从另一个表提取数据来更新当前表

drop table hr_personbackup --删除整个表

select count(nam) '该列显示的名称' from phone --统计某个字段的数量

 

delete from hr_person where tel is null --删除,最好加条件

select top 0 * from people --用于看有什么字段名称

delete from people where xingming in (select nam from hr_personbackup group by nam)
--条件可以是数据集,不一定是表


insert into phone (nam) select xingming from people p where p.xingming is not null
--把某个字段插入目标表的字段


--把一个表的数据插入到另一个表指定的列中
insert into hr_person(departname,nam,sex,identityno,workdate,duty,educationlevel,LastOpDate,CreateDate,isSocialSec,id_person)
select bumen,xingming,sex1,sfz,worktime,job2,xueli,getdate(),getdate(),0,id2 from people where id2 is not null


--截取身份证的出生年月
select substring(identityno,7,8) from hr_personbackup

--把身份证转换为出生日期
select cast(substring(identityno,7,8) as datetime) from hr_personbackup

--得到身份证的到现在时间的年龄,不过一般是设计年龄字段 = 当前时间减去现在的时间
select datediff(year,substring(identityno,7,8),getdate()) from hr_personbackup

--cast 用于转化字段格式,不过必须与目标字段的格式不一样
update hr_person set birthday = cast(substring(identityno,7,8) as datetime) from hr_person

 

--查找相同的记录
select nam from hr_person group by nam having count(nam) >1

 

未完待续。。。

posted on 2013-01-12 10:17  慢慢沉淀  阅读(186)  评论(0编辑  收藏  举报