Built-in character set parsing for DATE_FORMATE function in MySQL _ MySQL

Source: Internet
Author: User
Tags month name percona server
MySQL DATE_FORMATE function built-in character set parsing bitsCN.com

Today, I helped my colleagues handle an SQL statement (simplified) and reported the following error:

The code is as follows:
Mysql> select date_format ('2017-11-19 ', 'Y-m-D')> timediff ('2017-11-19', '2017-11-20 ');

ERROR 1267 (HY000): Illegal mix of collations (utf8_general_ci, COERCIBLE) and (latin1_swedish_ci, NUMERIC) for operation '>'


At first glance, it was quite inexplicable. I checked the manual and found that there was such a paragraph:

The language used for day and month www.111cn.net names and abbreviations is controlled by the value of the lc_time_names system variable (Section 9.7, "MySQL Server Locale Support ").

The DATE_FORMAT () returns a string with a character set and collation given by character_set_connection and collation_connection so that it can return month and weekday names containing non-ASCII characters.


That is to say, the result returned by the DATE_FORMATE () function carries the character set/verification set attribute, while the TIMEDIFF () function does not have the character set/verification set attribute. let's verify it:

The code is as follows:
Mysql> set names utf8;
Mysql> select charset (date_format ('2017-11-19 ', 'Y-m-D'), charset (timediff ('2017-11-19 ', '2017-11-20 '));
+ -------------------------------------------- + ------------------------------------------------- +
| Charset (date_format ('2017-11-19 ', 'Y-m-D') | charset (timediff ('2017-11-19', '2017-11-20 ') |
+ -------------------------------------------- + ------------------------------------------------- +
| Utf8 | binary |
+ -------------------------------------------- + ------------------------------------------------- +

Mysql> set names gb2312;
Mysql> select charset (date_format ('2017-11-19 ', 'Y-m-D'), charset (timediff ('2017-11-19 ', '2017-11-20 '));
+ -------------------------------------------- + ------------------------------------------------- +
| Charset (date_format ('2017-11-19 ', 'Y-m-D') | charset (timediff ('2017-11-19', '2017-11-20 ') |
+ -------------------------------------------- + ------------------------------------------------- +
| Gb2312 | binary |
+ -------------------------------------------- + ------------------------------------------------- +

As you can see, the character SET returned by the DATE_FORMAT () function varies with the character_set_connection and collation_connection values modified through set names. In this case, if you want to work properly, you need to convert the result into a character set. for example:

The code is as follows:
Mysql> select date_format ('2017-11-19 ', 'Y-m-D')> convert (timediff ('2017-11-19', '2017-11-20 ') using utf8 );
+ Shards +
| Date_format ('2017-11-19 ', 'Y-m-D')> convert (timediff ('2017-11-19', '2017-11-20 ') using utf8) |
+ Shards +
| 1 |
+ Shards +

You can.

P.S, MySQL versions: 5.5.20-55-log Percona Server (GPL), Release rel24.1, Revision 217

Appendix

The following describes the function parameters:
% S, % s two-digit second (,..., 59)
% I two-digit score (,..., 59)
% H two-digit hour, 24 hours (,..., 23)
% H, % I two-digit hour, 12 hours (,..., 12)
% K, 24 hours (0, 1,..., 23)
% L hour in numeric format (1, 2,..., 12)
% T 24-hour time format (hh: mm: s)
% R 12-hour time format (hh: mm: ss AM or hh: mm: ss PM)
% P AM or P M
% W name of each day in a week (Sunday, Monday,..., Saturday)
% A abbreviation of the name of each day in a week (Sun, Mon,..., Sat)
% D indicates the number of days in a month (00, 01,..., 31)
% E indicates the number of days in a month (1, 2,..., 31)
% D indicates the number of days in a month (1st, 2nd, 3rd ,...)
% W represents the number of days in a week (0 = Sunday, 1 = Monday,..., 6 = Saturday)
% J represents the number of days in a year in three digits (001,002,..., 366)
% U week (0, 1, 52), where Sunday is the first day of the week
% U week (0, 1, 52), where Monday is the first day of the week
% M month name (January, February,..., December)
% B abbreviated month name (January, February,..., December)
% M double-digit month (01, 02,..., 12)
% C indicates the month (1, 2,..., 12)
The year in the format of % Y.
The year in double-digit % y format.
% Direct value "%"

For more details, see: http://www.111cn.net/database/mysql/56671.htm

BitsCN.com

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.