Returns an integer that represents the specified date of a specified date. Returns the current week

Source: Internet
Author: User

Current week

W = weekday (date)
Lastday = dateadd ("D", 7-w, date)
Firstday = dateadd ("D", 1-w, date)

Or

Select IDS from news_external where datediff (wk, entry_date, getdate () = 0

 

 

Current month

Select IDS from news_external where datediff (M, entry_date, getdate () = 0

 

 

Datepart
Returns an integer that represents the specified date of a specified date.
Syntax
Datepart (datepart, date)
Parameters
Datepart
Is a parameter that specifies the date part to be returned. The following table lists the dates and abbreviations recognized by Microsoft SQL Server.
Abbreviation of date
Yearyy, yyyy
Quarterqq, Q
Monthmm, m
Dayofyeardy, y
Daydd, d
Weekwk, WW
Weekdaydw
Hourhh
Minutemi, n
Secondss, S
Millisecondms
The Week (wk, WW) date section reflects the changes made to the set datefirst. The year January 1 defines the start Number of the week date section, for example, datepart (wk, 'Jan 1, XXX') = 1, where XXXX represents any year.
Weekday (DW) returns the number of days in the week, for example, Sunday = 1 and Saturday = 7. The number of weekday dates depends on the set datefirst value. This command sets the first day of the week.
Date
Is an expression that returns the datetime, smalldatetime, or date format string. Datetime data type is used for dates after January 1, January 1, 1753. The earlier date is stored as character data. Always put the datetime value in quotation marks. Because smalldatetime is accurate to minutes, when smalldatetime is used, the second and millisecond are always 0.
If only the last two digits of the year are specified, the number of the last two digits smaller than or equal to the value of the "two-digit year deadline" configuration option is in the same century as that of the end year. The last two-digit number greater than the value of this option is in the century before the end of the century. For example, if two digit year cutoff is 2049 (default), 49 is interpreted as 2049,2050 as 1950. To avoid blurring, use a four-digit year.
For more information about the time value, see time format. For more information about the specified date, see datetime and smalldatetime.
Return type
Int
Note
The day, month, and year functions are synonyms of datepart (DD, date), datepart (mm, date), and datepart (YY, date.
Example
The getdate function returns the current date. However, the comparison does not always require complete date information (usually only a part of the date is compared ). This example shows the output of getdate and datepart.
Select getdate () as 'current date'
Go

The following is the result set:
Current date
---------------------------
Feb 18 1998 pm

Select datepart (month, getdate () as 'month number'
Go

The following is the result set:
Month number
------------
2

This example assumes that the date is July 22, May 29.
Select datepart (month, getdate ())
Go

The following is the result set:
-----------
5

(1 row (s) affected)

In this example, the date is represented by a number. Note: SQL Server interprets 0 as 01/01/1900.
Select datepart (M, 0), datepart (D, 0), datepart (YY, 0)

The following is the result set:
-----------------
1 1900

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.