Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Store an integer with a set number of digits

Author  Topic 

Apples
Posting Yak Master

146 Posts

Posted - 2009-01-14 : 14:50:00
I'm trying to store an integer in a table as "07" but it gets put in there as "7".

I want all the integers to have 2 digits; if they are a single digit number, they should have a zero in front of it.

How can I do this?

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-01-14 : 14:54:53
You must use a character data type, such as char or varchar. You can't use an integer data type for this or any of the number data types.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2009-01-14 : 14:56:00
Integer columns do not have a defined display format, except in the context of an application.

If you want a leading zero on value stored as integer datatype, you can add that when you retrieve it from the database, or let your front-end application handle the formatting.






CODO ERGO SUM
Go to Top of Page

rohitkumar
Constraint Violating Yak Guru

472 Posts

Posted - 2009-01-14 : 15:03:12
quote:
Originally posted by tkizer

You must use a character data type, such as char or varchar.
Tara Kizer



and if you're doing that remember 07 is now not same as 7
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-14 : 23:59:41
quote:
Originally posted by Apples

I'm trying to store an integer in a table as "07" but it gets put in there as "7".

I want all the integers to have 2 digits; if they are a single digit number, they should have a zero in front of it.

How can I do this?


mke field as varchar and use

RIGHT('0' + integervalue,2)
for value
Go to Top of Page
   

- Advertisement -