Desktop productivity layout

SQL Server - Cast and Convert

Usage of Cast and Convert with an example

In SQL Server, you can convert an INT to VARCHAR using either the CAST or CONVERT function. Here are both options:

Using CAST

1
SELECT CAST(123 AS VARCHAR(10)) AS ConvertedValue;

Using CONVERT

1
SELECT CONVERT(VARCHAR(10), 123) AS ConvertedValue;

Key points

Always specify the length for VARCHAR (e.g., VARCHAR(10)), otherwise SQL Server uses a default length of 30.

If you omit the length, it might cause truncation or unexpected results in some contexts.

Documentation

Reference: Microsoft Learn.

License GPLv3 | Terms
Built with Hugo
Theme Stack designed by Jimmy