不同數據庫模式下DATE類型的行爲解析

{"type":"doc","content":[{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"​​​​​​​​​​​​摘要:本文章主要介紹了GaussDB(DWS)數據類型中的DATE類型在不同數據庫模式下且在不同應用場景下的行爲表現及對比。","attrs":{}}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"本文分享自華爲雲社區","attrs":{}},{"type":"link","attrs":{"href":"https://bbs.huaweicloud.com/blogs/266760?utm_source=infoq&utm_medium=bbs-ex&utm_campaign=ei&utm_content=content","title":"","type":null},"content":[{"type":"text","text":"《GaussDB(DWS)數據類型之DATE類型》","attrs":{}}]},{"type":"text","text":",原文作者:小仲。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"GaussDB(DWS)中有三種模式,分別是Oracle模式,TD模式和MySQL模式。不同的類型在不同的模式下,也有不同的行爲表現。接下來便對GaussDB(DWS)中的日期DATE類型進行詳細介紹。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"DATE類型的取值範圍:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"GaussDB(DWS)在存儲DATE類型時,不支持年月日全部爲零或部分爲零的場景,且取值範圍在三種模式下是不同的:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在Oracle模式下,DATE類型的取值範圍是公元前4713年1月1日到公元294277年1月1日。在MySQL模式和TD模式中,DATE類型的取值範圍是公元前4713年到公元5874897年。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"用例說明:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"1. 不同模式下DATE類型的行爲","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Oracle模式","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"a.DATE類型的表現","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"    ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"GaussDB(DWS)中DATE類型存儲時不帶時分秒,由於在Oracle模式下的DATE類型需要帶有時分秒,使得GaussDB(DWS)在Oracle模式下存在帶時分秒的和不帶時分秒的兩種DATE類型,所以爲了容易區分,在顯示時不帶時分秒的DATE類型用列名date表示;帶時分秒的DATE類型用同樣帶時分秒的TIMESTAMP類型的列名timestamp表示。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"postgres=# select date '4713-01-01 BC';\n timestamp \n------------------------\n 4713-01-01 00:00:00 BC\n(1 row)\t\npostgres=# select date '294277-01-01';\n timestamp \n-----------------------\n 294277-01-01 00:00:00\n(1 row)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"​","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"b. Oracle模式下得到只含有年月日的DATE類型的方法","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"    ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果想要在Oracle模式下將日期轉成只有年月日的DATE類型,有方法可以實現麼?事實上也是有的。GaussDB(DWS)提供了兩種方式,一種是通過在Oracle模式下使用date函數:date函數的參數至少需要包含年月日,並將日期中的年月日提取並輸出;另一種則是通過cast函數進行轉換,但轉換時應將date加上雙引號,否則轉換後的數據還是會含有時分秒部分。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"postgres=# select date('2008-05-24 10:40:21');\n date \n------------\n 2008-05-24\n(1 row)\npostgres=# select cast('2008-05-24 10:40:21.100050' as \"date\");\n date \n------------\n 2008-05-24\n(1 row)\npostgres=# select cast('2008-05-24 10:40:21.100050' as date);\n timestamp \n---------------------\n 2008-05-24 10:40:21\n(1 row)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"​","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"c.DATE類型操作符","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/b9/b99115baa91f595f13bab2d88a5ea317.jpeg","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"MySQL模式和TD模式: DATE類型只有年月日。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"    ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"a. DATE類型的表現","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"    ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"MySQL模式與TD模式下DATE類型無差異。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"mysql_db=# select date '5874897-01-01';\n\n date \n\n---------------\n\n 5874897-01-01\n\n(1 row)\n\ntd_db=# select date '5874897-01-01';\n\n date \n\n---------------\n\n 5874897-01-01\n\n(1 row)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"​","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"b. MySQL模式與TD模式下得到只含有年月日的DATE類型的方法","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"    ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"由於在MySQL模式或TD模式下,DATE類型只有年月日,故使用to_date函數和date函數所得到的結果都只有年月日,同時cast函數進行類型轉換時,date加不加引號所得到的結果也是一致的。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"td_db=# select date('2008-05-24 10:40:21');\n date \n------------\n 2008-05-24\n(1 row)\ntd_db=# select cast('2008-05-24 10:40:21.100050' as \"date\");\n date \n------------\n 2008-05-24\n(1 row)\ntd_db=# select cast('2008-05-24 10:40:21.100050' as date);\n date \n------------\n 2008-05-24\n(1 row)\n\nmysql_db=# select date('2008-05-24 10:40:21');\n date \n------------\n 2008-05-24\n(1 row)\nmysql_db=# select cast('2008-05-24 10:40:21.100050' as \"date\");\n date \n------------\n 2008-05-24\n(1 row)\nmysql_db=# select cast('2008-05-24 10:40:21.100050' as date);\n date \n------------\n 2008-05-24\n(1 row)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"​","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"c. DATE類型操作符","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/ac/ac9602474baf126636de5a652dae8ace.jpeg","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"與Oracle模式的差異:當DATE類型的值進行相減時返回值不帶day,只有數值。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"2. 不同模式下的DATE類型相關函數結果的行爲:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"GaussDB(DWS)中存在與DATE類型相關的函數:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"to_date:","attrs":{}},{"type":"text","text":"將文本類型的值轉換爲指定格式的時間戳,若參數只有一個時,默認格式爲DATE類型的格式。","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"to_char:","attrs":{}},{"type":"text","text":"將一個DATE、TIMESTAMP、TIMESTAMP WITHTIME ZONE或者TIMESTAMP WITH LOCAL TIME ZONE類型的DATETIME或者INTERVAL值按照fmt指定的格式轉換爲VARCHAR類型。可選參數fmt可以爲以下幾類:日期、時間、星期、季度和世紀。每類都可以有不同的模板,模板之間可以合理組合;常見的模板有:HH、MM、SS、YYYY、MM、DD。模板可以有修飾詞;常用的修飾詞是FM,可以用來抑制前導的零或尾隨的空白。返回值類型:varchar","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"current_date:","attrs":{}},{"type":"text","text":"獲取當前日期。","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"date_part:","attrs":{}},{"type":"text","text":"獲取給定日期時間中的指定域值。","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"isfinite:","attrs":{}},{"type":"text","text":"測試給定日期是否爲有效日期。","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/e9/e94fe539197ded0df20d6761eb672c8e.jpeg","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"​","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Oracle模式:","attrs":{}},{"type":"text","text":"由於Oracle模式中的DATE類型含有時分秒,故to_date和to_char函數得到的結果含有時分秒。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"TD模式:","attrs":{}},{"type":"text","text":"由於TD模式下的DATE類型只有年月日,故to_date函數得到的結果只有年月日。在TD模式下,to_char函數的輸出結果可以由GUC參數convert_empty_str_to_null_td控制,若不設置參數則返回上表中的結果,若設置參數則返回結果與TD數據庫中的結果保持一致,爲2008/05/24。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"MySQL模式:","attrs":{}},{"type":"text","text":"由於MySQL模式下DATE類型只有年月日,故to_date函數和to_char函數的返回結果只有年月日。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"想了解GuassDB(DWS)更多信息,歡迎微信搜索“GaussDB DWS”關注微信公衆號,和您分享最新最全的PB級數倉黑科技,後臺還可獲取衆多學習資料哦~","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://bbs.huaweicloud.com/blogs?utm_source=infoq&utm_medium=bbs-ex&utm_campaign=ei&utm_content=content","title":"","type":null},"content":[{"type":"text","text":"點擊關注,第一時間瞭解華爲雲新鮮技術~","attrs":{}}]}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章