Several commonly used date functions of SQL Server (2)

Source: Internet
Author: User

in the previous section of SQL Server's commonly used date functions (1), we introduced the first two date functions in the table below. In this section, we will continue to learn the following three functions.

function

description

getdate ()

returns the current date and time

convert ()

display date/time in different formats

datediff ()

returns the time between two dates

datepart ()

Returns a separate part of the date/time

dateadd ()

add or subtract a specified interval from a date

3. datediff (datepart, startdate, enddate) Function
    • This function is used to calculate the day (year, month…) between two dates ...) Number, enddate minus startdate. If startdate is later than enddate, a negative value is returned. Startdate indicates the start time, enddate indicates the end time, And datepart indicates the calculation unit of the time interval, such as year, month, and day.DatepartThe following table lists the common parameters:
    • Datepart table:

DepartValue

Description

YY, yyyy

Year

QQ, Q

Quarter

Mm, m

Month

Dy, y

Day of the year

DD, d

Day

Wk, WW

Week of the year

DW, W

The day of the week, starting from Sunday.

HH

Hours

Mi, n

Minutes

SS, S

Seconds

MS

Millisecond

MCS

Subtle

NS

Nanoseconds

    • Instance: Search for users registered in the last week

Select * From userinfo

Where datediff (day, regtime, getdate () between 0 and 7

4. SQL Server datepart (Datepart,Date) Function
    • This function is used to return a separate part of the date/time, such as year, month, day, hour, and minute. WhereDateThe parameter is a valid date expression;DatepartThe parameters are shown in the table above.
    • Instance: view the registered year, month, and day of Cathy

Select datepart (yyyy, regtime) as year, datepart (mm, regtime) as month, datepart (DD, regtime) as date
From userinfo
Where susername = 'Cathy'

5. SQL Server dateadd (Datepart,Number,Date) Function
    • This function can calculate the time between adding or subtracting the corresponding time interval. WhereDateA parameter is a valid date expression.NumberIs the number of intervals you want to add. If you specify a decimal value, the decimal number is truncated without rounding. For future time, this number is a positive number. For past time, this number is a negative number. For the datepart parameter, see the list in datediff.
    • Instance:

Declare @ A Date

Set @ A = getdate ()

Select dateadd (day, 3, @)

Select dateadd (day, 9, @)

6. Quarterly query instances:

 

Declare @ Date Datetime
Set @ Date = Getdate ()
-- At the beginning of the quarter, calculate the first day of the quarter where the given date is located
Select Dateadd (Quarter, Datediff (Quarter, 0 , @ Date ), 0 ) As ' The first day of the current quarter '
-- Calculate the last day of the quarter of the given date at the end of the quarter
Select Dateadd (Quarter, 1 + Datediff (Quarter, 0 , @ Date ), - 1 ) As ' Last day of the current quarter '
-- Beginning of last quarter
Select Dateadd (Quarter, Datediff (Quarter, 0 , @ Date ) - 1 , 0 ) As ' The beginning of the last quarter of the current quarter '
-- Last quarter end
Select Dateadd (Quarter, Datediff (Quarter, 0 , @ Date ), - 1 ) As ' Last quarter of the current quarter '
-- Early next quarter
Select Dateadd (Quarter, 1 + Datediff (Quarter, 0 , @ Date ), 0 ) As ' The beginning of the next quarter of the current quarter '
-- End of next quarter
Select Dateadd (Quarter, 2 + Datediff (Quarter, 0 , @ Date ), - 1 ) As ' End of the next quarter '

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.