MSSQL function Datename Use the example to explain (get the current month/year of the day of the first day of SQL statements) _mssql

Source: Internet
Author: User
Tags dateformat getdate mssql

MSSQL function Datename

Copy Code code as follows:

Grammar

Datename (datepart, date)
Parameters
DatePart
is part of the returned date. The following table lists all the valid datepart parameters. User-defined variable equivalents are not valid.

Date
is an expression that resolves to a time, date, smalldatetime, DateTime, datetime2, or DateTimeOffset value. Date can be an expression, a column expression, a user-defined variable, or a string literal.

To avoid uncertainty, use a four-digit year. For information about two-digit years, see the two digit year cutoff option.

return type
nvarchar

return value
Each datepart and its initials return the same value.

The return value varies depending on the locale used by the set LANGUAGE and the default language of the login. If date is a string literal in some format, the return value is determined by the set DateFormat. The set DateFormat does not affect the return value when a column expression of date or time data type is present.

How to get the current date, formatted with a dash, without a horizontal line, and how to get the current time.

How to get the month, quarter, day of the year ...

Copy Code code as follows:

SELECT CONVERT (varchar (), GETDATE (), 120)--Current month and day, example:2013-11-19
SELECT CONVERT (varchar (), GETDATE (), 112)--Current month and day, example:20131119
SELECT CONVERT (varchar (), GETDATE (), 108)--Current month and day, example:20:28:36
SELECT datename (Day, GETDATE ())--Date of the month
SELECT datename (month, GETDATE ())--month
SELECT datename (Quarter, GETDATE ())--Quarterly
SELECT datename (DayOfYear, GETDATE ())--Day of the year
SELECT Datename (Week,getdate ())--the first few weeks of the year
SELECT Datename (Weekday,getdate ())--Day of the week
SELECT Datename (Hour,getdate ())--current time, 24-hour
SELECT Datename (Minute,getdate ())--Take the current number of minutes
SELECT Datename (Second,getdate ())--Take the current number of seconds
SELECT Datename (Millisecond,getdate ())--milliseconds

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.