I’ve got a column in my database which contains a price, stored as a varchar(10). The format that I need to pull it out as has a comma, but no decimal. So, if the price is 1500.00121, it should come out as “1,500”, with comma intact. So here is what I have so far:
CONVERT(varchar, CAST(p.Price1 AS money), 1)
This still has the decimal place to ".xx" in it. How can I remove the decimal and trailing numbers while retaining the comma?
Why not stored it in
Decimal(10,2)datatype? It’s much better than storing it inVARCHARsince you don’t have extra casting to another datatype.You can use
CASTandROUNDfunction:That’s it. The
CASTfunction converts datatype to another datatype. TheROUNDfunction returns a numeric value, rounded to the specified length or precision.The original syntax for
ROUNDisWhere
Functionparameter is the type of operation to perform.Functionmust be tinyint, smallint, or int. When function is omitted or has a value of 0 (default), numeric_expression is rounded. When a value other than 0 is specified, numeric_expression is truncated.SQLFiddle Demo
ROUND (MSDN)
Decimal (MSDN)