Datepart () : Returns an integer representing the specified date part of the specified date
Syntax: Datepart (datepart, date) Return type: int
DateName (): returns a string representing the specified date part of the specified date
Syntax: DateName(
datepart,date
)
返回类型:nvarchar
Date part | abbreviation | Remarks |
---|---|---|
year | yy, yyyy | year |
quarter | qq, q | Quarter |
month | mm, m | month |
dayofyear | dy, y | Day of the year |
day | dd, d | day |
week | wk, ww | Week of the year |
weekday | The dw date part returns the number corresponding to a day of the week, for example: Sunday = 1 | Day of the week |
Hour | hh | hour |
minute | my n | minute |
second | ss, s | second |
millisecond | ms | millisecond |
select GETDATE () as 'Current date and time', DateName(year,GetDate())+'-'+DateName(month,GetDate())+'-'+DateName(day,GetDate()) as '当前日期', DateName (quarter, GetDate ()) as 'first quarter', DateName (week, GetDate ()) as 'Week of the year', DateName (DAYOFYEAR, GetDate ()) as 'Day of the year', DateName(year,GetDate()) as '年', DateName(month,GetDate()) as '月', DateName(day,GetDate()) as '日', DateName(hour,GetDate()) as '时', DateName(minute,GetDate()) as '分', DateName(second,GetDate()) as '秒', DateName(MILLISECOND,GetDate()) as '豪秒', DateName (WEEKDAY, GetDate ()) as 'Day of the week' select GETDATE () as 'Current date and time', DatePart(year,GetDate())+'-'+DatePart(month,GetDate())+'-'+DatePart(day,GetDate()) as '当前日期', DatePart (quarter, GetDate ()) as 'first quarter', DatePart (week, GetDate ()) as 'Week of the year', DatePart (DAYOFYEAR, GetDate ()) as 'Day of the year', DatePart(year,GetDate()) as '年', DatePart(month,GetDate()) as '月', DatePart(day,GetDate()) as '日', DatePart(hour,GetDate()) as '时', DatePart(minute,GetDate()) as '分', DatePart(second,GetDate()) as '秒', DatePart(MILLISECOND,GetDate()) as '豪秒', DatePart (WEEKDAY, GetDate ()) as 'Day of the week'
search result:
Question: Why is the current date returned by DatePart 2023 instead of 2018-01-4?
note:
1)
Because the return type of DatePart is int, the result of the current date is the result of the operation
2)
In most English versions of SQL SERVER (and some traditional versions),
SELECT DATENAME (month, getdate ()) gets the string type January;
And in the simplified Chinese version: SELECT DATENAME (month, getdate ()) gets the string type 01
And SELECT DATEPART (month, getdate ()) in all versions get int type 1
3)
SELECT DATENAME (weekday, getdate ()) gets "week X"
SELECT DATEPART (weekday, getdate ()) gets the corresponding number of the week, one (1) / two (2) / three (3).