SQL Server Date function

Source: Internet
Author: User
Tags date1

1. Date and time functions in SQL Server

1. Current system date, time
Select GETDATE ()

2. DateAdd returns a new datetime value based on adding a period of time to the specified date

Example: Add 2 days to the date
Select DATEADD (day,2, ' 2004-10-15 ')--return: 2004-10-17 00:00:00.000

3. DateDiff returns the number of date and time boundaries across two specified dates.

Select DateDiff (Day, ' 2004-09-01 ', ' 2004-09-18 ')--return: 17

4. DatePart returns an integer representing the specified date portion of the specified date.

Select DATEPART (Month, ' 2004-10-15 ')--return 10

5. Datename returns a string representing the specified date part of the specified date

Select Datename (Weekday, ' 2004-10-15 ')--return: Friday

6. Day (), month (), year ()--can be compared with datepart

Select Current date =convert (varchar), GETDATE (), 120)
, Current time =convert (varchar (8), GETDATE (), 114)

Select Datename (DW, ' 2004-10-15 ')

Select how many weeks of the year =datename (week, ' 2004-10-15 ')
, today is a few weeks =datename (weekday, ' 2004-10-15 ')

Second, SQL date format conversion

Select CONVERT (varchar, GETDATE (), 120)
2004-09-12 11:06:08
Select Replace (replace (CONVERT (varchar, GETDATE (), 120), '-', '), ', '), ': ', ')
20040912110608

Select CONVERT (varchar), GETDATE (), 111)
2004/09/12

Select CONVERT (varchar), GETDATE (), 112)
20040912

Select CONVERT (varchar), GETDATE (), 102)
2004.09.12

other date format conversion methods that I don't use:

Select CONVERT (varchar), GETDATE (), 101)
09/12/2004

Select CONVERT (varchar), GETDATE (), 103)
12/09/2004

Select CONVERT (varchar), GETDATE (), 104)
12.09.2004

Select CONVERT (varchar), GETDATE (), 105)
12-09-2004

Select CONVERT (varchar), GETDATE (), 106)
12 09 2004

Select CONVERT (varchar), GETDATE (), 107)
09 12, 2004

Select CONVERT (varchar), GETDATE (), 108)
11:06:08

Select CONVERT (varchar), GETDATE (), 109)
09 12 2004 1

Select CONVERT (varchar), GETDATE (), 110)
09-12-2004

Select CONVERT (varchar), GETDATE (), 113)
12 09 2004 1

Select CONVERT (varchar), GETDATE (), 114)
11:06:08.177


Example:
1.GetDate () for SQL Server:select GetDate ()

2.DateDiff (' s ', ' 2005-07-20 ', ' 2005-7-25 22:56:32 ') returns a value of 514,592 seconds
DateDiff (' d ', ' 2005-07-20 ', ' 2005-7-25 22:56:32 ') returns a value of 5 days

3.DatePart (' W ', ' 2005-7-25 22:56:32 ') returns a value of 2 that is Monday (Sunday is 1, Saturday is 7)
DatePart (' d ', ' 2005-7-25 22:56:32 ') returns a value of 25, or 25th
DatePart (' y ', ' 2005-7-25 22:56:32 ') returns a value of 206 that is the No. 206 Day of the Year
DatePart (' yyyy ', ' 2005-7-25 22:56:32 ') returns a value of 2005 that is 2005

the specific syntax:

Date functions are used to manipulate data of datetime and smalldatetime types and perform arithmetic operations. As with other functions, you can
The SELECT and WHERE clauses of the SELECT statement are used with the date function in an expression. Here's how to use it:

A date function parameter in which the number of arguments should be different from the function.

· Day ()

The day () function syntax is as follows:
Day (<date_expression>)
The day () function returns the date value in Date_expression.

· MONTH ()

The MONTH () function syntax is as follows:
MONTH (<date_expression>)
The month () function returns the months value in Date_expression.
Unlike the day () function, an integer value of 1 is returned when the argument to the MONTH () function is an integer, that is, SQL Server considers its
is January 1900.

· Year ()

