Granvic February 2016

Formatting column data type directly in the query

So, I've got this little problem: I made a select to show some specific data and this is it

SELECT zona, SUM(etotal)
AS total
FROM ft (nolock)
GROUP BY zona

What I intend now is to be able to see the total values already as values. Which is the best way to reach this directly from the query?

Answers


lad2025 February 2016

This type of formatting should be done in application layer.

With SQL Server 2012+ you could use FORMAT:

SELECT zona, 
       total = FORMAT(SUM(etotal), '########.##€')
FROM ft (nolock) 
GROUP BY zona;

LiveDemo

Or:

SELECT zona, 
       total = FORMAT(SUM(etotal), 'C', 'de-de')
FROM ft (nolock) 
GROUP BY zona;

LiveDemo2

Warning:

I hope that you understand how NOLOCK works (possible dirty reads/...).

Post Status

Asked in February 2016
Viewed 1,196 times
Voted 13
Answered 1 times

Search




Leave an answer