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
 SQL Server 2000 Forums
 SQL Server Development (2000)
 inserting padded values

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2001-12-06 : 09:58:06
bornmaverick writes "How to insert padded values into SQL server numeric field?
eg. I have a table test(test_id numeric(7)),now what I want is that when I insert a record by the query:
insert into test values(0000001);
and then select the record, like select * from test, it shoudl return the same values that I inserted , ie. 0000001.
In short, I want SQL server to allow insertion of padded fields.

Is that possible?"

robvolk
Most Valuable Yak

15732 Posts

Posted - 2001-12-06 : 10:56:44
You can't pad numeric values with leading zeros. You would need to modify your SELECT statement to convert the numeric column to a fixed-length string, with leading zeros. This should work:

SELECT Replace( Str(test_id, 7, 0), ' ', '0') test_id FROM test

STR() will convert a number to a string with a fixed length and decimal places (here, it's 7 digits long with 0 decimal places). The number is padded on the left with spaces, so the REPLACE() function turns them into 0's.

Check Books Online for the STR(), CONVERT() and REPLACE() functions for more details. They're really handy for stuff like this.

Go to Top of Page
   

- Advertisement -