PostgreSQL 13に型textからvarcharへの変換サンプル

2022年1月9日

環境
Windows10 64 bit
PostgreSQL 13.2

書式
桁数指定あり
cast(cast(文字列 as text) as varchar(桁数))
桁数指定なし
cast(cast(文字列 as text) as varchar)

使用例
1.型textからvarchar文字数指定(6桁)へ変換します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
postgres=# select cast(cast('studyskill' as text) as varchar(6)) result;
result
--------
studys
(1)
postgres=# select cast(cast('studyskill' as text) as varchar(6)) result; result -------- studys (1 行)
postgres=# select cast(cast('studyskill' as text) as varchar(6)) result;
 result
--------
 studys
(1 行)

2.型textからvarchar文字数指定なしへ変換します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
postgres=# select cast(cast('study skill' as text) as varchar) result;
result
-------------
study skill
(1)
postgres=# select cast(cast('study skill' as text) as varchar) result; result ------------- study skill (1 行)
postgres=# select cast(cast('study skill' as text) as varchar) result;
   result
-------------
 study skill
(1 行)

 

PostgreSQL

Posted by arkgame