Query SQL statement with date added

Source: Internet
Author: User
Tags datetime time interval

Query SQL statement with date added

This is an SQL statement about the date added to the MSSQL access, which provides a large number of functions for the database tutorial date operation, and today our date is added using the DateAdd function for instance.

*/

Method one stored procedure

SET NOCOUNT ON

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

Insert @kp

Select 1,2, ' 2008-08-08 ' UNION ALL

Select 2,3, ' 2008-09-01 ' UNION ALL

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

Select Id,dateadd (MONTH,YS,SYRQ) as Syrq from @kp

Method two simple query statements

Select DATEADD (Month,cast (ys as int), SYRQ) as new date from KP

Method three DateAdd function date add

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

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

Sqlstr= "SELECT * from table where Time field =dateadd (d,5, ' 2008-08-08 ')"

Select DATEADD (D,5,cast (' 2008-08-08 ' as DateTime))

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

2008-08-13 00:00:00.000

2008-08-08 00:00:00.000

See a date Add SQL instance

Use pubs

Go

Select DATEADD (Day, pubdate) as timeframe

From titles

Go

/*

DateAdd function returns

Returns a Variant (date) that contains a date, plus a time interval.

Grammar

DATEADD (interval, number, date)

The following named arguments are in the DATEADD function syntax:

Partial description

Interval necessary. string expression, which is the time interval you want to add.

Number is necessary. Numeric expression, which is the number of time intervals to add. The value can be positive (get a future date), or it can be negative (get past date).

Date necessary. Variant (date) or text that represents a date, plus a time interval.

Set up

The interval parameter has the following set values:

Set description

YYYY the number of days of the year Q-m-month y

D-Day W week days WW week HH when n minutes s sec

*/

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.