PostgreSQL 14にcastで文字を日付に変換する

環境
Windows10 64bit
PostgreSQL 14.1

1.文字を日付に変換します
書式
cast(文字列 as date )

操作例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
arkgamedb=# select cast('20220107' as date);
date
------------
2022-01-07
(1)
arkgamedb=# select cast('20220107' as date); date ------------ 2022-01-07 (1 行)
arkgamedb=# select cast('20220107' as date);
    date
------------
 2022-01-07
(1 行)

2.文字列を日付(timestamp)に変換します
書式
cast(文字列 as timestamp )

操作例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
arkgamedb=# select cast('20220107' as timestamp);
timestamp
---------------------
2022-01-07 00:00:00
(1)
arkgamedb=# select cast('20220107' as timestamp); timestamp --------------------- 2022-01-07 00:00:00 (1 行)
arkgamedb=# select cast('20220107' as timestamp);
      timestamp
---------------------
 2022-01-07 00:00:00
(1 行)

3.文字列を日付時刻(timestamp)に変換するサンプル
操作例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
arkgamedb=# select cast('20220107 223456' as timestamp);
timestamp
---------------------
2022-01-07 22:34:56
(1)
arkgamedb=# select cast('20220107 223456' as timestamp); timestamp --------------------- 2022-01-07 22:34:56 (1 行)
arkgamedb=# select cast('20220107 223456' as timestamp);
      timestamp
---------------------
 2022-01-07 22:34:56
(1 行)

 

PostgreSQL

Posted by arkgame