Table of Contents
1,YYYYmmdd格式日期转换为null
2,查看报错信息:
3,换成规范的%Y-%m-%d %H:%i:%s试试
4,再次确定缩小范围,是%h的问题,将时换成大写就搞定了
Home Database Mysql Tutorial MySQL日期字符串转换成NULL值的异常处理_MySQL

MySQL日期字符串转换成NULL值的异常处理_MySQL

May 30, 2016 pm 05:11 PM
string date

1,YYYYmmdd格式日期转换为null

看如下记录,一个能取到值,一个取不到值**
mysql> SELECT DATE_FORMAT(STR_TO_DATE(‘20150922 13:01:01’, ‘%Y%m%d %H:%m:%s’),’%H:%m’);
+————————————————————————–+
| DATE_FORMAT(STR_TO_DATE(‘20150922 13:01:01’, ‘%Y%m%d %H:%m:%s’),’%H:%m’) |
+————————————————————————–+
| 13:01 |
+————————————————————————–+
1 row in set (0.00 sec)

mysql> SELECT DATE_FORMAT(STR_TO_DATE(‘20150922 12:55:00’, ‘%Y%m%d %H:%m:%s’),’%H:%m’);
+————————————————————————–+
| DATE_FORMAT(STR_TO_DATE(‘20150922 12:55:00’, ‘%Y%m%d %H:%m:%s’),’%H:%m’) |
+————————————————————————–+
| NULL |
+————————————————————————–+
1 row in set, 1 warning (0.00 sec)

mysql>

2,查看报错信息:

mysql> show warnings;
+———+——+————————————————————————+
| Level | Code | Message |
+———+——+————————————————————————+
| Warning | 1411 | Incorrect datetime value: ‘20150922 12:55:00’ for function str_to_date |
+———+——+————————————————————————+
1 row in set (0.00 sec)

mysql>
报警说是Incorrect datetime value: ‘20150922 12:55:00’ for function str_to_date,不正确的日期格式,所以换成比较规范的日期格式

原blog地址:http://blog.csdn.net/mchdba/article/details/48719765,未经过csdn原博客博主mchdba允许,不能转载。

3,换成规范的%Y-%m-%d %H:%i:%s试试

mysql> select DATE_FORMAT(str_to_date(‘2015-09-22 13:00:01’, ‘%Y-%m-%d %H:%i:%s’),’%H:%i’);
+——————————————————————————+
| DATE_FORMAT(str_to_date(‘2015-09-22 13:00:01’, ‘%Y-%m-%d %H:%i:%s’),’%H:%i’) |
+——————————————————————————+
| 13:00 |
+——————————————————————————+
1 row in set (0.00 sec)

mysql> select DATE_FORMAT(str_to_date(‘2015-09-22 12:55:00’, ‘%Y-%m-%d %H:%i:%s’),’%H:%i’);
+——————————————————————————+
| DATE_FORMAT(str_to_date(‘2015-09-22 12:55:00’, ‘%Y-%m-%d %H:%i:%s’),’%H:%i’) |
+——————————————————————————+
| 12:55 |
+——————————————————————————+
1 row in set (0.00 sec)

mysql>

看到在规范的格式下,日期从字符串转换到日期格式,然后截取时分都是能取到值的。

参考官网地址:http://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html#function_str-to-date

4,再次确定缩小范围,是%h的问题,将时换成大写就搞定了

再看到一个案例,将格式变成2015-09-22 13:00:01表准备格式,可以仍然取不到时分值,如下所示:

