tags:

views:

614

answers:

10

I have a form that records a student ID number. Some of those numbers contain a leading zero. When the number gets recorded into the database it drops the leading 0.

The field is set up to only accept numbers. The length of the student ID varies.

I need the field to be recorded and displayed with the leading zero.

+9  A: 

If you are always going to have a number of a certain length (say, it will always be 10 characters), then you can just get the length of the number in the database (after it is converted to a string) and then add the appropriate 0's.

However, if this is an arbitrary amount of leading zeros, then you will have to store the content as a string in the database so you can capture the leading zeros.

casperOne
In this case the length varies. and recording as a string is the last resort -- switching everyting over to record text.is there a way to do it still keeping the field as numbers?
AO, I think you will have to add another column called LeadingZero to record the leading 0 (null by default). Then if the leading 0 is non-null then prefix it on the displays.
Turnkey
I would say not to use a bit column for leading zero, but use an int column to store number of leading zeros and store as a numeric (per my answer below)
BenAlabaster
An integer with significant leading zeroes is not an integer, it's a string. Storing it some other way sounds like a terrible hack that'll break things like searches.
bobince
+6  A: 

It sounds like this should be stored as string data. It sounds like the leading zeros are part of the data itself, not just part of it's formatting.

You could reformat the data for display with the leading zeros in it, however I believe you should store the correct form of the ID number, it will lead to less bugs down the road (ex: you forgot to format it in one place but not in another).

Jim Petkus
A: 

See here: Pad or remove leading zeroes from numbers

SQLMenace
A: 
declare @recordNumber integer;
set @recordNumber = 93088;
declare @padZeroes integer;
set @padZeroes = 8;
select 
     right( replicate('0',@padZeroes) 
          + convert(varchar,@recordNumber), @padZeroes);
Michael Buen
+2  A: 

Unless you intend on doing calculations on that ID, its probably best to store them as text/string.

Optimal Solutions
A: 

Another option is since the field is an id, i would recommend creating a secondary field for display number (nvarchar) that you can use for reports, etc...

Then in your application when the student id is entered you can insert that into the database as the number, as well as the display number.

Tom Anderson
+4  A: 

There are a few ways of doing this - depending on the answers to my comments in your question:

  1. Store the extra data in the database by converting the datatype from numeric to varchar/string.

    • Advantages: Very simple in its implementation; You can treat all the values in the same way.
    • Disadvantage: If you've got very large amounts of data, storage sizes will escalate; indexing and sorting on strings doesn't perform so well.
    • Use if: Each number may have an arbitrary length (and hence number of zeros).
    • Don't use if: You're going to be spending a lot of time sorting data, sorting numeric strings is a pain in the ass - look up natural sorting to see some of the pitfalls;
  2. Continue to store the data in the database as numeric but pad the numeric back to a set length (i.e. 10 as I have suggested in my example below):

    • Advantages: Data will index better, search better, not require such large amounts of storage if you've got large amounts of data.
    • Disadvantage: Every query or display of data will require every data instance to be padded to the correct length causing a slight performance hit.
    • Use if: All the output numbers will be the same length (i.e. including zeros they're all [for example] 10 digits); Large amounts of sorting will be necessary.
  3. Add a field to your table to store the original length of the numeric, continue to store the value as numeric (to leverage sorting/indexing performance gains of numeric vs. string) in your new field store the length as it would include the significant zeros:

    • Advantages: Reduction in required storage space; maximum use of indexing; sorting of numerics is far easier than sorting text numerics; You still get the ability to pad numerics to arbitrary lengths like you have with option 1.
    • Disadvantages: An extra field is required in your database, so all your queries will have to pull that extra field thus potentially requiring a slight increase in resources at query/display time.
    • Use if: Storage space/indexing/sorting performance is any sort of concern.
    • Don't use if: You don't have the luxury of changing the table structure to include the extra value; This will overcomplicate already complex queries.

If I were you and I had access to modify the db structure slightly, I'd go with option 3, sure you need to pull out an extra field to get the length. The slightly increased complexity pays huge dividends in the advantages versus the disadvantages. The performance hit of padding the string back out the correct length will be far superceded by the performance increase of the indexing and storage space required.

BenAlabaster
+1 for option 3, nice amalgamation of options to gain the best overall effect...
BobTheBuilder
+1  A: 

I worked with a database with a similar problem. They were storing zip codes as a number. The consequence was that people in New Jersey couldn't use our app.

You're using data that is logically a text string and not a number. It just happens to look like a number, but you really need to treat it as text. Use a text-oriented data type, or at least create a database view that enables you to pull back a properly formatted value for this.

Chris Farmer
Here, here. If something isn't going to be used in a calculation store it as text. 25 years and I've never been sorry I did.
Jim Blizard
A: 

An Oracle solution

Store the ID as a number and convert it into a character for display. For instance, to display 42 as a zero-padded, three-character string:

SQL> select to_char(42, '099') from dual;

 042

Change the format string to fit your needs.

(I don't know if this is transferable to other SQL flavors, however.)

Jon Ericson
A: 

You could just concatenate '1' to the beginning of the ID when storing it in the database. When retrieving it, treat it as a string and remove the first char.

MySQL Example:

SET @student_id = '123456789';

INSERT INTO student_table (id,name) VALUES(CONCAT('1',@student_id),'John Smith');
...
SELECT SUBSTRING(id,1) FROM student_table;

Mathematically:

Initially I thought too much and did it mathematically by adding an integer to the student ID, depending on its length (like 1,000,000,000 if it's 9 digits), before storing it.

SET @new_student_id = ABS(@student_id) + POW(10, CHAR_LENGTH(@student_id));
INSERT INTO student_table (id,name) VALUES(@new_student_id,'John Smith');
Kristian B