The year () function syntax is as follows:
Year (<date_expression>)
The year () function returns the years value in Date_expression.
Reminder: When using a date function, its date value should be between 1753 and 9999, which is the date that the SQL Server system can recognize
Otherwise, errors may occur.

· DATEADD ()

The DATEADD () function syntax is as follows:
DATEADD (&LT;DATEPART&GT;, <number>, <date>)
The DATEADD () function returns a new date that is generated by the specified date, plus the specified extra date interval number. Parameter "DatePart
Often used in date functions, it is used to specify the components that make up the date type data, such as year, quarter, month, day, week, and so on.
The values are as shown in table 4-9:

· DATEDIFF ()

The DATEDIFF () function syntax is as follows:
DATEDIFF (&LT;DATEPART&GT;, <date1>, <date2>)
The DATEDIFF () function returns the difference in the datepart aspect of the two specified date, that is, the date2 exceeds the date1 gap value, which
The result value is an integer value with a positive sign. For different datepart, the maximum gap value allowed by the DATEDIFF () function is not
, such as: When DatePart is second, the maximum gap value allowed by the DATEDIFF () function is 68: The year datepart is
Millisecond, the maximum gap allowed by the DATEDIFF () function is 24 days, 20 hours, 30 minutes, 23 seconds, 647 milliseconds.

· Datename ()

The Datename () function syntax is as follows:
Datename (&LT;DATEPART&GT;, <date) >
The Datename () function returns the specified portion of the date as a string in this section. specified by DatePart.

· DATEPART ()

The DATEPART () function syntax is as follows:
DATEPART (&LT;DATEPART&GT;, <date>)
The DATEPART () function returns the specified part of the date as an integer value. This section is specified by DatePart.
DATEPART (dd, date) is equivalent to day (date)
DATEPART (mm, date) equals month (date)
DATEPART (yy, date) is equivalent to year (date)

· GETDATE ()

The GETDATE () function syntax is as follows:
GETDATE ()
The GETDATE () function returns the current date and time of the system in the default format of DateTime, which is often used as a parameter to other functions or commands.

In the development database application, you often encounter problems with processing time, such as querying the records for a specified time. Here are some common questions
, combined with some of their own experience, and everyone to discuss this kind of problem.

Let's start by introducing the usage of several main functions of processing time in SQL Server:

GETDATE () function: Gets the current date and time of the system. The return value is of type DateTime.

Usage: getdate ()
Example:

Select GETDATE () as Dte,dateadd (Day,-1,getdate ()) as Nowdat
Output Result:
DTE Nowdat
1999-11-21 19:13:10.083 1999-11-20 19:13:10.083
(1 row (s) affected)
DatePart () Function: Returns the specified part of the time as an integer.
Usage: DATEPART (datepart,date)
Parameter description: The part of the time to return when datepart, used to value year, month, day, hour, minute.
The date is the specified time.
Example:
SELECT DATEPART (month, GETDATE ()) as ' month number '
Output Result:
Month number
11
(1 row (s) affected)
DATEADD () Function: Returns a new time value by adding an integer value to the specified part of the specified time.
Usage: DATEADD (datepart,number,date)
Parameter description: DatePart (IBID.)
Date (IBID.)
Number to increment value, integer, positive negative, positive value returns the time values after date, negative value returns date
The previous time value
Example:
Select GETDATE () as Today
Select DATEADD (Day,-1,getdate ())
Select DATEADD (Day,1,getdate ())
Output:
Today
1999-11-21 19:42:41.410
(1 row (s) affected)
Yesterday
1999-11-20 19:42:41.410
(1 row (s) affected)
Tomorrow
1999-11-22 19:42:41.410
(1 row (s) affected)
DateDiff () Function: Returns a difference of two time to the specified time portion. Returns an integer value. such as 1991-6-12 and 1991-6-21
Between the days
To calculate the difference of 9 days, 1998-6-12 and 1999-6-23 by year difference 1 years, 1999-12-1 and 1999-3-12 by month difference 9 months
Usage: DateDiff (DAREPART,DATE1,DATE2)
Parameter description: DatePart (IBID.)
Date1, Date2 (ibid. date)
Example:
Select DateDiff (Month, ' 1991-6-12 ', ' 1992-6-21 ') as a

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.