Date Functions Flashcards
DATE()
DATE(expression)
Returns date
Returns a date given a number, string, or date <expression>.</expression>
If DATE() does not recognize the input as a date, what should you use instead?
DATEPARSE()
3 functions that changes data from string/integer/date into a date
(doesn’t change the date in any way like truncating or finding the difference)
DATE(expression)
DATE(“September 22, 2018”)
DATE(#2018-09-22 14:52#)
Remember: this returns just date and no time
DATEPARSE(date format, date string)
DATEPARSE(‘yyyy-MM-dd’, “2018-09-22”)
MAKEDATE(year, month, day)
MAKEDATE(2018,9,22)
required integer
DATEADD()
DATEADD(date_part, interval, date)
Returns date
Returns the <date> with the specified number <interval> added to the specified <date_part> of that date. For example, adding three months or 12 days to a starting date.</date_part></interval></date>
DATEADD(‘week’, 1, [due date])
Push out all due dates by one week
Results in date
Add 280 days to the date February 20, 2021
DATEADD(‘day’, 280, #2/20/21#)
Result: #November 27, 2021#
DATEDIFF()
DATEDIFF(date_part, date1, date2, [start_of_week])
Returns integer
Returns the number of date parts (weeks, years, etc) between two dates in units of <date_part></date_part>
Number of days between March 25, 1986 and February 20, 2021
DATEDIFF(‘day’, #3/25/1986#, #2/20/2021#)
Result: 12,751
Number of days between March 25, 1986 and February 20, 2021 (start of week is Sunday)
DATEDIFF(‘day’, #3/25/1986#, #2/20/2021#, ‘sunday’)
How many months someone was in a band?
Using [date left band] & [date joined band]
DATEDIFF(‘month’, [date joined band], [date left band])
DATENAME()
DATENAME(date_part, date, [start_of_week])
Returns the name of the specified date part as a discrete string.
Bonus: An inverse function is DATEPARSE, which takes a string value and formats it as a date.
DATENAME(‘year’, #3/25/1986#)
Result: “1986”
DATENAME(‘month’, #1986-03-25#)
Result: “March”
DATEPARSE()
DATEPARSE(date_format, date_string)
Returns specifically formatted strings as dates.
The <date_format> argument describes how the <date_string> field is arranged. Because of the variety of ways the string field can be ordered, the <date_format> must match exactly.</date_format></date_string></date_format>
Bonus: Inverse functions, which take dates apart and return the value of their parts, are DATEPART (integer output) and DATENAME (string output).
DATEPARSE(‘yyyy-MM-dd’, “1986-03-25”)
March 25, 1986
DATEPART()
DATEPART(date_part, date, [start_of_week])
Returns the name of the specified date part as an integer.
Bonus: An inverse function is DATEPARSE, which takes a string value and formats it as a date
DATEPART(‘year’, #1986-03-25#)
1986
DATEPART(‘month’, #1986-03-25#)
3
DATETRUNC()
DATETRUNC(date_part, date, [start_of_week])
Think “date rounding”
Output: Date
Changes the date, not the date format (i.e. doesn’t stop showing time)
Truncates the <date> to the accuracy specified by the <date_part>. This function returns a new date. (I.e. when you truncate a date that is in the middle of the month at the month level, this function returns the first day of the month.)</date_part></date>
Bc every date must have a value for day, month, quarter, and year, DATETRUNC sets the values as the lowest value for each date part up to the date part specified.
DATETRUNC(‘day’, #5/17/2022 3:12:48 PM#)
5/17/2022 12:00:00 AM
DATETRUNC(‘day’, #9/22/2018#)
9/22/2018
DATETRUNC(‘iso-week’, #9/22/2018#)
9/17/2018
(the monday of the week containing 9/22/2018)
DATETRUNC(quarter, #9/22/2018#)
7/1/2018
(the first day of the quarter containing 9/22/2018)
DAY()
DAY(date)
Returns the day of the month (1-31) as an integer.