日期:2014-05-17  浏览次数:20435 次

sql循环替换
数据库表stu_Info表中有一列info_Time,怎么将前面的日期2012-11-22循环替换成2012-11-23啊? 谢谢

info_Time 
2012-11-22 13:54:56.000
2012-11-22 13:57:12.000
2012-11-22 13:58:42.000
2012-11-22 13:58:48.000
2012-11-22 14:00:05.000
2012-11-22 14:03:14.000
2012-11-22 14:03:50.000
2012-11-22 09:48:26.000
2012-11-22 11:00:36.000
2012-11-22 11:00:36.000
------解决方案--------------------
select info_time, dateadd(dd,1,info_time) from stu_Info

------解决方案--------------------
----------------------------
-- Author  :TravyLee(物是人非事事休,欲语泪先流!)
-- Date    :2012-11-23 08:57:22
-- Version:
--      Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86) 
-- Jul  9 2008 14:43:34 
-- Copyright (c) 1988-2008 Microsoft Corporation
-- Developer Edition on Windows NT 6.1 <X86> (Build 7601: Service Pack 1)
--
----------------------------
--> 测试数据:[test]
if object_id('[test]') is not null drop table [test]
go 
create table [test]([info_Time] datetime)
insert [test]
select '2012-11-22 13:54:56.000' union all
select '2012-11-22 13:57:12.000' union all
select '2012-11-22 13:58:42.000' union all
select '2012-11-22 13:58:48.000' union all
select '2012-11-22 14:00:05.000' union all
select '2012-11-22 14:03:14.000' union all
select '2012-11-22 14:03:50.000' union all
select '2012-11-22 09:48:26.000' union all
select '2012-11-22 11:00:36.000' union all
select '2012-11-22 11:00:36.000'

select * from [test]
go

--更新

update test
set [info_Time]=dateadd(dd,1,[info_Time])

--直接查询
select dateadd(dd,1,[info_Time]) as [info_Time] from test

/*
info_Time
-----------------------
2012-11-23 13:54:56.000
2012-11-23 13:57:12.000
2012-11-23 13:58:42.000
2012-11-23 13:58:48.000
2012-11-23 14:00:05.000
2012-11-23 14:03:14.000
2012-11-23 14:03:50.000
2012-11-23 09:48:26.000
2012-11-23 11:00:36.000
2012-11-23 11:00:36.000

(10 行受影响)


*/

------解决方案--------------------

若果是datetime类型
update stu_Info set info_Time=info_Time+1

如果是string,varchar类型
update stu_Info set info_Time=replace(info_Time,'2012-11-22','2012-11-23')

------解决方案--------------------
if object_id('[stu_info]') is not null drop table [stu_info]
go
create table [stu_info] (info_Time datetime)
insert into [stu_info]
select '2012-11-22 13:54:56.000' union all
select '2012-11-22 13:57:12.000' union all
select '2012-11-22 13:58:42.000' union all
select '2012-11-22 13:58:48.000' union all