SQL CAST Function Convert CHAR to VARCHAR

Here are sample SQL queries on how to use CAST function. Here you can convert CHAR to VARCHAR and DATETIME to DATE.

Refer my part-1 if you miss it. In my next example, I will give converting CHAR column to VARCHAR.

SQL Cast Function CHAR to VARCHAR

Example-1

CAST(CHAR-CLMN  as VARCHAR(100));

In the second way, CREATE DISTINCT TYPE SAL_1 AS DECIMAL(9,2); This is equal to CAST function.

Example-2

CAST(SAL_1 AS DECIMAL(9,2));

Points to Remember

  • The function CAST helps you to change almost all data types into another data types.
  • Before casting we need to make sure that you understand the rules
  • Converting type may have an impact on the result
  • Few people asked that unable to convert INTEGER to STRING. Before doing read all the CAST function rules.  

Source: IBM

Refer here on possibility of data types and rules for CAST.

Keep Reading

Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.