Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (55.6k points)

Can anyone explain how to use CAST function in SQL?

1 Answer

0 votes
by (119k points)
edited by

CAST () function is used to change the string from one datatype to another.

Here are the few examples of how to use CAST () function:

The following statement to convert a decimal number to Integer:

SELECT CAST (5.95 AS INT) result1;

We will get the following result:

result1

-------------

5

The following statement to convert a decimal to another decimal with different length: 

SELECT CAST (5.95 AS DEC (3,0)) result2;

result2

-------------

6

The following statement to convert a string to a datetime value:

SELECT CAST ('2020-05-02' AS DATETIME) result3;

result3

----------------------------------

2020-05-02 00:00:00.000

In order to learn SQL from top experts, you can register for this Microsoft SQL Certification program by Intellipaat that provides instructor-led training, and also certification.

Related questions

0 votes
1 answer
asked Jul 9, 2019 in SQL by Tech4ever (20.3k points)
0 votes
1 answer
asked Jul 24, 2019 in SQL by Tech4ever (20.3k points)
0 votes
1 answer
0 votes
1 answer

Browse Categories

...