Removed images and added text tables (#35900)

This commit is contained in:
Uma Sankar
2019-06-28 12:51:21 +05:30
committed by Randell Dawson
parent b18560f8d6
commit 1f6b3c082d
13 changed files with 884 additions and 366 deletions

View File

@@ -12,57 +12,114 @@ We will cover:
* Date Math
* Dates in a where or having clause
### Getting the current date
### Let's Get Started :
* ### Getting the current date :
Getting the date from the system can be very handy for processing data using SQL.
Getting the date from the system can be very handy for processing data using SQL.
```sql
-- current date
select now(), sysdate(), current_date(), current_time(), -- date and time from the system on execution
dayofyear(now()) as NumDaysSoFarThisYr,
EXTRACT(YEAR FROM now()) as theYearPart,
EXTRACT(YEAR_MONTH FROM now()) as theYrMonPart,
date_format(now(), '%W %M %Y') as oneOfManyFormats;
;
```
```sql
-- current date
select now(), sysdate(), current_date(), current_time(), -- date and time from the system on execution
dayofyear(now()) as NumDaysSoFarThisYr,
EXTRACT(YEAR FROM now()) as theYearPart,
EXTRACT(YEAR_MONTH FROM now()) as theYrMonPart,
date_format(now(), '%W %M %Y') as oneOfManyFormats;
;
```
Result :
```text
+--------------------+----------------------+
| now() | 2019-04-27 18:06:48 |
| sysdate() | 2019-04-27 18:06:48 |
| current_date() | 2019-04-27 |
| current_time() | 18:06:48 |
| NumDaysSoFarThisYr | 117 |
| theYearPart | 2019 |
| theYrMonPart | 201904 |
| oneOfManyFormats | Saturday April 2019 |
+--------------------+----------------------+
```
![image-1](https://github.com/SteveChevalier/guide-images/blob/master/date-functions04.JPG)
In SQL query, we see the following:
* The first two columns in the result are two ways to get the same information: the date on the system at the time the SQL is executed.
* The next two columns slice out just the Date and Time parts of the system date.
* The next one presents the "day number" of the system date in this year. You'll notice that this is one day more than the math shown in the next example.
* The next two extract just the year and then both the year and month
* Last, but not least, there is a single example of one of the many ways to format this dates.
In SQL query, we see the following:
* The first two columns in the result are two ways to get the same information: the date on the system at the time the SQL is executed.
* The next two columns slice out just the Date and Time parts of the system date.
* The next one presents the "day number" of the system date in this year. You'll notice that this is one day more than the math shown in the next example.
* The next two extract just the year and then both the year and month
* Last, but not least, there is a single example of one of the many ways to format this dates.
* ### Date Math :
### Date Math
```sql
select now(), current_date(),
datediff(now(),'2017-01-01') as daysThisYear,
subdate(current_date(), interval 150 day) as '150DaysAgo',
adddate(now(), interval 7 day) as dateInA_Week -- date in a week
;
```
```text
+------------------+----------------------+
| now() | 2019-04-27 18:19:59 |
| current_date() | 2019-04-27 |
| daysThisYear | 846 |
| 150DaysAgo | 2018-11-28 |
| dateInA_Week | 2019-05-04 18:19:59 |
+------------------+----------------------+
```
```sql
select now(), current_date(),
datediff(now(),'2017-01-01') as daysThisYear,
subdate(current_date(), interval 150 day) as '150DaysAgo',
adddate(now(), interval 7 day) as dateInA_Week -- date in a week
;
```
Here we see:
* The first two columns are just the system date and time for reference.
* The second column is the date difference (datediff) between the first of January 2017 and the system date.
* The last two columns are examples of subtracting and adding dates.
![image-1](https://github.com/SteveChevalier/guide-images/blob/master/date-functions02.jpg)
* ### In a where or having clause :
Here we see:
* The first two columns are just the system date and time for reference.
* The second column is the date difference (datediff) between the first of January 2017 and the system date.
* The last two columns are examples of subtracting and adding dates.
Here are two examples of using date math in a where clause:
### In a where or having clause
```sql
select * from student; - to show the current data being used for the example
select * from student where recordCreated < '2017-08-14';
select * from student where recordCreated < subdate(current_date(), interval 628 day);
```
Result :
Here are two examples of using date math in a where clause:
```sql
select * from student; - to show the current data being used for the example
select * from student where recordCreated < '2017-01-01';
select * from student where recordCreated < subdate(current_date(), interval 225 day);
```
![image-1](https://github.com/SteveChevalier/guide-images/blob/master/date-functions03.jpg)
```text
+------------+---------------------+-----------+---------------+
| studentID | FullName | sat_score | recordCreated |
+------------+---------------------+-----------+---------------+
| 1 | Jon snow | 200 | 2017-08-03 |
| 2 | Stan Lee | 900 | 2017-08-05 |
| 3 | Vincent Uvalle | 400 | 2017-08-07 |
| 4 | Merle Veres | 800 | 2017-08-07 |
| 5 | Donte Emmons | 1000 | 2017-08-07 |
| 6 | Demetrius Mccaster | 1200 | 2017-08-07 |
| 7 | Tim Goudy | 1400 | 2017-08-07 |
| 8 | Stephan Monfort | 1600 | 2017-08-07 |
| 9 | Maximo Backstrom | 1800 | 2017-08-14 |
| 10 | Dean Pickel | 800 | 2017-08-14 |
+------------+---------------------+-----------+---------------+
```
```text
+------------+---------------------+-----------+---------------+
| studentID | FullName | sat_score | recordCreated |
+------------+---------------------+-----------+---------------+
| 1 | Jon snow | 200 | 2017-08-03 |
| 2 | Stan Lee | 900 | 2017-08-05 |
| 3 | Vincent Uvalle | 400 | 2017-08-07 |
| 4 | Merle Veres | 800 | 2017-08-07 |
| 5 | Donte Emmons | 1000 | 2017-08-07 |
| 6 | Demetrius Mccaster | 1200 | 2017-08-07 |
| 7 | Tim Goudy | 1400 | 2017-08-07 |
| 8 | Stephan Monfort | 1600 | 2017-08-07 |
+------------+---------------------+-----------+---------------+
```
```text
+------------+---------------------+-----------+---------------+
| studentID | FullName | sat_score | recordCreated |
+------------+---------------------+-----------+---------------+
| 1 | Jon snow | 200 | 2017-08-03 |
| 2 | Stan Lee | 900 | 2017-08-05 |
+------------+---------------------+-----------+---------------+
```
Regarding the HAVING part: Keep in mind, most of the WHERE clause logic will also work in the HAVING clause of a GROUP BY. The difference between the two is that the WHERE clause runs against the full data, and the HAVING runs against the data aggregated by the GROUP BY clause.