Friday, March 23, 2012

Formatting numeric fields in select-clause

This is propably a very simple question, but I can′t seem to find the answer
to it in the documentation.
I want to format a numeric field so the result is right justified and
zero-filled.
ex select 1 will give the result 01
How do I manage this simple task?
best reguards from sunny Sweden
Magnus B
Xref: TK2MSFTNGP08.phx.gbl microsoft.public.sqlserver.mseq:8236
On Mon, 14 Feb 2005 08:13:01 -0800, Magnus Broman wrote:

>This is propably a very simple question, but I cant seem to find the answer
>to it in the documentation.
>I want to format a numeric field so the result is right justified and
>zero-filled.
>ex select 1 will give the result 01
>How do I manage this simple task?
Hi Magnus,
Formatting is usually done by the presentation layer. In fact, the format
SQL Server uses to send numeric values to the client software is quite
different from what you'll ever see on your screen <g>.
If you have valid reasons for doing the formatting at the server, you can;
but you should be aware that the data is then no longer numeric: after
formatting, it's string data.
DECLARE @.number int
SET @.number = 1
SELECT RIGHT('00' + CAST(@.number AS varchar(2)), 2)
Best, Hugo
(Remove _NO_ and _SPAM_ to get my e-mail address)

No comments:

Post a Comment