Nik's Technology Blog

Travels through programming, networks, and computers

Importing data from one Database to another with slightly different sized fields?

Preparation is the key to a faultless import.

Sometimes it's useful to know the maximum length of the data you store in your database. Even if you know the maximum size of the cell, the actual data stored may be smaller.

In Microsoft SQL use:

SELECT MAX(LEN(firstname))

FROM tblData

This MS SQL command will find the maximum length of the data in the firstname field of tblData table.

Using this method you can avoid having to increase the destination tables field sizes in cases where you haven't actually used the full capacity in the source database.

So if the source field size for firstname is varchar 50 and the destination is varchar 40, you can use the above SQL to determine if anyone's first name is actually longer than 40 characters. If it's not then you don't need to increase the cell size in the destination table.

If you get the following error when you try to use the LEN() SQL statement above, then you are trying to find the maximum length of a text field.

Error: Argument data type text is invalid for argument 1 of len function.

For text fields in SQL use Datalength() instead of LEN().


FROM tblJobs

The Datalength() function which will return the length of any expression, in this case the maximum length of the description field from the tblJobs table.

This can be used on all data types including text, ntext, image and varbinary.

Of course a perfectly designed database would have as little spare capacity in fields as possible, but with fields such as people's names this is hard to achieve this.