Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Numeric value to Varchar

I'm trying to fetch the records and append some letters to my numeric column, but I'm getting an error. I tried with cast and convert function.

For example:

select convert(varchar(10),StandardCost +'S')
from DimProduct where ProductKey = 212

here StandardCost is a numeric field, but when I fetch the record I get an error.

like image 307
Praveen k. Agrawal Avatar asked Mar 16 '11 09:03

Praveen k. Agrawal


People also ask

Can we convert int to varchar in SQL?

Converting int to string / varchar using Convert() This Convert() in SQL Server is also a conversion function used to convert a value of one data type to another.

Are numbers allowed in varchar?

So what is varchar in SQL? As the name suggests, varchar means character data that is varying. Also known as Variable Character, it is an indeterminate length string data type. It can hold numbers, letters and special characters.

What is convert function in SQL?

The CONVERT() function converts a value (of any type) into a specified datatype.


2 Answers

i think it should be

select convert(varchar(10),StandardCost) +'S' from DimProduct where ProductKey = 212 

or

select cast(StandardCost as varchar(10)) + 'S' from DimProduct where ProductKey = 212 
like image 170
MayureshP Avatar answered Sep 30 '22 09:09

MayureshP


First convert the numeric value then add the 'S':

 select convert(varchar(10),StandardCost) +'S'  from DimProduct where ProductKey = 212 
like image 43
Ocaso Protal Avatar answered Sep 30 '22 11:09

Ocaso Protal