Similarities and differences of datename functions and DATEPART functions __ functions

Source: Internet
Author: User
Tags getdate

The similarities and differences between the DATENAME function and the DATEPART function in SQL functions:

Syntax: Datename ( datepart ,date ) and datepart ( datepart , date )

Description: The Datename function obtains the name, according to the different SQL engine version different and obtains the difference result;


Gets the name of the time, such as Get Week: Datename (wk,date) returns, the day of the week, such as Sunday,monday, and DATEPART (wk,date) returns the value, such as Monday, returns 2, Sunday is 1,

The same datename(datepart , date ) can obtain the monthly score, while datepart ( datepart , date ) /c11> to get the monthly value of the corresponding values, such as January 1.

When the name value of the time equals the number, two functions get the same value. such as: Datename (Dy,getdate ()) =datepart (Dy,getdate ()).


The following are the desirable values for the first parameter of the two function:

Take year: yy, yyyy;

Month Taken: m,mm

Season: Q,QQ

Take weeks: DW

Weeks: wk,w

Take the number of days of the year: DY

Day taken from date: DAY,DD

Time, minutes, seconds: hh,mi,ss

Description:weekday (DW) Date part returns the day of the week (Sunday, Monday, etc.). Quarter (QQ) returns the quarterly value, the first quarter of the year. DayOfYear (Dy) returns the day ordinal of a year.

Example:

In most SQL SERVER English versions (as well as some traditional versions),

SELECT datename (month, GETDATE ()) get January;

In the Simplified Chinese version, you get: 01

and select DATEPART (Month,getdate ()) gets 1 in all versions


The same is true when you take the value of a week.

In these versions, the use of the Datename function in the English version will get the most original value.

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.