sql - datepart
DATEPART() is a SQL function used to extract all kinds of date information from timestamps, and it is a function that is unique to Microsoft's SQL Server Application.
SQL Datepart:
USE mydatabase;
SELECT DATEPART(year, '2007-06-01') AS "Year";
SQL Results:
DATEPART() requires 2 parameters separated by a comma (,). The first parameter specifies what type of date data will be extracted, and the second parameter is a timestamp value.
SQL Datepart:
USE mydatabase;
SELECT DATEPART(year, '2007-06-01') AS "Year",
DATEPART(month, '2007-06-01') AS "Month",
DATEPART(day, '2007-06-01') AS "Day",
DATEPART(dayofyear, '2007-06-01') AS "DayofYear",
DATEPART(weekday, '2007-06-01') AS "Weekday";
SQL Results:
Year | Month | Day | DayofYear | Weekday |
2007 | 6 | 1 | 152 | 6 |
Datepart Abbreviation Chart:
DatePart | Abbreviation | Example |
year | yy, yyyy | DATEPART(yy, getdate()) |
quarter | qq, q | DATEPART(qq, getdate()) |
month | mm, m | DATEPART(mm, getdate()) |
dayofyear | dy, y | DATEPART(dy, getdate()) |
day | dd, d | DATEPART(dd, getdate()) |
week | wk, ww | DATEPART(wk, getdate()) |
weekday | dw | DATEPART(dw, getdate()) |
hour | hh | DATEPART(hh, getdate()) |
minute | mi | DATEPART(mi, getdate()) |
second | ss | DATEPART(ss, getdate()) |
millisecond | ms | DATEPART(ms, getdate()) |
No comments:
Post a Comment