Nishant Khanna
Nishant Khanna

Reputation: 59

What is the benefit of cast()

What is the benefit of cast() here?

SELECT Grp_number
    ,Mobile
    ,cast(NULL AS VARCHAR(10)) Card
FROM PROFILE
WHERE country = 'United Arab Emirates'

Thanks in advance, Nishant

Upvotes: 0

Views: 76

Answers (2)

Lukasz Szozda
Lukasz Szozda

Reputation: 175954

If you don't use CAST you will get column with data type INT.

SELECT Grp_number
    ,Mobile
    ,cast(NULL AS VARCHAR(10)) Card       -- VARCHAR(10)
FROM PROFILE
WHERE country = 'United Arab Emirates'

SELECT Grp_number
    ,Mobile
    ,NULL Card                            -- INT
FROM PROFILE
WHERE country = 'United Arab Emirates';

This may cause problems with data type mapping (ORM/SSIS/reporting tools).

Upvotes: 2

AswinRajaram
AswinRajaram

Reputation: 1632

The data type of Card may not have been set as varchar(10) initially. Cast() helps to explicitly set the column as varchar(10) here.

Upvotes: 0

Related Questions