Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • SQL Server - Get Started
  • Install SQL Server
  • SQL Server Management Studio
  • SQL Server - Windows Authentication
  • SQL Server - Authentication
  • SQL Server - Create New User
  • SQL Server - GRANT/REVOKE Permissions to User
  • SQL Server - Data Types
  • SQL Server - Naming Conventions
  • SQL Server - CREATE Database
  • SQL Server - CREATE Table
  • Add Columns
  • Identity Column
  • Rename Column, Table
  • Drop Columns
  • SQL Server - Schema
  • SQL Server - Tables Relations
  • SQL Server - Primary Keys
  • Modify/Delete Primary Keys
  • SQL Server - Foreign Keys
  • Modify/Delete Foreign Keys
  • SQL Server - Check Constraints
  • SQL Server - Unique Constraints
  • SQL Server - Views
  • Modify/Delete Views
  • SQL Server - Functions
  • SQL Server - Stored Procedures
  • Stored Procedure Parameters
  • SQL Server - Indexes
  • Non-clustered Indexes
  • Modify/Delete Indexes
  • SQL Server - Triggers
  • DDL Triggers
  • LOGON Triggers
  • Enable/Disable Triggers
  • Modify/Delete Triggers
  • SQL Server - Sequence
  • SQL Server - Synonyms
  • SQL Server - IF ELSE Statement
  • SQL Server - Loops
  • SQL Server - Insert Data
  • SQL Server - Update Data
  • SQL Server - Delete Data
  • SQL Server - Select Query
  • WHERE Clause
  • GROUP BY Clause
  • HAVING Clause
  • ORDER BY Clause
  • SQL Server - Inner Join
  • Left Join
  • Right Join
  • Full Join
  • Self Join
  • Dynamic SQL
  • Built-in Functions
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

SQL Server CAST() Function: Convert DataType

SQL Server CAST() function converts an expression of one data type to another data type.

It is recommended to use the TRY_CAST() function over the CAST() function to minimize errors.

CAST(expression AS final_data_type(length))

Parameters

expression: Any valid expression that has to be converted.

final_data_type: The target data type to which you want to convert the given expression. This is an optional parameter.

length: An optional integer that specifies the length of the target data type.

Return Value

Returns the given expression converted to the target data type. It returns an error when converting a non-numeric char, nchar, varchar, or nvarchar value to decimal, float, int, or numeric.

It returns an error when converting an empty string to int or numeric data type.

When you convert data types with different decimal places, then the CAST() function either truncates the resulting value or rounds it off to the higher or lower value.

FromToBehavior
numericNumericRound
numericIntTruncate
numericMoneyRound
moneyIntRound
moneynumericRound
floatintTruncate
floatnumericRound
floatdatetimeRound
datetimeintRound

Example 1:

In the following example, a decimal is converted to an integer data type.

Example: CAST()
SELECT CAST(22.24 AS int) AS Result;

Example 2:

When you convert data types with different decimal places, then the CAST() function either truncates the resulting value or rounds it off to the higher or lower value.

Example: CAST()
SELECT CAST(22.24 AS dec(3,1)) AS Result1, 
    CAST(22.24 AS dec(3,0)) AS Result2;

Example 3:

In the following example, CAST() function is used to convert a string expression to a datetime value.

Example: CAST()
SELECT CAST('11/23/2022' AS datetime) as result;

Example 4:

In the following example, the CAST() is used to concatenate non-character value to a string expression. It is used with the Salary column which is of integer type and converts it to string value.

Example: CAST()
SELECT 'The Salary of ' + FirstName + ' is $' + CAST(Salary AS varchar(10)) FROM Employee;

Example 5:

In the following example, the CAST() function gives an error while converting a string to an int.

Example: CAST()
SELECT CAST( 'abcd' AS int) AS Result;

Example 6:

In the following example, the CAST() function converts a character string to a character of length 5. It truncates string to 5 characters.

Example: CAST()
SELECT CAST('Hello World' AS char(5)) AS Result;

Example 7:

Here, the CAST() is used with the HireDate column to convert from date to datetime.

Example: CAST()
SELECT HireDate, CAST(Hiredate AS datetime) AS Result FROM Employee;
TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.