Download presentation
Presentation is loading. Please wait.
1
Date Functions Farrokh Alemi, Ph.D.
In this section we discuss several date functions in SQL. This brief presentation was organized by Dr. Alemi.
2
Before Rx: Comorbidity
Cross Join Before Rx: Comorbidity After Rx: Complication Dates Matter In the electronic health records, all entries are date and time stamped. In analysis of data, dates play an important role. For example, diseases that follow a treatment might be considered complications and diseases that precede it might be considered a comorbidity. The same disease at two different times has different implications for the analysis. In this slides we describe several functions used for manipulation of dates.
3
Calculation of Age Cancer Survival Days Dates Matter Cross Join
Calculating age of a patient requires finding the difference between birth date and current date. Calculation of survival dates requires comparison of date of death and date of cancer treatment. In all of these calculations, we are examining dates and manipulating them. To facilitate the manipulation of dates, SQL has several date functions.
4
GETDate DatePart DateDiff Cross Join
In this section we go over three date functions: Getdate, DatePart, and DateDiff .
5
Cross Join Current Date Function
Let us start with getdate function. One of the most common functions is the getdate function. This function produces the current date.
6
Cross Join Current Date Function
The function has no arguments within the parentheses.
7
Cross Join Current Date Function
If we execute the command we get the result indicated. At the time of making this video the current date was June 14th The time was 2 pm 48 minutes 36 second and 563 nanoseconds.
8
Cross Join Date Add Function
The DateAdd function is a useful function. We now present an example for use of DateAdd function. The date add function increases or decreases a starting date by a fixed time interval.
9
DateAdd (Interval, Number, From)
Cross Join DateAdd (Interval, Number, From) Date Add Function The syntax of the date function is as provided in this slide.
10
DateAdd (Interval, Number, From)
Cross Join DateAdd (Interval, Number, From) hh dd mm qq yy Date Add Function The interval indicates if we are adding hours, days, month, quarter or year. A twice repeated h, d, m, q or y indicate the interval. For example, dd indicates we want to add days and yy indicates we want to add years.
11
DateAdd (Interval, Number, From)
Cross Join DateAdd (Interval, Number, From) Date Add Function The number indicates how many intervals should be added.
12
DateAdd (Interval, Number, From)
Cross Join DateAdd (Interval, Number, From) Date Add Function The From parameter gives the starting date and time.
13
Cross Join Date Add Function
Let us look at an example. In this code we are getting the current date in the first line.
14
Cross Join Date Add Function
Then we want to calculate the date of a week from the current date. In the second line, we are adding seven days
15
Cross Join Date Add Function
to the current date.
16
Cross Join Date Add Function
The resulting output indicates that at the time of preparation of these slides the current date was June 14th
17
Cross Join Date Add Function
and after dateadd function was executed, it calculated a week from current date to be June 21st. So we see that dateadd function added the specified number of days to the starting date.
18
Cross Join Date Add Function
Here we see a dateadd function used to calculate 2 years prior current date. To go back we need a negative number. We are using yy for the interval and for the number of intervals we are using -2 so that the computer will calculate 2 years back.
19
Cross Join Date Add Function
The result takes us back to June 14th 2016 from June 14th 2018.
20
Cross Join Date Part Function
Now we examine how to pull apart a date or time and select just part of it. The command we are using is called datepart.
21
Cross Join Part of Date Function
Here is an example of datepart function in use.
22
Cross Join Part of Date Function
hh dd mm yy Part of Date Function It has two parameters. The first parameter selected which part is needed. If we put in yy, we are indicating that we want to get the part of the date that is year. Other parameters are also possible. We could get the hour, the seconds, the month and so on.
23
Cross Join Part of Date Function
The second parameter indicates the column where the date can be found. Here we are dissecting the current date into its parts. If you wanted part of a different column, you would replace the getdate function in this statement with the name of the column in your data.
24
Cross Join Part of Date Function
Once we run the datepart we always obtain an integer. Here we have found that the year within the current date was 2018.
25
Cross Join Part of Date Function
Here we see an example command that gives the current month. The mm code says that we are interested to get the month part of the date.
26
Cross Join Part of Date Function
Note that the output of datepart is always an integer. So the output here 6 indicates the 6th month or June.
27
Cross Join Date Diff Function
A very common date function is date difference function. This function calculates the difference between two dates.
28
Date Difference Function
Cross Join DATEDIFF (datepart, expression1, expression2) Date Difference Function It has three arguments, datepart and two expressions involving dates.
29
Date Difference Function
datepart abbreviation year yy, yyyy quarter qq, q month mm, m dayofyear dy, y day dd, d week wk, ww hour hh minute mi, n second ss, s millisecond ms microsecond mcs nanosecond ns TZoffset tz ISO_WEEK isowk, isoww Cross Join DATEDIFF (datepart, expression1, expression2) Date Difference Function The datepart indicates the units in which the difference should be expressed. It can be any unit such as years or all the way down to nanoseconds.
30
Date Difference Function
Cross Join DATEDIFF (datepart, expression1, expression2) Date Difference Function Expression 1 and expression 2 are expressions involving manipulations of columns of dates, times or combined date and time.
31
Date Difference Function
Cross Join Date Difference Function This select command finds the current age of a patient born in September 8th It expresses the difference between date of birth and current date as age in units of years.
32
Date Difference Function
Cross Join Date Difference Function Running this select command results in estimated age of 64.
33
Month & Days Are Ignored
Cross Join Month & Days Are Ignored Common Problems A common error in date calculations is that they are carried out at the unit specified and all the rest of the information are ignored. Here we are taking the difference of current year 2018 and year of birth of 1954 and ignoring that this patient will not be 64 until September 8th of He is really 63 and 6 months right now but the SQL is calculating his age as 64 years. Date difference is always calculated at the unit level specified and the rest is ignored, so one second in the next year will look like one more year. oops
34
Date Format Cross Join Common Problems
A common problem with date calculations is the format of the column. Many columns are not in proper date format and must be converted or cast as date before calculations can be done.
35
Looks like a date but it is not
Date Format Cross Join Common Problems Looks like a date but it is not The entry within quotes is a text field. It looks like a date, but it is not. It is a text field. The text needs to be converted. But conversion is not so easy. SQL prevents conversions from text to date, precisely because it is fraught with data distortions.
36
Ambiguous Cross Join Common Problems Date Format
The entry may be ambiguous as a date. We do not know here that we are talking about May 1st or January 5th.
37
May contain errors May contain errors
Date Format Cross Join Common Problems May contain errors May contain errors Worse yet, it may contain entries that do not make sense, e.g. 32 day month or it may have misspelled month or even a illogical entry like the words “I do not know.” Before conversion, the analyst must make sure that all values are sensible for conversion to a date.
38
CONVERT(data_type(length), expression, style)
Date Format Cross Join CONVERT(data_type(length), expression, style) Common Problems The CONVERT() function converts an expression from one data type to another data type. The general command requires one to specify a data type. Numerous data types are allowed including date, variable character, integer, or float.
39
CONVERT(data_type(length), expression, style)
Date Format Cross Join CONVERT(data_type(length), expression, style) Common Problems Length is an optional command needed mostly for variable character data types. A variable character data type is a text field with a fixed length.
40
CONVERT(data_type(length), expression, style)
Date Format Cross Join CONVERT(data_type(length), expression, style) Common Problems Expression is the column or manipulation of the column that needs to be converted.
41
CONVERT(data_type(length), expression, style)
Date Format Cross Join CONVERT(data_type(length), expression, style) Common Problems Style Input Standard 100 mon dd yyyy hh:miAM/PM Default 101 mm/dd/yyyy US Style is optional and there are many different fixed style formats. Styles 100 and 101 are of particular interest as these are common format for dates, a complicated data entry style.
42
Cross Join Common Problems
1st conversion Common Problems If we want to convert text to date, we need to do it in two steps. We first covert the text into variable character, here of length 30 and using style This truncates the text field to 30 character, something more manageable by the computer. Style 101 also reads the date correctly.
43
Cross Join Common Problems
2nd conversion Cross Join 1st conversion Common Problems We then convert the variable character string into date time format.
44
Cross Join Common Problems
Now real date Common Problems The result is a reformatted text as date. Note that now nanoseconds are included in the date time format. Since no nanoseconds were specified in the text they are assumed to be zero nanoseconds.
45
Cross Join Common Problems
The hard part of working with date functions is the conversion of string or text input into date formats.
46
Date Functions are Needed in analysis of data from EHR
Analysis of dates is common when analyzing data from Electronic Health Records. Such an analysis requires the conversion of text to date and use of date functions.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.