<code class="hljs asciidoc">mysql> select DATE_FORMAT(str_to_date(&#39;2015-09-22 13:00:01&#39;, &#39;%Y-%m-%d %h:%i:%s&#39;),&#39;%h:%i&#39;);
+------------------------------------------------------------------------------+
| DATE_FORMAT(str_to_date(&#39;2015-09-22 13:00:01&#39;, &#39;%Y-%m-%d %h:%i:%s&#39;),&#39;%h:%i&#39;) |
+------------------------------------------------------------------------------+
| NULL                                                                         |
+------------------------------------------------------------------------------+
1 row in set, 1 warning (0.00 sec)

mysql> select DATE_FORMAT(str_to_date(&#39;2015-09-22 12:55:00&#39;, &#39;%Y-%m-%d %h:%i:%s&#39;),&#39;%h:%i&#39;);
+------------------------------------------------------------------------------+
| DATE_FORMAT(str_to_date(&#39;2015-09-22 12:55:00&#39;, &#39;%Y-%m-%d %h:%i:%s&#39;),&#39;%h:%i&#39;) |
+------------------------------------------------------------------------------+
| 12:55                                                                        |
+------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> </code>
Copy after login

那么问题在哪里呢?只能再次去官网找答案,找到http://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html#function_get-format<br /> 看到终极标准格式为:&rsquo;%Y-%m-%d %H.%i.%s&rsquo;,换成这个试试,看到范例中的差异在哪里了吗?就在于%H和%h的区别啊,如下所示:

<code class="hljs asciidoc"><code class="hljs asciidoc">mysql> select DATE_FORMAT(str_to_date(&#39;20150922 13:00:01&#39;, &#39;%Y%m%d %h:%i:%s&#39;),&#39;%h:%i&#39;);
+--------------------------------------------------------------------------+
| DATE_FORMAT(str_to_date(&#39;20150922 13:00:01&#39;, &#39;%Y%m%d %h:%i:%s&#39;),&#39;%h:%i&#39;) |
+--------------------------------------------------------------------------+
| NULL                                                                     |
+--------------------------------------------------------------------------+
1 row in set, 1 warning (0.00 sec)

mysql> select DATE_FORMAT(str_to_date(&#39;20150922 12:55:00&#39;, &#39;%Y%m%d %h:%i:%s&#39;),&#39;%h:%i&#39;);
+--------------------------------------------------------------------------+
| DATE_FORMAT(str_to_date(&#39;20150922 12:55:00&#39;, &#39;%Y%m%d %h:%i:%s&#39;),&#39;%h:%i&#39;) |
+--------------------------------------------------------------------------+
| 12:55                                                                    |
+--------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> </code></code>
Copy after login

<code class="hljs asciidoc"><strong>然后标准的日期格式为:%Y%m%d %H:%i:%s’或者%Y-%m-%d %H:%i:%s’ 都可以的,如下所示:</strong>

<code class="hljs asciidoc">select DATE_FORMAT(str_to_date(‘20150922 13:00:01’, ‘%Y%m%d %H:%i:%s’),’%H:%i’);<br> select DATE_FORMAT(str_to_date(‘20150922 12:55:00’, ‘%Y%m%d %H:%i:%s’),’%H:%i’);

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to search previous Weibo by date on Weibo_How to search previous Weibo by date on Weibo How to search previous Weibo by date on Weibo_How to search previous Weibo by date on Weibo Mar 30, 2024 pm 07:26 PM

1. First open the mobile web browser, search for the Weibo web version, and click the avatar button in the upper left corner after entering. 2. Then click Settings in the upper right corner. 3. Click the version switching option in settings. 4. Then select the color version option in the version switch. 5. Click Search to enter the search page. 6. After entering the keywords, click Find People. 7. When the search completion interface appears, click Filter. 8. Finally, enter the specific date in the release time column and click Filter.

How to remove the date that appears automatically when printing from PPT handouts How to remove the date that appears automatically when printing from PPT handouts Mar 26, 2024 pm 08:16 PM

1. Let me first talk about the method I used at the beginning, maybe everyone is using it too. First, open [View]——]Remarks Template[. 2. A place where you can actually see the date after opening it. 3. Select it first and delete it. 4. After deleting, click [Close Master View]. 5. Open the print preview again and find that the date is still there. 6. In fact, this date was not deleted here. It should be in the [Handout Master]. Look at the picture below. 7. Delete the date after you find it. 8. Now when you open the preview and take a look, the date is no longer there. Note: In fact, this method is also very easy to remember, because the printed handouts are handouts, so you should look for the [Handout Master].

How to determine whether a Golang string ends with a specified character How to determine whether a Golang string ends with a specified character Mar 12, 2024 pm 04:48 PM

Title: How to determine whether a string ends with a specific character in Golang. In the Go language, sometimes we need to determine whether a string ends with a specific character. This is very common when processing strings. This article will introduce how to use the Go language to implement this function, and provide code examples for your reference. First, let's take a look at how to determine whether a string ends with a specified character in Golang. The characters in a string in Golang can be obtained through indexing, and the length of the string can be

How to change the date into a pound sign in Excel How to change the date into a pound sign in Excel Mar 20, 2024 am 11:46 AM

Excel software has very powerful data processing functions. We often use excel software to process various data. Sometimes when we enter a date in an excel cell, the date in excel changes to a pound sign. How can we display the data normally? Let’s take a look at the solution below. 1. First, we put the mouse on the column width line between columns AB, double-click and adjust the column width, as shown in the figure below. 2. After the column is widened, we find that numbers are displayed in the cells instead of dates. This is definitely incorrect. Then we should check the format of the cells, as shown in the figure below. 3. Click the &quot;Number&quot; option in the &quot;Home&quot; tab, and click &quot;Other Number Format&quot; in the drop-down menu, as shown in the figure below.

How to repeat a string in python_python repeating string tutorial How to repeat a string in python_python repeating string tutorial Apr 02, 2024 pm 03:58 PM

1. First open pycharm and enter the pycharm homepage. 2. Then create a new python script, right-click - click new - click pythonfile. 3. Enter a string, code: s="-". 4. Then you need to repeat the symbols in the string 20 times, code: s1=s*20. 5. Enter the print output code, code: print(s1). 6. Finally run the script and you will see our return value at the bottom: - repeated 20 times.

How to intercept a string in Go language How to intercept a string in Go language Mar 13, 2024 am 08:33 AM

Go language is a powerful and flexible programming language that provides rich string processing functions, including string interception. In the Go language, we can use slices to intercept strings. Next, we will introduce in detail how to intercept strings in Go language, with specific code examples. 1. Use slicing to intercept a string. In the Go language, you can use slicing expressions to intercept a part of a string. The syntax of slice expression is as follows: slice:=str[start:end]where, s

Detailed explanation of the method of converting int type to string in PHP Detailed explanation of the method of converting int type to string in PHP Mar 26, 2024 am 11:45 AM

Detailed explanation of the method of converting int type to string in PHP In PHP development, we often encounter the need to convert int type to string type. This conversion can be achieved in a variety of ways. This article will introduce several common methods in detail, with specific code examples to help readers better understand. 1. Use PHP’s built-in function strval(). PHP provides a built-in function strval() that can convert variables of different types into string types. When we need to convert int type to string type,

How to solve the problem of Chinese garbled characters when converting hexadecimal to string in PHP How to solve the problem of Chinese garbled characters when converting hexadecimal to string in PHP Mar 04, 2024 am 09:36 AM

Methods to solve Chinese garbled characters when converting hexadecimal strings in PHP. In PHP programming, sometimes we encounter situations where we need to convert strings represented by hexadecimal into normal Chinese characters. However, in the process of this conversion, sometimes you will encounter the problem of Chinese garbled characters. This article will provide you with a method to solve the problem of Chinese garbled characters when converting hexadecimal to string in PHP, and give specific code examples. Use the hex2bin() function for hexadecimal conversion. PHP’s built-in hex2bin() function can convert 1

See all articles