Re: Formatting numeric fields in select-clause

From: Hugo Kornelis (hugo_at_pe_NO_rFact.in_SPAM_fo)
Date: 02/14/05


Date: Mon, 14 Feb 2005 22:58:40 +0100

On Mon, 14 Feb 2005 08:13:01 -0800, Magnus Broman wrote:

>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?

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)


Relevant Pages

  • Re: Set based solution
    ... And SQL IS a data manipulation language, hence my problem with not using it ... that I had always used SQL Server to do data formatting for the past 12 ... instead of coding hooks in a dll to do this formatting, ... SQL, and T-SQL, is not a general purpose programming language. ...
    (microsoft.public.sqlserver.programming)
  • Re: Set based solution
    ... Pro SQL Server 2000 Database Design ... >> that I had always used SQL Server to do data formatting for the past 12 ...
    (microsoft.public.sqlserver.programming)
  • Re: Access vs SQL Date Weirdness = Incompatibility?
    ... There's no need to store formatting with the dates: ... I don't remember the details for SQL Server, but in Jet databases, ... other machines, regardless of what the Short Date format is on the other ... as dd/mm. ...
    (microsoft.public.access.formscoding)
  • Re: How to do this from DTS?
    ... Allan Mitchell MCSE,MCDBA, (Microsoft SQL Server MVP) www.SQLDTS.com - The site for all your DTS needs. ... > The 05 records are built from a second table, where I do similar formatting> on the record, and then do an inner join on an orderid field that's in the> main table. ... > So when if I need to sort it, the orderid would force the 01's, 02's and> 05's for customer X together, as apposed to putting customer Y's data> intermingled with it. ...
    (microsoft.public.sqlserver.dts)
  • Re: How to do this from DTS?
    ... Allan Mitchell MCSE,MCDBA, (Microsoft SQL Server MVP) www.SQLDTS.com - The site for all your DTS needs. ... > The 05 records are built from a second table, where I do similar formatting> on the record, and then do an inner join on an orderid field that's in the> main table. ... > So when if I need to sort it, the orderid would force the 01's, 02's and> 05's for customer X together, as apposed to putting customer Y's data> intermingled with it. ...
    (microsoft.public.sqlserver.programming)