0% found this document useful (0 votes)
3 views

SQL Server CAST() Function

The SQL Server CAST() function is used to convert a value of any type into a specified datatype. It requires an expression to convert and the target datatype, with optional length for certain types. The function is supported in SQL Server starting from 2008 and is also applicable in Azure SQL Database and Data Warehouse.

Uploaded by

soha.adel66
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

SQL Server CAST() Function

The SQL Server CAST() function is used to convert a value of any type into a specified datatype. It requires an expression to convert and the target datatype, with optional length for certain types. The function is supported in SQL Server starting from 2008 and is also applicable in Azure SQL Database and Data Warehouse.

Uploaded by

soha.adel66
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

1/22/25, 6:01 PM SQL Server CAST() Function

 Tutorials  Exercises  Certificates  Services  Search...  Sign Up Log in

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP R

SQL Server CAST() Function


❮ Previous ❮ SQL Server Functions Next ❯

Example Get your own SQL Server

Convert a value to an int datatype:

SELECT CAST(25.65 AS int);

Try it Yourself »

Definition and Usage


The CAST() function converts a value (of any type) into a specified datatype.

https://www.w3schools.com/sql/func_sqlserver_cast.asp 1/9
1/22/25, 6:01 PM SQL Server CAST() Function

Tip: Also look at the CONVERT() function.


 Tutorials  Exercises  Certificates  Services   Sign Up Log in

 Syntax
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP R

CAST(expression AS datatype(length))

Parameter Values
Value Description

expression Required. The value to convert

datatype Required. The datatype to convert expression to. Can be one of the following:
bigint, int, smallint, tinyint, bit, decimal, numeric, money, smallmoney, float, real,
datetime, smalldatetime, char, varchar, text, nchar, nvarchar, ntext, binary,
varbinary, or image

(length) Optional. The length of the resulting data type (for char, varchar, nchar, nvarchar,
binary and varbinary)

Technical Details
Works in: SQL Server (starting with 2008), Azure SQL Database, Azure SQL Data Warehouse,
Parallel Data Warehouse

More Examples
https://www.w3schools.com/sql/func_sqlserver_cast.asp 2/9
1/22/25, 6:01 PM SQL Server CAST() Function

 Tutorials 
Example
Exercises  Certificates  Services   Sign Up Log in

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP R
Convert a value to a varchar datatype:

SELECT CAST(25.65 AS varchar);

Try it Yourself »

Example
Convert a value to a datetime datatype:

SELECT CAST('2017-08-25' AS datetime);

Try it Yourself »

❮ Previous ❮ SQL Server Functions Next ❯

Track your progress - it's free! Sign Up Log in

https://www.w3schools.com/sql/func_sqlserver_cast.asp 3/9
1/22/25, 6:01 PM SQL Server CAST() Function

 Tutorials  Exercises  Certificates  ADVERTISEMENT


Services   Sign Up Log in

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP R

https://www.w3schools.com/sql/func_sqlserver_cast.asp 4/9
1/22/25, 6:01 PM SQL Server CAST() Function

 Tutorials  Exercises  Certificates  Services   Sign Up Log in

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP R

COLOR PICKER


ADVERTISEMENT

https://www.w3schools.com/sql/func_sqlserver_cast.asp 5/9
1/22/25, 6:01 PM SQL Server CAST() Function

 Tutorials  Exercises  Certificates  Services   Sign Up Log in

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP R

ADVERTISEMENT

https://www.w3schools.com/sql/func_sqlserver_cast.asp 6/9
1/22/25, 6:01 PM SQL Server CAST() Function

 Tutorials  Exercises  Certificates  Services   Sign Up Log in

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP R

ADVERTISEMENT

 PLUS SPACES GET CERTIFIED FOR TEACHERS

FOR BUSINESS CONTACT US

Top Tutorials Top References


HTML Tutorial HTML Reference
CSS Tutorial CSS Reference

https://www.w3schools.com/sql/func_sqlserver_cast.asp 7/9
1/22/25, 6:01 PM SQL Server CAST() Function
JavaScript Tutorial JavaScript Reference

 Tutorials How
 To Tutorial
Exercises 
SQL Tutorial
Certificates  SQL Reference
Services 
Python Reference
 Sign Up Log in
Python Tutorial W3.CSS Reference
HTML
 CSS W3.CSS Tutorial
JAVASCRIPT SQL PYTHON BootstrapPHP
JAVA Reference HOW TO W3.CSS C C++ C# BOOTSTRAP R
Bootstrap Tutorial PHP Reference
PHP Tutorial HTML Colors
Java Tutorial Java Reference
C++ Tutorial Angular Reference
jQuery Tutorial jQuery Reference

Top Examples Get Certified


HTML Examples HTML Certificate
CSS Examples CSS Certificate
JavaScript Examples JavaScript Certificate
How To Examples Front End Certificate
SQL Examples SQL Certificate
Python Examples Python Certificate
W3.CSS Examples PHP Certificate
Bootstrap Examples jQuery Certificate
PHP Examples Java Certificate
Java Examples C++ Certificate
XML Examples C# Certificate
jQuery Examples XML Certificate

    

FORUM ABOUT ACADEMY


W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy
policy.

https://www.w3schools.com/sql/func_sqlserver_cast.asp 8/9
1/22/25, 6:01 PM SQL Server CAST() Function
Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.

 Tutorials  Exercises  Certificates  Services   Sign Up Log in

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP R

https://www.w3schools.com/sql/func_sqlserver_cast.asp 9/9

You might also like