--2 insert cooldb(name,sex,time,phone) select name,sex,time,phone from a t where not exists(select 1 from cooldb where phone=t.phone)
哇 先谢谢上面的几位 回复这么快呢我的表 time 是varchar 类型...
--如果你time都是varchar型update tb set age = (case when isdate(time) = 1 then datediff(yy , cast(time as datetime) , getdate()) else year(getdate()) - cast(time as int) end) from tb where age is null
好的 下次注意 我的cooldb表 字段 id name sex time age address phone a表 字段name sex time phone id是自动增长的就把a表插进 cooldb表 以phone去重就好了,其他没有字段null插入 谢谢大家这么热情 我会按你们方法去试
create table tb(id int, name varchar(10), sex varchar(10), time varchar(30), age int,address varchar(50), phone varchar(20)) insert into tb values(1 ,'张三', '男', '1984-01-15 00:00:00.000', 25 , '中国' , '123123123123') insert into tb values(2 ,'李四', '女', '1987-06-26 00:00:00.000', 22 , '中国' , '123123123123') insert into tb values(3 ,'王二', '女', '1984-11-25 00:00:00.000', 25 , null , '123123123123') insert into tb values(4 ,'丫丫', '男', '1979-11-26 00:00:00.000', null, '中国' , '123123123123') insert into tb values(5 ,'的的', '女', '1984-06-09 00:00:00.000', null, '中国' , null) insert into tb values(6 ,'天天', '男', '1979' , null, '中国' , '123123123123') insert into tb values(7 ,'滴滴', '男', '1983' , null, '中国' , '123123123123') go --原始数据 select * from tb /* id name sex time age address phone ----------- ---------- ---------- ------------------------------ ----------- -------------------------------------------------- -------------------- 1 张三 男 1984-01-15 00:00:00.000 25 中国 123123123123 2 李四 女 1987-06-26 00:00:00.000 22 中国 123123123123 3 王二 女 1984-11-25 00:00:00.000 25 NULL 123123123123 4 丫丫 男 1979-11-26 00:00:00.000 NULL 中国 123123123123 5 的的 女 1984-06-09 00:00:00.000 NULL 中国 NULL 6 天天 男 1979 NULL 中国 123123123123 7 滴滴 男 1983 NULL 中国 123123123123(所影响的行数为 7 行) */update tb set age = (case when isdate(time) = 1 then datediff(yy , cast(time as datetime) , getdate()) else year(getdate()) - cast(time as int) end) from tb where age is null--更新后的数据 select * from tb /* id name sex time age address phone ----------- ---------- ---------- ------------------------------ ----------- -------------------------------------------------- -------------------- 1 张三 男 1984-01-15 00:00:00.000 25 中国 123123123123 2 李四 女 1987-06-26 00:00:00.000 22 中国 123123123123 3 王二 女 1984-11-25 00:00:00.000 25 NULL 123123123123 4 丫丫 男 1979-11-26 00:00:00.000 30 中国 123123123123 5 的的 女 1984-06-09 00:00:00.000 25 中国 NULL 6 天天 男 1979 30 中国 123123123123 7 滴滴 男 1983 26 中国 123123123123(所影响的行数为 7 行) */drop table tb
set age = datediff(yy , time , getdate())update tb
set age = datediff(yy , time , getdate())
from tb
where age is null
最好给出完整的表结构,测试数据,计算方法和正确结果.发帖注意事项
http://topic.csdn.net/u/20091130/21/fb718680-98ff-4afb-98d8-cff2f8293ed5.html?24281
insert cooldb(name,sex,time,phone)
select name,sex,time,phone
from a t where not exists(select 1 from cooldb where phone=t.phone)
哇 先谢谢上面的几位 回复这么快呢我的表 time 是varchar 类型...
set age = (case when isdate(time) = 1 then datediff(yy , cast(time as datetime) , getdate())
else year(getdate()) - cast(time as int)
end)
from tb where age is null
好的 下次注意 我的cooldb表 字段
id name sex time age address phone a表 字段name sex time phone id是自动增长的就把a表插进 cooldb表 以phone去重就好了,其他没有字段null插入
谢谢大家这么热情 我会按你们方法去试
insert into tb values(1 ,'张三', '男', '1984-01-15 00:00:00.000', 25 , '中国' , '123123123123')
insert into tb values(2 ,'李四', '女', '1987-06-26 00:00:00.000', 22 , '中国' , '123123123123')
insert into tb values(3 ,'王二', '女', '1984-11-25 00:00:00.000', 25 , null , '123123123123')
insert into tb values(4 ,'丫丫', '男', '1979-11-26 00:00:00.000', null, '中国' , '123123123123')
insert into tb values(5 ,'的的', '女', '1984-06-09 00:00:00.000', null, '中国' , null)
insert into tb values(6 ,'天天', '男', '1979' , null, '中国' , '123123123123')
insert into tb values(7 ,'滴滴', '男', '1983' , null, '中国' , '123123123123')
go
--原始数据
select * from tb
/*
id name sex time age address phone
----------- ---------- ---------- ------------------------------ ----------- -------------------------------------------------- --------------------
1 张三 男 1984-01-15 00:00:00.000 25 中国 123123123123
2 李四 女 1987-06-26 00:00:00.000 22 中国 123123123123
3 王二 女 1984-11-25 00:00:00.000 25 NULL 123123123123
4 丫丫 男 1979-11-26 00:00:00.000 NULL 中国 123123123123
5 的的 女 1984-06-09 00:00:00.000 NULL 中国 NULL
6 天天 男 1979 NULL 中国 123123123123
7 滴滴 男 1983 NULL 中国 123123123123(所影响的行数为 7 行)
*/update tb
set age = (case when isdate(time) = 1 then datediff(yy , cast(time as datetime) , getdate())
else year(getdate()) - cast(time as int)
end)
from tb where age is null--更新后的数据
select * from tb
/*
id name sex time age address phone
----------- ---------- ---------- ------------------------------ ----------- -------------------------------------------------- --------------------
1 张三 男 1984-01-15 00:00:00.000 25 中国 123123123123
2 李四 女 1987-06-26 00:00:00.000 22 中国 123123123123
3 王二 女 1984-11-25 00:00:00.000 25 NULL 123123123123
4 丫丫 男 1979-11-26 00:00:00.000 30 中国 123123123123
5 的的 女 1984-06-09 00:00:00.000 25 中国 NULL
6 天天 男 1979 30 中国 123123123123
7 滴滴 男 1983 26 中国 123123123123(所影响的行数为 7 行)
*/drop table tb
呵呵 谢谢你
将 varchar 值 'NULL' 转换为数据类型为 int 的列时发生语法错误我的所有字段都是 varchar(哎,老板加的。。) 我吧age改成int的也是这个错
还有个问题请教 我的time 有些行也是空的....将 varchar 值 'NULL' 转换为数据类型为 int 的列时发生语法错误
create table tb(id int, name varchar(10), sex varchar(10), time varchar(30), age int,address varchar(50), phone varchar(20))
insert into tb values(1 ,'张三', '男', '1984-01-15 00:00:00.000', 25 , '中国' , '123123123123')
insert into tb values(2 ,'李四', '女', '1987-06-26 00:00:00.000', 22 , '中国' , '123123123123')
insert into tb values(3 ,'王二', '女', '1984-11-25 00:00:00.000', 25 , null , '123123123123')
insert into tb values(4 ,'丫丫', '男', '1979-11-26 00:00:00.000', null, '中国' , '123123123123')
insert into tb values(5 ,'的的', '女', '1984-06-09 00:00:00.000', null, '中国' , null)
insert into tb values(6 ,'天天', '男', '1979' , null, '中国' , '123123123123')
insert into tb values(7 ,'滴滴', '男', '1983' , null, '中国' , '123123123123')
insert into tb values(8 ,'东东', '男', null , null, '中国' , '123123123123') --加了条测试数据.go
--原始数据
select * from tb
/*
id name sex time age address phone
----------- ---------- ---------- ------------------------------ ----------- -------------------------------------------------- --------------------
1 张三 男 1984-01-15 00:00:00.000 25 中国 123123123123
2 李四 女 1987-06-26 00:00:00.000 22 中国 123123123123
3 王二 女 1984-11-25 00:00:00.000 25 NULL 123123123123
4 丫丫 男 1979-11-26 00:00:00.000 NULL 中国 123123123123
5 的的 女 1984-06-09 00:00:00.000 NULL 中国 NULL
6 天天 男 1979 NULL 中国 123123123123
7 滴滴 男 1983 NULL 中国 123123123123
8 东东 男 NULL NULL 中国 123123123123(所影响的行数为 8 行)*/update tb
set age = (case when isdate(time) = 1 then datediff(yy , cast(time as datetime) , getdate())
when ISNUMERIC(time) = 1 then year(getdate()) - cast(time as int)
else null
end)
from tb where age is null--更新后的数据
id name sex time age address phone
----------- ---------- ---------- ------------------------------ ----------- -------------------------------------------------- --------------------
1 张三 男 1984-01-15 00:00:00.000 25 中国 123123123123
2 李四 女 1987-06-26 00:00:00.000 22 中国 123123123123
3 王二 女 1984-11-25 00:00:00.000 25 NULL 123123123123
4 丫丫 男 1979-11-26 00:00:00.000 30 中国 123123123123
5 的的 女 1984-06-09 00:00:00.000 25 中国 NULL
6 天天 男 1979 30 中国 123123123123
7 滴滴 男 1983 26 中国 123123123123
8 东东 男 NULL NULL 中国 123123123123(所影响的行数为 8 行)
*/drop table tb