Learn Python
Learn Data Structure & Algorithm
Learn Numpy
Learn Pandas
Learn Matplotlib
Learn Seaborn
Learn Statistics
Learn Math
Learn MATLAB
Learn Machine learning
Learn Github
Learn OpenCV
Learn Deep Learning
MySQL Introduction
MySQL Setup
MySQL Operators
MySQL Basic Works
MySQL Create Table
MySQL insert and get values
MySQL Condition
MySQL GROUP BY
MySQL Case
MySQL sorting and calculate avg,sum,count
MySQL Update
MySQL Time
MySQL String
MySQL Rollback & Commit
MySQL Join and Concatenate
MySQL Index
MySQL Date
MySQL View
MYSQL With Python
Learn MongoDB
Learn Web scraping
Learn Excel
Learn Power BI
Learn Tableau
Learn Docker
Learn Hadoop
Suppose you have a database name database and inside that, you have a table named DataBaseTable which have a date column. In the date column, you have date and time.
This function will return the current date and time. This date and time will be taken from the server.
If you pass date and time together in the this function then it will return only date.
If you pass date and time together in this function then it will return you only month.
If you pass date and time together in this function then it will return you only month name.
If you pass date and time together in this function then it will return you only year.
If you pass date and time together in this function then it will return you only day.
If you pass date and time together in this function then it will return you only day name. Here day name means Sat, Sun, Mon, Tue, Wed, Thus, and Fry.
If you pass date and time together in this function then it will return you only day name index. Here day's name means Sat, Sun, Mon, Tue, Wed, Thus, and Fry. Index means, Sat in the first position so the index is 0. Same way Fri index is 6.
If you pass date and time together in this function then it will return you only day number of the year. You know that a year has 364 days. So this function will return the day number like 77 number day from 364 days.
If you pass date and time together in this function then it will return you a quarter of that date. It means in which quarter the date is present. In a year there are four quarters, first quarter=January-March, second quarter= April-June, third quarter=July-Sep, and fourth quarter=Oct-Dec.
In this function, we pass an existing date and interval. According to that date and interval this function will create a new date. As an interval, you can pass month, day, second, minute, hour, year, week, etc. Suppose you have the date and pass the day as an interval and the interval value is 10. So this function will create a new date from a given date and the date will after 10 days from the given date. It means that here we added 10 days more with the given date.
To see difference between two dates this function is used. To see the difference you have to pass two dates.
To format the date according to your need, this function is used.
We can divide date format into four parts:
Day:For day you have three options:
%d-->if you use this then for one digit date it will add 0 before. After
9 you will have 2 digit date like 12, 25, etc. But from 0 to 9 you will have one
digit date. Now
if you want to show two digit date like 01, 02, 06, etc. Then you will use
this.
%e-->if you use this then for one digit date it will not add 0 before. After
9 you will have 2 digit date like 12, 25, etc. But from 0 to 9 you will have one
digit date. Now
if you want to show one digit date like 1, 2, 6, etc then you will use this.
%D-->If you want to show date where st, nd, rd or th is written after the number then use
this option. For example 1st, 2nd, 3rd.
Year:For year you have two options:
%Y-->This will display the year in 4 digit, like 2021, 2001.
%y-->This will display the year in 2 digit, like 21, 01.
Month:For month you have three options:
%M-->This will display the month name in full form, like January, March.
%b-->This will display the month name in short form, like Jan, Mar.
%m-->This will display the month in digit from, like 1, 5, 12.
Week:For week you have three options:
%a-->This will show the week days name in the string form and it will show only three
letters, like mon, sun.
%W-->This will show the week days name in the string form and it will show only all the
letters, like monday, sunday.
%w-->This will show the week days in the form of digit(0-6).
Time format is divided into four parts:
Hour:For hour you have four options:
%h-->It will show hours from 1 to 12. After 12 it will show 1. It will add
0 before one digit hour.
%H-->It will show hours from 1-24. It will add a 0 before one digit hour.
%g-->It will show hours from 1 to 12. After 12 it will show 1. It will not add
0 before one digit hour.
%G-->It will show hours from 1-24. It will not add add 0 before one digit hour.
Minutes:For minutes write %i
Seconds:For seconds write %s
Microseconds: For microseconds write %f
AM and PM write %P
Let's see time with date:
SELECT DATE_FORMAT("2011-12-15 02:30:50","%d/%b/%Y, %g:%i") AS date_col;
In the date format function, we have the date in SQL default format and we changed that in our own format. But
in this function, we will have dates in our own format and it will change that date in SQL default format.
Here you have to pass two parameters. First, the date which is in your format. Suppose you Mar 14, 2001. Here
first you have a month so in the second parameter first write %M, then you have date so write %d and the year
so write %Y
Example:
SELECT STR_TO_DATE("Mar 14 2001","%M %d %Y") AS date_col;