Skip to main content
All CollectionsData Tips, Tools, And Analytics
Data Type Considerations for Source System Integration
Data Type Considerations for Source System Integration
Openbridge Support avatar
Written by Openbridge Support
Updated over 7 months ago

When integrating data from source systems, such as Amazon, it is vital to consider the data types of the fields being loaded into the target database. In many cases, fields like `OrderDate` or `OrderID,` typically representing numeric values, are stored as `VARCHAR` in the database. This decision ensures the broadest possible compatibility and mitigates the risk of data load failures due to data type conflicts.

Why VARCHAR is Used Instead of Specific Data Types

Source systems, including Amazon, may not always guarantee adherence to a specific data type for all records. For example, fields like `OrderDate` or `OrderID` might sometimes contain non-decimal, non-integer, or even non-numeric values.

If the target database expects a specific data type, such as `INTEGER` or `DECIMAL,` and the source system supplies a value that does not conform to that data type, the data load process will fail due to a data type conflict.

To avoid such unpredictable load failures, it is common practice to default to using `VARCHAR` for fields with inconsistent data types identified in the source system. By storing the data as `VARCHAR,` the target database can accommodate a wide range of values, regardless of their original data type. This approach significantly increases the probability of a successful data load.

Data Compatibility and Flexibility

Permissive data types like `VARCHAR` allow for excellent compatibility and flexibility when integrating data from diverse source systems. Different systems may store data in various formats or data types, and enforcing strict data type constraints in the target database can lead to data load failures if the incoming data does not conform to the expected format.

By using `VARCHAR,` the target database can accommodate a wide range of values, regardless of their original data type. This flexibility ensures that data can be successfully loaded into the target system, even if the source systems have inconsistent or varying data types for specific fields.

Avoiding Data Load Failures

Data load failures can occur when there is a mismatch between the expected data type in the target database and the actual data type supplied by the source system. For example, the data load process will fail if a field is defined as an `INTEGER` in the target database, the source system supplies a non-integer value.

Using permissive data types like `VARCHAR` significantly reduces the risk of data load failures due to data type mismatches. The target database can accept and store the data as a string, regardless of its original format. This approach increases the reliability and stability of the data integration process.

Handling Evolving Data Schemas

Source systems may undergo changes or updates to their data schemas over time. New fields may be added, or existing fields may be modified to accommodate different data types. If the target database has strict data type constraints, such schema changes in the source systems can break the data integration process.

The target database can adapt more quickly to evolving data schemas using permissive data types. It can handle data type variations without requiring immediate database schema modifications. This flexibility allows for a smoother, more maintainable data integration process, even as the source systems evolve.

Data Transformation and Validation

Using permissive data types does not compromise data integrity or validation. Data transformation and validation processes can still be applied to the loaded data to ensure its quality and consistency.

After loading the data into the target database as `VARCHAR,` data transformation routines can be implemented to convert the data into the desired format or data type. This can involve parsing, casting, or applying format-specific validation rules to the data. Separating the data loading and transformation steps makes the overall data integration process more modular and manageable.

Casting VARCHAR to Specific Data Types

Although the base data type in the database table is `VARCHAR,` it is possible to cast the values to a specific data type when needed. Most database systems provide functions or mechanisms to convert `VARCHAR` values to other data types, such as `INTEGER,` `DECIMAL,` or `DATETIME,` as long as the values are in a compatible format.

For example, a `ItemQty` field stored as `VARCHAR` contains numeric values that can be cast to an `INTEGER` or `DECIMAL` type using the appropriate casting function in the database query or view. Similarly, date or timestamp values are stored as `VARCHAR` can be cast to the corresponding `DATE` or `TIMESTAMP` data types.

To cast a VARCHAR column to a DATETIME in an Amazon Redshift view, you can use the `CAST` function and the appropriate datetime format. Here's an example of how you can do that:

Suppose you have a table named `my_table` with a VARCHAR column named `date_string` containing date values in the format 'YYYY-MM-DD' format. To create a view that casts this column to a DATETIME, you can use the following SQL statement:

CREATE VIEW my_view AS
SELECT
CAST(date_string AS DATETIME) AS date_column,
-- other columns
FROM
my_table;

In this example:

  • The `CAST` function converts the `date_string` column to a DATETIME data type.

  • The `AS DATETIME` part specifies the target data type for the cast operation.

  • The resulting casted column is aliased as `date_column` in the view.

You can then use the view `my_view` to access the cast DATETIME column `date_column`.

If your VARCHAR column contains datetime values in a different format; you may need to use the appropriate formatting function or specify the format explicitly in the `CAST` function. For example, if the date format is 'MM/DD/YYYY', you can use:

CAST(date_string AS DATETIME FORMAT 'MM/DD/YYYY')

Make sure to replace `'MM/DD/YYYY'` with the actual format of your date strings.

If your VARCHAR column contains datetime values in a different format, you may need to use the appropriate formatting function in BigQuery. For example, if the date format is 'MM/DD/YYYY', you can use the PARSE_DATE function:

SELECT
PARSE_DATE('%m/%d/%Y', date_string) AS date_column,
-- other columns
FROM
my_table;

In this case, the PARSE_DATE function is used to parse the date_string column based on the specified format string '%m/%d/%Y'. The resulting parsed date is aliased as date_column. Similarly, using the appropriate casting functions, you can cast other data types in BigQuery views.

Here are a few more examples:

Casting VARCHAR to INTEGER:

SELECT CAST(quantity_string AS INT64) AS quantity_column, -- other columns FROM my_table;

Casting VARCHAR to FLOAT:

SELECT CAST(amount_string AS FLOAT64) AS amount_column, -- other columns FROM my_table;

Casting VARCHAR to TIMESTAMP:

SELECT CAST(timestamp_string AS TIMESTAMP) AS timestamp_column, -- other columns FROM my_table;

Make sure to replace '%m/%d/%Y' or other format strings with the actual format of your data strings. By casting the VARCHAR column to a DATETIME in this view, you can perform date-related operations and queries on the casted column more easily. You can still perform type-specific operations and queries on the fields by leveraging data type casting, even though they are stored as `VARCHAR` in the underlying table.

Summary

Storing fields as `VARCHAR` in the database is a strategic decision to ensure compatibility and resilience when integrating data from source systems. It accommodates inconsistent data types supplied by the source system, reducing the risk of data load failures. While the base data type is `VARCHAR,` the values can be cast to specific data types, enabling type-specific operations and queries. This approach provides flexibility and reliability in handling data from various source systems.

Did this answer your question?