Convert Integer to String in SQL Server

Write the following script in SQL Server. We have three different ways in which we can achieve the desired tasks. We would be examining each one of them and check their output. In the first method we have used CAST function; in the second method we have used CONVERT function and in the third method we have used STR function.

  1. DECLARE @Number INT  
  2.   
  3. SET @Number=12345  
  4.   
  5. -- Using CAST function  
  6.   
  7. SELECT CAST(@Number as varchar(10)) as Num1  
  8.   
  9. -- Using CONVERT function  
  10.   
  11. SELECT CONVERT(varchar(10),@Number) as Num2  
  12.   
  13. -- Using STR function  
  14.   
  15. SELECT LTRIM(STR(@Number,10)) as Num3  
Execute the preceding script and check the output.

output

 

Ebook Download
View all
Learn
View all