Compute
Elastic Cloud Server
Huawei Cloud Flexus
Bare Metal Server
Auto Scaling
Image Management Service
Dedicated Host
FunctionGraph
Cloud Phone Host
Huawei Cloud EulerOS
Networking
Virtual Private Cloud
Elastic IP
Elastic Load Balance
NAT Gateway
Direct Connect
Virtual Private Network
VPC Endpoint
Cloud Connect
Enterprise Router
Enterprise Switch
Global Accelerator
Management & Governance
Cloud Eye
Identity and Access Management
Cloud Trace Service
Resource Formation Service
Tag Management Service
Log Tank Service
Config
OneAccess
Resource Access Manager
Simple Message Notification
Application Performance Management
Application Operations Management
Organizations
Optimization Advisor
IAM Identity Center
Cloud Operations Center
Resource Governance Center
Migration
Server Migration Service
Object Storage Migration Service
Cloud Data Migration
Migration Center
Cloud Ecosystem
KooGallery
Partner Center
User Support
My Account
Billing Center
Cost Center
Resource Center
Enterprise Management
Service Tickets
HUAWEI CLOUD (International) FAQs
ICP Filing
Support Plans
My Credentials
Customer Operation Capabilities
Partner Support Plans
Professional Services
Analytics
MapReduce Service
Data Lake Insight
CloudTable Service
Cloud Search Service
Data Lake Visualization
Data Ingestion Service
GaussDB(DWS)
DataArts Studio
Data Lake Factory
DataArts Lake Formation
IoT
IoT Device Access
Others
Product Pricing Details
System Permissions
Console Quick Start
Common FAQs
Instructions for Associating with a HUAWEI CLOUD Partner
Message Center
Security & Compliance
Security Technologies and Applications
Web Application Firewall
Host Security Service
Cloud Firewall
SecMaster
Anti-DDoS Service
Data Encryption Workshop
Database Security Service
Cloud Bastion Host
Data Security Center
Cloud Certificate Manager
Edge Security
Situation Awareness
Managed Threat Detection
Blockchain
Blockchain Service
Web3 Node Engine Service
Media Services
Media Processing Center
Video On Demand
Live
SparkRTC
MetaStudio
Storage
Object Storage Service
Elastic Volume Service
Cloud Backup and Recovery
Storage Disaster Recovery Service
Scalable File Service Turbo
Scalable File Service
Volume Backup Service
Cloud Server Backup Service
Data Express Service
Dedicated Distributed Storage Service
Containers
Cloud Container Engine
SoftWare Repository for Container
Application Service Mesh
Ubiquitous Cloud Native Service
Cloud Container Instance
Databases
Relational Database Service
Document Database Service
Data Admin Service
Data Replication Service
GeminiDB
GaussDB
Distributed Database Middleware
Database and Application Migration UGO
TaurusDB
Middleware
Distributed Cache Service
API Gateway
Distributed Message Service for Kafka
Distributed Message Service for RabbitMQ
Distributed Message Service for RocketMQ
Cloud Service Engine
Multi-Site High Availability Service
EventGrid
Dedicated Cloud
Dedicated Computing Cluster
Business Applications
Workspace
ROMA Connect
Message & SMS
Domain Name Service
Edge Data Center Management
Meeting
AI
Face Recognition Service
Graph Engine Service
Content Moderation
Image Recognition
Optical Character Recognition
ModelArts
ImageSearch
Conversational Bot Service
Speech Interaction Service
Huawei HiLens
Video Intelligent Analysis Service
Developer Tools
SDK Developer Guide
API Request Signing Guide
Terraform
Koo Command Line Interface
Content Delivery & Edge Computing
Content Delivery Network
Intelligent EdgeFabric
CloudPond
Intelligent EdgeCloud
Solutions
SAP Cloud
High Performance Computing
Developer Services
ServiceStage
CodeArts
CodeArts PerfTest
CodeArts Req
CodeArts Pipeline
CodeArts Build
CodeArts Deploy
CodeArts Artifact
CodeArts TestPlan
CodeArts Check
CodeArts Repo
Cloud Application Engine
MacroVerse aPaaS
KooMessage
KooPhone
KooDrive

User-Defined Data Type Conversion

Updated on 2024-09-06 GMT+08:00

Description

There are three data type conversion modes for PostgreSQL: implicit conversion, assignment conversion, and explicit conversion. They correspond to i (Implicit), a (Assignment), and e (Explicit) in the pg_cast system catalog.

  • Implicit conversion: a conversion from low bytes to high bytes of the same data type, for example, from int to bigint
  • Assignment conversion: a conversion from high bytes to low bytes of the same data type, for example, from smallint to int
  • Explicit conversion: a conversion between different data types

How to Use

  1. Before converting data types, you can run the following command to check whether RDS for PostgreSQL supports data type conversion:
    select * from pg_catalog.pg_cast ;
    oid  | castsource | casttarget | castfunc | castcontext | castmethod 
    -------+------------+------------+----------+-------------+------------
     11277 |         20 |         21 |      714 | a           | f
     11278 |         20 |         23 |      480 | a           | f
     11279 |         20 |        700 |      652 | i           | f
     11280 |         20 |        701 |      482 | i           | f
    ......
  2. Run the following command to check whether int4 can be converted to text:
    select * from pg_catalog.pg_cast where castsource = 'int4'::regtype and casttarget = 'bool'::regtype;
      oid  | castsource | casttarget | castfunc | castcontext | castmethod 
    -------+------------+------------+----------+-------------+------------
     11311 |         23 |         16 |     2557 | e           | f
    (1 row)

    The conversion is supported, and the conversion type is implicit conversion.

    If no built-in conversion functions are available, customize a conversion function to support the conversion. For details, see User-Defined Data Type Conversion.

User-Defined Data Type Conversion

  • Use double colons (::) to perform a forcible conversion.
    select '10'::int,'2023-10-05'::date;
     int4 |    date    
    ------+------------
       10 | 2023-10-05
    (1 row)
  • Use the CAST function to convert the type.
    select CAST('10' as int),CAST('2023-10-05' as date);
     int4 |    date    
    ------+------------
       10 | 2023-10-05
    (1 row)
  • Customize a data type conversion.

    For details, see https://www.postgresql.org/docs/14/sql-createcast.html.

    NOTICE:

    Adding a custom type conversion will affect the existing execution plans of RDS for PostgreSQL. Therefore, customizing type conversions are not recommended.

    • Conversion between time and character types

      CREATE CAST(varchar as date) WITH INOUT AS IMPLICIT;

    • Conversion between boolean types and numeric types

      create cast(boolean as numeric) with INOUT AS IMPLICIT;

    • Conversion between numeric types and character types

      create cast(varchar as numeric) with INOUT AS IMPLICIT;

    Example: Convert text to date.

    create or replace function public.text_to_date(text) returns date as 
    $$          
      select to_date($1,'yyyy-mm-dd');  
    $$ 
    language sql strict;  
    
    create cast (text as date) with function public.text_to_date(text) as implicit;  
    
    select text '2023-09-09' + 1;
      ?column?  
    ------------
     2023-09-10
    (1 row)

We use cookies to improve our site and your experience. By continuing to browse our site you accept our cookie policy. Find out more

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback