search
HomeDatabaseMysql Tutorial【Sqlserver】修改数据库表中的数据:对缺失的数据根据已有的数

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 -- 查询时间

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

 1、sqlServer里面日期的加法

 

DATEADD

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

语法

DATEADD <strong>(</strong> <em>datepart </em><strong>,</strong> <em>number</em><strong>,</strong> <em>date </em><strong>) </strong>

参数

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'
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

MySQL: String Data Types and ENUMs?MySQL: String Data Types and ENUMs?May 13, 2025 am 12:05 AM

MySQloffersechar, Varchar, text, Anddenumforstringdata.usecharforfixed-Lengthstrings, VarcharerForvariable-Length, text forlarger text, AndenumforenforcingdataAntegritywithaetofvalues.

MySQL BLOB: how to optimize BLOBs requestsMySQL BLOB: how to optimize BLOBs requestsMay 13, 2025 am 12:03 AM

Optimizing MySQLBLOB requests can be done through the following strategies: 1. Reduce the frequency of BLOB query, use independent requests or delay loading; 2. Select the appropriate BLOB type (such as TINYBLOB); 3. Separate the BLOB data into separate tables; 4. Compress the BLOB data at the application layer; 5. Index the BLOB metadata. These methods can effectively improve performance by combining monitoring, caching and data sharding in actual applications.

Adding Users to MySQL: The Complete TutorialAdding Users to MySQL: The Complete TutorialMay 12, 2025 am 12:14 AM

Mastering the method of adding MySQL users is crucial for database administrators and developers because it ensures the security and access control of the database. 1) Create a new user using the CREATEUSER command, 2) Assign permissions through the GRANT command, 3) Use FLUSHPRIVILEGES to ensure permissions take effect, 4) Regularly audit and clean user accounts to maintain performance and security.

Mastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMay 12, 2025 am 12:12 AM

ChooseCHARforfixed-lengthdata,VARCHARforvariable-lengthdata,andTEXTforlargetextfields.1)CHARisefficientforconsistent-lengthdatalikecodes.2)VARCHARsuitsvariable-lengthdatalikenames,balancingflexibilityandperformance.3)TEXTisidealforlargetextslikeartic

MySQL: String Data Types and Indexing: Best PracticesMySQL: String Data Types and Indexing: Best PracticesMay 12, 2025 am 12:11 AM

Best practices for handling string data types and indexes in MySQL include: 1) Selecting the appropriate string type, such as CHAR for fixed length, VARCHAR for variable length, and TEXT for large text; 2) Be cautious in indexing, avoid over-indexing, and create indexes for common queries; 3) Use prefix indexes and full-text indexes to optimize long string searches; 4) Regularly monitor and optimize indexes to keep indexes small and efficient. Through these methods, we can balance read and write performance and improve database efficiency.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),