server|sqlserver| function SQL Server commonly used time functions---GETDATE, getutcdate, datename SQL Server commonly used time functions---GETDATE, getutcdate, Datename Author: Amoy Net Date: 2005-4-12 15:53:24 Visits: Origin: Amoy-Net--http://www.tot.name display options: Automatic scrolling [left button stop]getdat
Transact-SQL referenceDatename
Returns the string representing the specified date part of the specified date.Syntax
DATENAME ( 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.
Date
Abbreviations
Year
YY,
The date and time functions in SQL Server are always problematic in this regard. So, I will summarize what I mainly useDatepart(Month, getdate (). This item can be set to day (now () in access. Unfortunately, the day () in SQL Server does not support parameters.Date and time functions in SQL Server
These scalar functions operate on the input values of date and ti
T-SQL time operations (dateadd, datediff, datepart, datename), datepartdatename
-- 1. current System Date and Time select getdate () -- 09:27:27. 277--2. the Time Operation dateadd returns the new datetime value dateadd (datepart, number, date) based on a period of time added to the specified date. Note: datepart: year (yy, yyyy) | quarter (qq, q) | month (mm, m) | week (ww, wk) | day (dd, dy) | hour (h) |
DATEADD (Ms,-3,dateadd (mm, DATEDIFF ()) + 1, 0))--. this week Monday select DATEADD (wk, DATEDIFF (Wk,0,getdate ()), 0)--. this week Sunday select DATEADD (Wk,datediff ( Wk,0,getdate ()), 6)--. the first day of the year select DATEADD (yy, DATEDIFF (Yy,0,getdate ()), 0)--. the last day of the year select DATEADD (ms,- 3,dateadd (yy, DATEDIFF (Yy,0,getdate ()) +1, 0))--. The first day of the quarter SELECT DATEADD (QQ, DATEDIFF (Qq,0,getdate ()), 0) --. Day of the Night SELECT DATEADD (
expression, user-defined variable, or string text.
To avoid uncertainty, use a four-digit year. For information about the two-digit year, see the two digit year cutoff option.
2. When datename and datepart are used to set datepart to weekday, they are displayed differently. And it depends on the day of the week.
CodeAs follows:
View code
1
Set
Datefirst
1
2
Select datename
time.
How to get the month, quarter, the day of the year...
Copy codeThe Code is as follows:Select convert (varchar (10), getdate (), 120) -- current year month day, Example: 2013-11-19Select convert (varchar (10), getdate (), 112) -- current year, month, and day, Example: 20131119Select convert (varchar (10), getdate (), 108) -- current year month day, Example: 20: 28: 36Select datename (day, getdate () -- number of the current monthSelect
1. Get the week (display Chinese characters such as: Monday)
Select datename (DW, getdate ())
2. Obtain the quarter
Select datename (QQ, getdate ())
3. Days passed this year
Select datename (dy, getdate ())
4. the week of the current year
Select datename (wk, getdate ())
The second parameter
SQL Server Date datename getdate function
GetDate () Function instance
You can call the use of the GETDATE () function in SQL Server 2005 's user customization function (udfs:user Defined functions), which is an example of the following example:
--Create a UDF now ()
CREATE FUNCTION Dbo.now ()
RETURNS DATETIME
As
BEGIN
Return (GETDATE ());
End;
Go
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 e
--getdate Current system date, timeSelect getdate()--2014-11-24 13:22:40.407--the CONVERT () function can display date/time data in different formatsSelect Convert(varchar( -),getdate(), at)--2014-11-24--DateDiff Returns the number of date and time boundaries across two specified datesSelect DateDiff( Day,'2014-11-24','2014-12-28')-- theSelect DateDiff(Month,'2014-11-24','2014-12-28')--1--datepart Returns an integer representing the specified date part of a specified dateSelect DatePart(Month,'2
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
The main use of the sql2005| function Datename function is to extract the specified part of the data from the date, such as the year we want in the current date, the month, and so on. The return type is nvarchar.
The specific syntax is as follows:
Datename (param,date);
Param is the parameter that specifies the part of the date to return, including the following:
Date Part
abbreviation
SELECT DATEDIFF (year, ' 1992-11-28 ', GETDATE ())--age, where year can be replaced by day MONTHSELECT DATEPART (Month,getdate ())-Returns an integer representing the specified date portion of the specified date. such as 12SELECT datename (WEEKDAY, ' 2014-11-12 ')--Returns the specified day of the week, such as Monday,SELECT Datename (Weekday,getdate ())--Returns the current date is the day of the weekSELEC
type in SQL the default format for datetime is YYYY-MM-DD hh:mi:ss:mmm, but most of the time we just want to get his date part, not the later. The previous article mentions using the Datename () function to intercept a date that does not contain a time part, and now it is more simple to get to a date that does not contain time! Use the CONVERT () function: Select CONVERT (char), GetDate (), as Date * The 3
.asp?id=1210302
The book belongs to the story:
DECLARE @Birthday smalldatetime
Set @Birthday = ' 1949-6-5 '
--use more than one date variable, in order to expand: To calculate the future or a moment in time of the exact age of the function!
DECLARE @ smalldatetime
SET @ = GETDATE ()
Select DateDiff (year, @Birthday, @)
+ Case when DateAdd (Year,datediff, @Birthday, @), @Birthday) Then 1
else 0
End
Again the first one:
1. Calculate the number of Monday to Sunday between any two dates!
The ca
value plus a new date after a time period. A partial value of the addition operation in the specified date of the DP, for example: Year,month,day,hour,minute,second,millsecond, num Specifies the value that is added to the DP, and if the value is a non-integer value, the fractional part of the value is discarded, D is the date on which the addition operation was performed. SELECT DATEADD (year,1, ' 2015-04-30 01:01:01 ') as Yearadd,DATETIME (month, 2, ' 2015-04-30 01:01:01 ') as Weekdayadd,DATEA
I have added SQL time and date functions to my favorites. If you need these functions, please refer to the following section.
I have added SQL time and date functions to my favorites. If you need these functions, please refer to the following section.
① Getdate (): returns the current system date and time.
The Code is as follows:
Current system time = GETDATE ()
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.