1.
NOW():
NOW () function in SQL will give the current system's date and time.
Write a query to display the current system's date and time.
Query:
1. mysql> SELECT NOW () AS Current_Date_Time;
2. CURDATE()
CURDATE () function in SQL will give the current system's date.
Write a query to display the current system's date.
Query:
1. mysql> SELECT CURDATE () AS CurrentDate;
3. CURTIME()
CURTIME () function in SQL will give the current system time.
Write a query to display the current system's time.
Query:
1. mysql> SELECT CURTIME () AS CurrentTime;
4. DATE()
Using the DATE () function in SQL, you can specifically extract the date from
the DATETIME datatype column.
Write a query to display the date from the given date and time, i.e., 2021-10-
24 18:28:44.
Query:
1. mysql> SELECT DATE ("2021-10-24 18:28:44") AS SHOW_DATE;
5. EXTRACT()
Using the EXTRACT() function in SQL, we can extract a specific part of date
and time according to our requirements: day, month, year, day, hour,
minute, etc.
Write a query to display the year from the given date, i.e., 24 th October 2021.
Query:
1. mysql> SELECT EXTRACT (YEAR FROM "2021-10-24") AS SHOW_YEAR;
Write a query to display the month from the given date, i.e., 24 th October
2021.
Query:
1. mysql> SELECT EXTRACT (MONTH FROM "2021-10-24") AS SHOW_MONTH;
Write a query to display the day from the given date, i.e., 24 th October 2021.
Query:
1. mysql> SELECT EXTRACT (DAY FROM "2021-10-24") AS SHOW_DAY;
Write a query to display the hour from the given time, i.e., 19:10:43.
Query:
1. mysql> SELECT EXTRACT (HOUR FROM "19:10:43") AS SHOW_HOUR;
Write a query to display the minute from the given time, i.e., 19:10:43.
Query:
1. mysql> SELECT EXTRACT (MINUTE FROM "19:10:43") AS SHOW_MINUTE;
Write a query to display the seconds from the given time, i.e., 19:10:43.
Query:
1. mysql> SELECT EXTRACT (SECOND FROM "19:10:43") AS SHOW_SECOND;
6. DATE_ADD()
Using the DATE_ADD () function in SQL, we can add a specific time interval to
the given date.
Write a query to add an interval of 15 days to the given date, i.e.,
24th October, 2021.
Query:
mysql> SELECT DATE_ADD ("2021-10-24", INTERVAL 15 DAY) AS NEW_DAT
E;
Write a query to add an interval of 5 months to the given date, i.e.,
24th October, 2021.
Query:
mysql> SELECT DATE_ADD ("2021-10-24", INTERVAL 5 MONTH) AS NEW_DA
TE;
Write a query to add an interval of 5 years to the given date, i.e.,
24th October, 2021.
Query:
mysql> SELECT DATE_ADD ("2021-10-24", INTERVAL 5 YEAR) AS NEW_DATE
;
7. DATE_SUB()
Using the DATE_SUB () function in SQL, we can remove a specific time
interval from the given date.
Write a query to remove an interval of 5 years from the given date, i.e.,
24th October, 2021.
Query:
mysql> SELECT DATE_SUB("2021-10-24", INTERVAL 5 YEAR) AS NEW_DATE;
Example 2:
Write a query to remove an interval of 5 months from the given date, i.e.,
24th October, 2021.
Query:
mysql> SELECT DATE_SUB("2021-10-24", INTERVAL 5 MONTH) AS NEW_DA
TE;
Example 3:
Write a query to remove an interval of 15 days from the given date, i.e.,
24th October, 2021.
Query:
mysql> SELECT DATE_SUB("2021-10-24", INTERVAL 15 DAY) AS NEW_DATE;
8. DATEDIFF()
Using the DATEDIFF() function in SQL will give us the number of days that fall
between the two given dates.
Write a query to calculate the difference between two given dates, i.e.,
24th October, 2021, and 9th October, 2021.
Query:
mysql> SELECT DATEDIFF("2021-10-24", "2021-10-09") AS NEW_DATE;
SQL string functions:
CONCAT This function is used to concatenate two or more strings into a single string.
SELECT CONCAT('Hello', 'World');
Output: HelloWorld
SUBSTRING This function is used to extract a substring from a given string.
SELECT SUBSTRING('Hello World', 1, 5);
Output: Hello
LEN This function is used to get the length of a string.
SELECT LENGTH('Hello World');
Output: 11
REPLACE This function is used to replace a part of a string with another string.
SELECT REPLACE('Hello World', 'Hello', 'Hi');
Output: Hi World
UPPER This function is used to convert a string to uppercase.
SELECT UPPER('Hello World');
Output: HELLO WORLD
LOWER This function is used to convert a string to lowercase.
SELECT LOWER('Hello World');
Output: hello world
TRIM This function is used to remove extra spaces from the beginning and end of a
string.
SELECT TRIM(' Hello World ');
Output: Hello World