I’m asked to use the CONVERT
function to return the third column as a datatype that outputs 2 digits to the right of the decimal point and all comma’s to the left (i.e. 3, 106.34). Name it FormatTotal.
This is where I’m at.
x
USE AP
SELECT InvoiceTotal,
CAST(InvoiceTotal AS int) intTotal,
CAST(InvoiceTotal AS decimal(8,1)) DecimalTotal
CONVERT(decimal(???), InvoiceTotal) AS FormatTotal
FROM Invoices;
Advertisement
Answer
This looks like a task for format()
:
format(InvoiceTotal, 'N2', 'en-US') as FormatTotal
N
is the format specifier for numbers.
2
gives you the precision (ie the number of decimal digits).
en-US
defines the comma separator for thousands and the dot separator for decimals.