Implementation of comparison, sorting and other operations on varchar type dates in MySQL

Implementation of comparison, sorting and other operations on varchar type dates in MySQL

When using MySQL, dates are generally stored in formats such as datetime and timestamp. However, sometimes due to special needs or historical reasons, the date is stored in varchar format. So how do we handle date data in this varchar format?

Use function: STR_TO_DATE(str, format)

The STR_TO_DATE(str, format) function is the inverse function of the DATE_FORMAT() function. It takes a string str and a format string format. STR_TO_DATE() returns a DATETIME value if the format string contains both date and time parts, or returns a date or time value if the string contains only a date or time part.

Let’s take a case to illustrate this.

For example, I now have a membership table, the structure of the table is as follows:

I created the following data in the table:

Now, if I need to select all members who registered in August 2018 and sort them by registration time, how can I do it? Here is a reference:

SELECT id,nickname,createtime 
FROM member
WHERE str_to_date(createtime, '%Y-%m-%d') BETWEEN str_to_date('2018-08-01', '%Y-%m-%d') AND str_to_date('2018-08-31', '%Y-%m-%d')
ORDER BY str_to_date(createtime, '%Y-%m-%d')

The execution results are as follows:

Summary: The STR_TO_DATE(str, format) function can convert varchar date data in the database into DATETIME date data. During the conversion process, please note that the format parameter must correspond to the format of str. After that, we can perform comparison, sorting and other operations!

This is the end of this article about the implementation of operations such as comparison and sorting of varchar type dates in MySQL. For more relevant mysql varchar type comparison and sorting content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Method for comparing the size of varchar type numbers in MySQL database
  • Some things to note about varchar type in Mysql
  • The difference between char, varchar and text field types in MySQL
  • Mysql varchar type sum example operation
  • MySQL data type varchar detailed explanation

<<:  HTTP return code list (Chinese and English explanation)

>>:  Vue Learning - VueRouter Routing Basics

Recommend

Description of the default transaction isolation level of mysql and oracle

1. Transaction characteristics (ACID) (1) Atomici...

Various types of MySQL indexes

What is an index? An index is a data structure th...

Use of Linux file command

1. Command Introduction The file command is used ...

Vue3.x uses mitt.js for component communication

Table of contents Quick Start How to use Core Pri...

10 Tips for Mobile App User Interface Design

Tip 1: Stay focused The best mobile apps focus on...

CentOS7.5 installation tutorial of MySQL

1. First check whether the system has mysql insta...

Vue+spring boot realizes the verification code function

This article example shares the specific code of ...

Using react-beautiful-dnd to implement drag and drop between lists

Table of contents Why choose react-beautiful-dnd ...

Pure CSS to achieve the water drop animation button in Material Design

Preface You should often see this kind of special...

JS realizes the case of eliminating stars

This article example shares the specific code of ...

How to get datetime data in mysql, followed by .0

The data type of MySQL is datetime. The data stor...

What is jQuery used for? jQuery is actually a js framework

Introduction to jQuery The jQuery library can be ...