Cast statement
The Cast statement is used to convert a data type from one data type to another data type. If the conversion fails, the function will return an error. Otherwise, it will return the converted value. The Cast statement provides a data type to a dynamic parameter (?) or a NULL value in SQL Server.
CAST(expression AS datatype(length))
Convert statement
The Convert statement is used to convert expressions from one type to another, In many cases there will be a need within a stored procedure or other routine to convert data from a DATETIME type to a VARCHAR type. The Convert statement is used for such things. The Convert() function can be used to display date/time data in various formats in SQL Server datatype.
CONVERT(data_type(length), expression, style)
Using CAST function
The CAST function in SQL Server is used to convert one data type to another. It is used to explicitly convert a value from one data type to another. This function takes two arguments: the expression to be converted and the target data type. In the below example, we retrieve the name of the OrderName for those OrderDetails that have a 1 as the first digit of OrderName, and converts their OrderId values to int.
-- Use CAST
SELECT SUBSTRING(OrderName,1, 40) AS OrderName, OrderAddress
FROM OrderDetails
WHERE CAST( OrderId AS int)LIKE '2%';
The above SQL SELECT statement select the data from “OrderDetails” table and filters the results based on the “OrderId” column. This statement first converts the “OrderId” column to an INT data type using the CAST function, and then uses the LIKE operator to filter the results where the “OrderId” starts with '10'. This SELECT statement uses the SUBSTRING function to truncate the “OrderName” column to the first 40 characters. The second column selected is “orderAddress”.
The query will return rows from the OrderDetails table where OrderId starts with '10' and the OrderName will be truncated to 40 characters, along with orderAddress column of the same rows.
Output

Using CONVERT function
The CONVERT function in SQL Server is used to convert one data type to another. It is used for both explicit and implicit conversions. This function takes two arguments: the expression to be converted and the target data type.
In the below example, we retrieve the name of the OrderName for those OrderDetails that have a 1 as the first digit of OrderName, and converts their OrderId values to int.
SELECT SUBSTRING(OrderName, 1, 20) AS OrderName ,OrderAddress
FROM OrderDetails
WHERE CONVERT(int, OrderId) LIKE '1%';
This SQL query retrieves data from the "OrderDetails" table, and filters the results based on the "OrderId" column. The query first converts the "OrderId" column to an INT data type using the CONVERT function, and then uses the LIKE operator to filter the results where the "OrderId" starts with '8'. This SELECT statement uses the SUBSTRING function to truncate the “OrderName” column to the first 40 characters. The second column selected is “OrderAddress”.
This SQL statement will return rows from the OrderDetail table where OrderId starts with '8' and the OrderName will be truncated to 40 characters, along with orderAddress column of the same rows.
Output

The CAST statement with arithmetic operators
Computed) by dividing the total OrderId by the OrderDetails. This value is rounded to the nearest whole number and is then CAST to an int data type.
-- Use ROUND
SELECT CAST(ROUND(OrderId, 0) AS int) AS Computed
FROM OrderDetails
WHERE OrderId != 0;
The above SQL SELECT statement select the data from “OrderDetail” table and filters the results based on the “OrderId” column. This statement first converts to ROUND the “OrderId” column to Computed Column using the CAST function, and then uses the WHERE codition to filter the results where the “OrderId” =0. This SELECT statement uses the function to truncate the “OrderId” column to the . The second column selected is “0”.The query will return rows from the OrderDetails table.
Output

Using the Cast to concatenate statement
This example concatenates noncharacter expressions by using Cast. It uses the Sample database.
SELECT 'The Order Details ' + CAST(OrderName AS varchar(12)) AS OrderCity
FROM OrderDetails
WHERE OrderId BETWEEN 8 AND 10;
The above SQL SELECT statement select the data from “OrderDetail” table and filters the results based on the 'OrderId” . This statement is select the “OrderName” column to an 'Varchar' data type using the CAST function, and then uses the to filter the result as OrderCity from OrderDetails table and Where OrderId between 8 and 10.
Output

Using CAST to produce more readable text statement
This example uses Cast in the Select list, to convert the OrderName column to a char(50) column.
SELECT DISTINCT CAST(OrderName AS char(50)) AS Name
FROM OrderDetails
WHERE OrderName LIKE 'Apple';
This SQL query Select data from the "OrderDetail" table, and filters the results based on the "OrderName" column. The query first DISTINCT the "OrderName" column to an char data type using the DISTINCT function and as a column 'Name', and then uses the LIKE operator to filter the results where the "OrderName" starts with 'Apple'.
Output

We can see that in the above query we are select the Distinct Cast for OrderName to convert name.
Using Cast with the Like clause statement
This example converts the EmpName,EmpAddress,EmpCity column OrderId values to data type Int and then to data type char(200) so that the Like clause can use it.
SELECT EmpName ,EmpAddress, EmpCity
FROM EmployeeDetail AS p
JOIN OrderDetails AS s
ON s.OrderId = s.OrderId
WHERE CAST(CAST(s.OrderId AS int) AS char(200)) LIKE '1%';
This SQL query retrieves data from the "OrderDetail" table, and Add the second table EmployeeDetail Join the "OrderId" column. The query first converts the "OrderId" column to an INT data type using the CONVERT function, and then uses the LIKE operator to filter the results where the "OrderId" starts with '1%'. This SELECT statement uses the JOIN function to truncate the “OrderId” column to the first 200 characters.
Output

Using Convert or Cast with typed Xml statement
These examples show use of Convert to convert data to typed XML by using the Xml data type and columns SQL Server.
This statement converts a string with white space text and markup into typed XML and removes all insignificant white space (boundary white space between nodes).
SELECT CONVERT(XML, '<root><child/></root>')
The above SQL SELECT statement select and Convert to XML and second 'WHITE SPACE' to namespace XML and root and child.
Output

We can see that in the above query remove white space xml and root section.











Dipak NilePosted Jan 6, 2020, 6:41 PM
Easy to understand ,nice explanation.
Bikesh SrivastavaPosted Dec 16, 2019, 6:41 AM
Well expained
Rushi MehtaPosted Dec 13, 2019, 5:09 AM
Nice article
Sourav Kumar DasPosted Dec 11, 2019, 11:01 PM
Nice useful article Sir. Thanks for sharing.
Onkar SharmaPosted Dec 11, 2019, 10:51 AM
Nice Article...