Skip to content
IGCSE·Tuition

ICT · Help with common difficulties

My database imports numbers as text

The data looks right on screen, yet the sort order is strange and your query misses records you can see.

On this page
  1. What is actually going wrong?
  2. A worked example
  3. The mistake to watch for
  4. Which number-like values should stay text?
  5. Check yourself
  6. Where this leads next

When a database reads a number field as text, it sorts character by character, so 100 lands before 9, and any test such as “less than 20” gives wrong results. The fix is to clean the values and set the correct field type at the import step, then check a sample against the source.

This page supports database structure and validation and the lesson on detecting an incorrect numeric-text interpretation. The steps are software-neutral, because the wording of menus changes between products and versions.

What is actually going wrong?

A database field has a data type. Text fields store characters and compare them left to right. Number fields store a value and compare it by size. During an import, the software guesses the type from the values it sees, or it uses the type you chose.

There are three usual causes:

  1. A stray character in one row, such as “RM 2.00”, a trailing space or a comma in “1,200”. One bad value can turn the whole column into text.
  2. A text default accepted at the import screen. The software offered text for every column and you clicked through.
  3. A value that should be text but was treated as a number, which is the reverse problem: a phone number loses its leading zero.

A worked example

A fictional canteen file has four rows.

ItemIDItemPriceStock
C01Nasi lemak3.5012
C02Teh aisRM 2.009
C03Roti canai1.50100
C04Kuih0.8040

The student imports it and leaves every column on text. Then they sort Stock ascending and get 100, 12, 40, 9. Text sorting compares the first character first: “100” and “12” both start with 1, and “0” is lower than “2”, so 100 comes before 12. Then 40 and 9 follow because 4 is lower than 9.

The query Stock < 20 returns 100 and 12 as text. It misses 9, because “9” is higher than “2”. The correct numeric answer is 12 and 9.

The fix, step by step:

  1. Keep the original file and work on a copy.
  2. Clean the source: change “RM 2.00” to 2.00. Show the RM with a currency format in the database, not inside the value.
  3. Set types at import: ItemID text, Item text, Price number (decimal), Stock number (whole).
  4. Check: sort Stock both ways. You should now see 9, 12, 40, 100 ascending.
  5. Re-run the query: Stock < 20 should return 12 and 9 only.

You can rehearse the sorting and filtering idea with fictional data in the read-only SQL practice lab.

The mistake to watch for

A student changes the Price field to a number after the import and sees the RM 2.00 value go blank. They assume the software is faulty. In fact the value could not be read as a number, so it was dropped or flagged. The correction is to clean the value first, then convert, and always compare records with the original.

Which number-like values should stay text?

ValueTypeReason
Phone number starting with 0TextLeading zero matters; never calculated
Postcode 08000TextLeading zero matters
Student ID S0042TextContains a letter and a code pattern
Quantity in stockNumberSorted and compared by size
PriceNumberAdded, averaged, compared

The test is simple: will I add, average or compare it by size? If yes, use a number type.

Check yourself

1. Stock values 5, 40 and 7 are sorted ascending as text. What order appears?

Show answer

Text compares the first character: 4 is lower than 5, and 5 is lower than 7. So the order is 40, 5, 7.

2. A column of ages imports as text because one cell holds “16 yrs”. What are two sensible fixes?

Show answer

Clean the source cell to 16 and set the field type to number before importing, or fix the cell in the database and then convert the type. In both cases check a sample against the original file.

3. Why is a phone number better stored as text?

Show answer

It is an identifier, not a quantity. A number type can remove the initial zero, and you never add or average phone numbers.

Where this leads next

Practise choosing types from sample values with choosing field types, and then importing a small fictional dataset. When you need to show your result as evidence, the ICT practical task and evidence checker gives a software-neutral checklist, and the lesson on capturing evidence that shows the relevant setting covers the screenshot side.

Import problems are easy to fix once you know where to look, and harder when the wrong setting sits on a screen you clicked past. Online one-to-one ICT tuition lets a teacher watch that step with you.

Questions people ask

Why does my database treat a number as text?

Usually because one value in the column contains something that is not a digit, such as a currency label, a space or a comma, or because the import step was left on a text default. The software then picks the type that fits every value, and text always fits. Clean the source or set the field type before importing.

Should phone numbers and ID codes be numbers?

No. Phone numbers, postcodes and codes such as 0123 keep leading zeros and are never added or averaged, so text is the right type. Use a number type only for values you may calculate with, compare by size or sort by size.

Can I just change the field type after the import?

Sometimes, but values that cannot be converted may be lost or flagged as errors, depending on the software. Keep a copy of the original file first, clean the values that hold extra characters, then change the type and check a sample of records against the source.

How do I know the type is wrong without running a query?

Sort the column both ways and read the first and last rows. If 100 comes before 9, the field is being sorted as text. Also look at how the values align: many programs left-align text and right-align numbers, though this depends on the software.

Sources

  1. Cambridge IGCSE ICT 0417 syllabus page

Updated:

Your next step

If imported data keeps behaving strangely and you cannot tell which field is to blame, a paid one-hour trial lets a teacher watch your import on screen and point to the exact setting.

Paid one-hour trial at your assigned teacher’s confirmed rate, starting from RM80. Other fees, schedules and ongoing arrangements are confirmed directly with your teacher after the trial class.

9,000+ students helped through our service