January 16, 2021

How to Use MySQL Date and Time Functions

Follow this step by step easy tutorial to learn how to use MySQL date and time functions and see how to modify the time formats.

Don’t forget to check out our site for more free how-to videos!
– our feed
– join us on facebook
– our group in Google+

There are various functions which can be used for calculating the date and time in MySQL but for the purpose of this tutorial we will use 3 primary functions for date and time each.
For this tutorial, we will be using SQL YOG as an instance of MySQL. 
Step 1- Current Date
Let’s start with MySQL date functions.
For that write the following query:
select curdate();
and press F5 to execute. It will show the current date of the system. Curdate is a short form of current date.

Step 2-Changing date format
Now if we want to view the date in a specific format, we can use a function called the date_format().
Write “date_format” function in a similar pattern as before, but this time inside the parentheses, we will write the date within single quotation marks and then specify the required format in single quotations after a comma.
Here we are using W for week, M for Month and Y for years which are implicit format specified in the MySQL environment, and used with a percentage sign.
The actual query would be:
select date_format(‘2013-04-18′,’%W %M %Y’);

Step 3- Viewing Name of the Day
In MySQL date functions library, there is a method which can return the day name, when given the date as its perimeter.
It is called “dayname” function. To use that, write dayname(), following the “select” keyword, and specify the date within single quotes inside the parenthesis.
The query would be:
select dayname(‘1998-04-25′);

Step 4- Time Functions
Now let’s move on to mysql time functions
First of all let’s find out the current time. For that, we will write curtime(),which stands for the current time, and write the query in the same way as we did for the current date.
Hence the query would be:
select curtime();

Step 5- Changing time format
Now let’s see how we can change the format of the output.
For that, use the time_format() function, as we did for the date format.
Write the following query in the editor:
select time_format(’14:28:18′,’%h : %i : %s’);
Over here, h, i and s are used for hours, minutes and seconds respectively.

Step 6- Changing Time to Seconds
In MyQL time functions library, there are various functions related to time. One of them which is widely used by programmers is the time_to_sec() function.
This method changes the given time to seconds.
To use this, write time_to_sec() and specify the time inside the parentheses within single quotes.
In the Query Editor, write:
select time_to_sec(’14:28:18′);
When we execute the query, we can see that the time being displayed is in seconds.

Step 7- Date along with Time
Lastly, to display the current date and time altogether, we can use now() function along with the select keyword.
select now();
in the query editor and execute it.

And this was all about MySQL date & time functions.

Nguồn: https://chuabenhphukhoa.vn

Xem thêm bài viết khác: https://chuabenhphukhoa.vn/cong-nghe

Related Posts

Galaxy Note 3 vs Galaxy Note 4/ Edge – Speed test

June 13, 2020

June 13, 2020 14

Để biết thông tin và giá bán của các sản phẩm trên hãy tuy cập: Theo dõi Fanpage của maxmobile...

How To Run A Python Script With Notepad++

June 11, 2020

June 11, 2020 29

How To Run A Python Script With Notepad++ In this video I will show you how to use the NppExec...

Unboxing Samsung Galaxy Note FE Indonesia!

June 22, 2020

June 22, 2020 34

Flat earth? fire extinguisher? atau singkatan yang lain? Instagram GadgetIn: Twitter GadgetIn: Facebook GadgetIn: HP ini bisa dibeli di: HP...

Top 10 Note-Taking Apps 2018 | The Best Tools

June 14, 2020

June 14, 2020 38

The World’s Best 10 Note-Taking Applications… inc. 4 bonus mentions excellent note-taking alternatives. Notetaking applications help us to capture essential...

1.1 What is a Vector? – The Nature of Code

June 14, 2020

June 14, 2020 41

Welcome to Nature of Code Chapter 1: Vectors! What is a vector? How do they work in p5.js? These questions...

The Notebook (2004) Extended Trailer HD

June 4, 2020

June 4, 2020 49

The Notebook is a 2004 American romantic drama film directed by Nick Cassavetes and based on the novel of the...

Dell Vostro 3468 14 inch Laptop: Overview, Features & Specifications | Core i3 7th Gen

June 23, 2020

June 23, 2020 30

I have got Dell Vostro 14 inch laptop with me. It bears with model number 3468. Find Full overview features...

Evolution of the Galaxy Note (Note 1 – Note 10)

June 9, 2020

June 9, 2020 38

History of the Samsung Galaxy Note The Galaxy Note was the first commercially successful phablet. Selling over 50 million units...

Năm 2020 – Note 9 Xách Tay Hàn giảm mạnh. Có nên mua ở thời điểm hiện tại hay không?

June 13, 2020

June 13, 2020 20

Note 10 và Note 10 Plus đã ra mắt được nửa năm rồi. Trong khi Galaxy Note 9 chính hãng...

DO NOT say "you're welcome"! Respond to "thank you" PROPERLY!

June 15, 2020

June 15, 2020 42

You’re welcome is SO OVERUSED! Here are 16 advanced ways to respond to ‘thank you’ in both casual and formal...

Galaxy Note 7 FE kutusundan çıkıyor – Note 7 ile hasret giderdik!

June 19, 2020

June 19, 2020 50

Samsung’un piyasadan çektiği Galaxy Note 7 modelinin yenilenmiş ve güvenilir sürümü olan ve Türkiye’de resmen satışa sunulmayan Galaxy Note 7...

Đánh giá xe Yamaha Nozza Grande 125 sau 1.300km sử dụng |TINXE.VN|

June 16, 2020

June 16, 2020 27

Xin chào tất cả các bạn, đây là kênh chính thức của Tinxe chuyên về Ô tô – Xe máy,...

Đánh giá hiệu năng & thời lượng pin Huawei Nova 3e

June 11, 2020

June 11, 2020 37

Tham khảo GIÁ RẺ NHẤT tại: Nova 3e với con chip Kirin do Huawei tự sản xuất, tương tự như...

How to Reverse a Calf Table

June 15, 2020

June 15, 2020

Handle bigger calves with less effort with priefert’s unique two-stage pivot calf table.Designed so a smaller individual can roll a...

Galaxy Note 5, 3 triệu có phải mức giá thơm?

June 9, 2020

June 9, 2020 38

► Chi tiết sản phẩm trong video xem tại đây: – Galaxy Note 5: Samsung Galaxy Note 5 giá 3...

  • ❗ Subscribe To Our Main YouTube Channel


  • short and accurate ! Great video, only thing you need is to zoom in the code to read what you're talking about, that would make things a lot easier

  • Is it possible to use the "now()" method to insert the current time and date to a text type variable for example? Sorry for the silly question, there are some things in MySQL I'm still learning.

Leave a Reply

Your email address will not be published. Required fields are marked *