convert data type in sql 1

convert data type in sql

# There are two ways to change data type. By using CAST or ::
# Eg. CAST (column_name AS target_type) OR column_name::target_type

SELECT
	CAST (date_occurred AS DATE)
    CAST (total_amount AS INTEGER)
    CAST (duration AS INTERVAL)
# OR    
    date_occurred::DATE
    total_amount::INTEGER
    duration::INTERVAL

Here is what the above code is Doing:
1. The first line of code is selecting the date_occurred column and casting it as a date.
2. The second line of code is selecting the total_amount column and casting it as an integer.
3. The third line of code is selecting the duration column and casting it as an interval.

Similar Posts