SQL statement for date addition Query

Source: Internet
Author: User

SQL statement for date addition Query

This is an SQL statement that describes the date addition of mssql access. It provides a large number of functions for database tutorial date operations. Today, we use the dateadd function to add dates.

*/

// Method 1 stored procedure

Set nocount on

Declare @ kp table ([id] int, [ys] int, [syrq] datetime)

Insert @ kp

Select 1, 2, '2017-08-08 'union all

Select 2, 3, '2014-09-01 'union all

Select 3, 33, '2017-08-11'

Select id, dateadd (month, ys, syrq) as syrq from @ kp

// Method 2 simple query statement

Select dateadd (month, cast (ys as int), syrq) as new date from kp

// Method 3 dateadd function date Addition

Dateadd (d, 5, "2008-08-08 ")

Dateadd (d, 5, "2008-08-08 ")

Sqlstr = "select * from table where time field = dateadd (d, 5, '2017-08-08 ')"

Select dateadd (d, 5, cast ('2017-08-08 'as datetime ))

Select cast ('2017-08-08 'as datetime)

2008-08-13 00:00:00. 000

2008-08-08 00:00:00. 000

Add SQL instances by date

Use pubs

Go

Select dateadd (day, 21, pubdate) as timeframe

From titles

Go

/*

Returns the dateadd function.

Returns the variant (date) that contains a date. This date is added with a time interval.

Syntax

Dateadd (interval, number, date)

The dateadd function syntax includes the following naming parameters:

Partial description

Interval is required. String expression, which is the time interval to be added.

Number is required. A numeric expression is the number of time intervals to be added. The value can be a positive number (to get the future date) or a negative number (to get the past date ).

Date is required. Variant (date) or text that represents the date, which is added with the time interval.

Set

The interval parameter has the following settings:

Set description

Yyyy Year q quarter m month y day of a year

D, w, the number of days in a week, ww, hh, n minutes, s seconds

*/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.