【Sqlserver】修改数据库表中的数据:对缺失的数据根据已有的数据进行修补

时间:2021-11-11 00:54:35

1
--查询时间范围内的数据 2 select * from dbo.point where wtime >'2014-05-01 23:59:59' and wtime< '2014-05-02 00:40:42.000'
3
4
5 delete from point where wtime >'2014-05-01 23:59:59' and wtime< '2014-05-02 00:40:42.000'
6
7 --查询时间范围内的数据,并插入到临时表中
8 select * into a_temp from point where wtime between '2014-05-01 00:00:42.000' and '2014-05-01 00:40:42.000'
9
10 --查询临时表内的数据
11 select * from a_temp
12
13
14 --更新临时表的时间
15 update a_temp set wtime=DATEADD(day, 1, wtime)
16
17 --把临时表中的数据插入到表中
18 insert into point select pointid,pointtype,pointvalue,pointunit,pointvalue2,pointunit2,wtime from a_temp

 1、sqlServer里面日期的加法

 

DATEADD

在向指定日期加上一段时间的基础上,返回新的 datetime 值。

语法

DATEADD ( datepart , number, date )

参数

datepart

是规定应向日期的哪一部分返回新值的参数。下表列出了 Microsoft® SQL Server™ 识别的日期部分和缩写。

日期部分 缩写
Year yy, yyyy
quarter qq, q
Month mm, m
dayofyear dy, y
Day dd, d
Week wk, ww
Hour hh
minute mi, n
second ss, s
millisecond ms

 

number

是用来增加 datepart 的值。如果指定一个不是整数的值,则将废弃此值的小数部分。例如,如果为 datepart 指定 day,为 number 指定 1.75,则 date 将增加 1。

date

是返回 datetime 或 smalldatetime 值或日期格式字符串的表达式。有关指定日期的更多信息,请参见 datetime 和 smalldatetime

如果您只指定年份的最后两位数字,则小于或等于"两位数年份截止期"配置选项的值的最后两位数字的数字所在世纪与截止年所在世纪相同。大于该选项的值的最后两位数字的数字所在世纪为截止年所在世纪的前一个世纪。例如,如果 two digit year cutoff 为 2049(默认),则 49 被解释为 2049,2050 被解释为 1950。为避免模糊,请使用四位数的年份。

返回类型

返回 datetime,但如果 date 参数是 smalldatetime,返回 smalldatetime。

 

2、

使用 INSERT...SELECT 插入行

INSERT 语句中的 SELECT 子查询可用于将一个或多个其它的表或视图的值添加到表中。使用 SELECT 子查询可同时插入多行。

下面的 INSERT 语句将 titles 中数据的 type 是 modern cooking 的所有行的数据插入到一个单独的表中:

USE pubs
INSERT INTO MyBooks
SELECT title_id, title, type
FROM titles
WHERE type = 'mod_cook'