SQL - Dates
Date values are stored in date table columns in the form of a timestamp. A SQL timestamp is a record containing date/time data, such as the month, day, year, hour, and minutes/seconds. It's not much different from the standard date format.Date Columns:
Column Type | Format |
time | HH:MM:SS |
date | YYYY-MM-DD |
datetime | YYYY-MM-DD HH:MM:SS |
SQL SELECT GETDATE():
USE mydatabase; SELECT GETDATE();
Timestamp Result:
2004-06-22 10:33:11.840 |
Using a built in function, ISDATE() we can do some testing on date values to see if they meet the formatting requirements.
SQL Code:
USE mydatabase; SELECT ISDATE('8/24/08') AS "MM/DD/YY", ISDATE('2004-12-01') AS "YYYY/MM/DD";
SQL - Month(), Day(), Year()
The Month(), Day() and Year() functions all extract corresponding values from a given date.SQL Year():
USE mydatabase; SELECT YEAR(GETDATE()) as "Year"; SELECT YEAR('8/14/04') as "Year";
SQL Month():
USE mydatabase; SELECT MONTH(GETDATE()) as "Month"; SELECT MONTH('8/14/04') as "Month";
SQL Day():
USE mydatabase; SELECT DAY(GETDATE()) as "Day"; SELECT DAY('8/14/04') as "Day";
0 comments:
Post a Comment