Data types

<< Database administration | Migration from MS SQL to Firebird | SQL syntax >>

Data types

This section describes the different data types available in Firebird and MS SQL, and how to translate types from one system to another.

MS SQL has different data types, depending on the version. The following table lists the data types along with the version in which they were introduced.

Table 1. Data types conversion table
MS SQL
Ver
Data typeFirebirdMS SQL definition and comments
6.5bigintINT648-byte integer type.
6.5binaryCHARFixed-length binary data with a maximum length of 8,000 bytes. In 6.5, maximum was 255.
6.5bitCHAR(1)Integer data with either a 1 or 0 value. Typically, replaced by constants 'T' and 'F'.
6.5charCHARFixed-length non-Unicode character data with a maximum length of 8,000 characters. In 6.5, maximum was 255. Firebird can hold up to 32,767 characters.
6.5cursor A reference to a cursor. This can only be used inside stored procedures or triggers; it cannot be used on table declarations.
6.5datetimeTIMESTAMPDate and time data from January 1, 1753, to December 31, 9999, with an accuracy of three-hundredths of a second, or 3.33 milliseconds.
6.5decimalDECIMALFixed precision and scale numeric data from -1038 -1 through 1038 -1.
6.5floatFLOATFloating precision number data from -1.79E + 308 through 1.79E + 308.
6.5imageBLOBVariable-length binary data with a maximum length of 231 - 1 (2,147,483,647) bytes.
6.5intINTEGERInteger (whole number) data from -231 (-2,147,483,648) through 231 - 1 (2,147,483,647).
6.5moneyDECIMAL(18, 4)Monetary data values from -263 (-922,337,203,685,477.5808) through 263 - 1 (+922,337,203,685,477.5807), with accuracy to a ten-thousandth of a monetary unit.
7ncharCHAR(x) CHARACTER SET UNICODE_FSSFixed-length Unicode data with a maximum length of 4,000 characters.
7ntextBLOB SUB_TYPE TEXTVariable-length Unicode data with a maximum length of 230 - 1 (1,073,741,823) characters.
6.5numericNUMERICIn MS SQL, decimal and numeric are synonyms.
7nvarcharVARCHAR(x) CHARACTER SET UNICODE_FSSFixed-length Unicode data with a maximum length of 4,000 characters.
6.5realDOUBLEFloating precision number data from - 3.40E + 38 through 3.40E + 38.
6.5smalldatetimeTIMESTAMPDate and time data from January 1, 1900, through June 6, 2079, with an accuracy of one minute. Firebird's has greater range and accuracy.
6.5smallintSMALLINTInteger data from -215 (-32,768) through 215 - 1 (32,767).
6.5smallmoneyDECIMAL(10, 4)Monetary data values from -214,748.3648 through +214,748.3647, with accuracy to a ten-thousandth of a monetary unit. Note that Firebird's range is greater with this declaration.
2000sql_variantBLOBAllows the storage of data values of different data types.
2000tablenoneStores results temporarily for later user.
6.5textBLOB SUB_TYPE TEXTVariable-length non-Unicode data with a maximum length of 231 - 1 (2,147,483,647) characters.
6.5timestampINTEGERA database-wide unique number. In Firebird, you will have to manage uniqueness through generators.
6.5tinyintSMALLINTInteger data from 0 through 255. Firebird does not have such a small data type.
6.5varbinaryCHARVariable-length binary data with a maximum length of 8,000 bytes.
6.5varcharVARCHARVariable-length non-Unicode data with a maximum of 8,000 characters. Firebird can hold up to 32,765 characters. In 6.5, maximum was 255.
7uniqueidentifierCHAR(38)A globally unique identifier (GUID). In Firebird, you will have to generate the values with user-defined functions (UDFs).

A subtle difference in the way NUMERIC and DECIMAL behave in Firebird to bear in mind is that the NUMERIC definition means exactly the precision requested (total number of digits), while DECIMAL means at least the requested precision (the digits to the right of the decimal symbol, however, are maintained exactly). In MS SQL, on the other hand, numeric and decimal are synonyms.

There is also a very common quasi-data type, identity, which can only be used when defining tables. This is an int; which is automatically assigned a value on insertion and cannot be changed.

back to top of page

Converting the bit data type

The bit data type is used to hold a single Boolean value, 0 or 1. MS SQL does not support assigning NULL to these fields. InterBase can emulate this with an INTEGER or a CHAR(1) data type.

The acceptable values can be restricted using domains. For more information on Firebird domains, see the Data Definition documentation.

Converting the identity data type

There are many ways to perform the conversion. In general, Firebird is more flexible and powerful in this respect.

The most direct conversion is to create a BEFORE trigger on the table, assigning to the previous column the value from a generator. This ensures that the number is unique.

For added flexibility, a single generator can be used for many tables. In this case, the type would work in a similar way as a timestamp would - by creating a database-wide unique identifier.

Another common technique is to create a stored procedure to allow access to the generator, and allow clients to pre-fetch the number. This is particularly useful for tools such as Delphi which import the NOT NULL constraint on primary keys and refuse to post records with NULL values.

 CREATE TABLE my_table (
   my_number integer not null primary key
 )

 CREATE GENERATOR my_generator

 CREATE TRIGGER my_before_trigger FOR my_table
 BEFORE INSERT
 AS
 BEGIN
   IF (NEW.my_number IS NULL)
     THEN NEW.my_number = GEN_ID(my_generator, 1);
 END 

 CREATE PROCEDURE get_my_generator
 RETURNS (new_value INTEGER)
 AS
 BEGIN
   new_value = GEN_ID(my_generator, 1);
 END

back to top of page

Converting the uniqueidentifier data type

MS SQL depends on uniqueidentifier data types for replication. It is also a handy way of creating a world-wide unique identifier for a record.

To use the field like this, create a BEFORE trigger on the table with the field, and retrieve the value from a UDF.

TODO: write the UDF and write the importing procedure

See also:
Firebird and IBExpert SQL language references

back to top of page
<< Database administration | Migration from MS SQL to Firebird | SQL syntax >>