搜尋
問答 Mysql 在日期欄位中傳回一些記錄為空,因為日期有多種格式

0

我正在嘗試分析order_date 列,並且列具有多種日期格式,我想將所有這些日期轉換為相同的格式,這將使分析order_date 變得更容易。

我正在嘗試分析 order_date 但是此列有多種日期格式 2019/07/151/13/2014

#但是,在使用查詢將不同格式的日期轉換為一種格式 yyyy/mm/dd 時。 select date_format(order_date, '%y/%m/%d'),orderid from superstore;

#它顯示像這樣的空值。

我也嘗試過使用“CAST”,但它顯示每個值都為空。

選擇 order_date 像 '%Y' 時的情況,然後 date_format(order_date, '%Y/%m/%d') else null end as newdate from superstore;

#
Your Answer
提交

1 個答案
0

date_format 函數用於格式化您應該使用的日期資料類型https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_str-to -date 任何null str_to_date 傳回的值要麼失敗,要麼以空值開始。您需要檢查這些並適當調整 str_to_date 參數。但有一個問題是 20/2/20 y/m/d 或 d/m/y(例如),如何區分月份和日期都 例如

drop table if exists t;
create table t
(dt varchar(10));
insert into t values
('1/1/2020'),('2020/1/12'),('12/12/12'),(null),('13-14-15');

select dt,
        case when length(substring_index(dt,'/',-1)) = 4  then str_to_date(dt,'%d/%m/%Y')
             when length(substring_index(dt,'/',1)) = 4  then str_to_date(dt,'%Y/%m/%d')
             when length(substring_index(dt,'/',1)) = 2  then str_to_date(dt,'%y/%m/%d')
             else str_to_date(dt,'%y/%m/%d')
        end  dateformatted
from t;

https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=553219f33ad9e9a4404fc4c0cb65 71c9一个>

#請注意,在任何情況下我都無法識別月份和日期,有時甚至是年份..

2024-04-07 10:22:18

提交

Hot Tools

vc9-vc14(32+64位元)運行庫合集(連結在下方)

vc9-vc14(32+64位元)運行庫合集(連結在下方)

phpStudy安裝所需運行函式庫集合下載

VC9 32位

VC9 32位

VC9 32位元 phpstudy整合安裝環境運行庫

php程式設計師工具箱完整版

php程式設計師工具箱完整版

程式設計師工具箱 v1.0 php整合環境

VC11 32位

VC11 32位

VC11 32位元 phpstudy整合安裝環境運行庫

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用