The SQL Server CAST()
function converts an expression from one data type to another. This function is useful for ensuring that data is in the correct format for processing.
SELECT CAST(expression AS data_type);
The CAST()
function takes two arguments:
expression
: The value to be converted.data_type
: The target data type.Let's look at some examples of SQL Server CAST()
function queries:
SELECT CAST('123' AS INT) AS int_value;
This query converts the string '123' to an integer. The result will be:
int_value
---------
123
SELECT CAST(123 AS VARCHAR(10)) AS string_value;
This query converts the integer 123 to a string. The result will be:
string_value
-------------
123
SELECT CAST('2024-06-01' AS VARCHAR(10)) AS string_date;
This query converts the date '2024-06-01' to a string. The result will be:
string_date
------------
2024-06-01
SELECT CAST('2024-06-01' AS DATE) AS date_value;
This query converts the string '2024-06-01' to a date. The result will be:
date_value
----------
2024-06-01
SELECT order_id, CAST(order_amount AS DECIMAL(10, 2)) AS decimal_amount
FROM orders;
This query converts the order_amount
column to a decimal data type with two decimal places. The result will show the original order_id
and the corresponding decimal_amount
.
DECLARE @amount VARCHAR(10) = '123.45';
SELECT CAST(@amount AS DECIMAL(10, 2)) AS decimal_amount;
This query uses a variable to store a string value and then converts it to a decimal data type with two decimal places. The result will be:
decimal_amount
--------------
123.45
Let's go through a complete example that includes creating a table, inserting data, and using the CAST()
function.
This step involves creating a new table named transactions
to store some sample data with transaction amounts.
CREATE TABLE transactions (
id INT PRIMARY KEY,
transaction_amount VARCHAR(50)
);
In this example, we create a table named transactions
with columns for id
and transaction_amount
.
This step involves inserting some sample data into the transactions
table.
INSERT INTO transactions (id, transaction_amount) VALUES (1, '100.50');
INSERT INTO transactions (id, transaction_amount) VALUES (2, '200.75');
INSERT INTO transactions (id, transaction_amount) VALUES (3, '300.25');
Here, we insert data into the transactions
table.
This step involves using the CAST()
function to convert the transaction_amount
column to a decimal data type.
SELECT id, transaction_amount, CAST(transaction_amount AS DECIMAL(10, 2)) AS decimal_amount
FROM transactions;
This query retrieves the id
, transaction_amount
, and the decimal value of the transaction_amount
column for each row in the transactions
table. The result will be:
id transaction_amount decimal_amount
--- ------------------- --------------
1 100.50 100.50
2 200.75 200.75
3 300.25 300.25
The SQL Server CAST()
function is a powerful tool for converting expressions from one data type to another. Understanding how to use the CAST()
function and its syntax is essential for effective data processing and manipulation in SQL Server.