IBM DB2 to dbt Model Conversion
Purpose
Transform IBM DB2 DDL (views, tables, stored procedures) into production-quality dbt models
compatible with Snowflake, maintaining the same business logic and data transformation steps while
following dbt best practices.
When to Use This Skill
Activate this skill when users ask about:
- Converting DB2 views or tables to dbt models
- Migrating DB2 stored procedures to dbt
- Translating DB2 SQL syntax to Snowflake
- Generating schema.yml files with tests and documentation
- Handling DB2-specific syntax conversions (Inline SQL PL, FETCH FIRST, CONTINUE/EXIT handlers)
Task Description
You are a database engineer working for a hospital system. You need to convert IBM DB2 DDL to
equivalent dbt code compatible with Snowflake, maintaining the same business logic and data
transformation steps while following dbt best practices.
Input Requirements
I will provide you the DB2 DDL to convert.
Audience
The code will be executed by data engineers who are learning Snowflake and dbt.
Output Requirements
Generate the following:
- One or more dbt models with complete SQL for every column
- A corresponding schema.yml file with appropriate tests and documentation
- A config block with materialization strategy
- Explanation of key changes and architectural decisions
- Inline comments highlighting any syntax that was converted
Conversion Guidelines
General Principles
- Replace procedural logic with declarative SQL where possible
- Break down complex procedures into multiple modular dbt models
- Implement appropriate incremental processing strategies
- Maintain data quality checks through dbt tests
- Use Snowflake SQL functions rather than macros whenever possible
Sample Response Format
-- dbt model: models/[domain]/[target_schema_name]/model_name.sql
{{ config(materialized='view') }}
/* Original Object: [database].[schema].[object_name]
Source Platform: IBM DB2
Purpose: [brief description]
Conversion Notes: [key changes]
Description: [SQL logic description] */
WITH source_data AS (
SELECT
customer_id::INTEGER AS customer_id,
customer_name::VARCHAR(100) AS customer_name,
account_balance::NUMBER(18,2) AS account_balance,
created_date::TIMESTAMP_NTZ AS created_date
FROM {{ ref('upstream_model') }}
),
transformed_data AS (
SELECT
customer_id,
UPPER(customer_name)::VARCHAR(100) AS customer_name_upper,
account_balance,
created_date,
CURRENT_TIMESTAMP()::TIMESTAMP_NTZ AS loaded_at
FROM source_data
)
SELECT
customer_id,
customer_name_upper,
account_balance,
created_date,
loaded_at
FROM transformed_data
## models/[domain]/[target_schema_name]/_models.yml
version: 2
models:
- name: model_name
description: "Table description; converted from IBM DB2 [Original object name]"
columns:
- name: customer_id
description: "Primary key - unique customer identifier"
tests:
- unique
- not_null
- name: customer_name_upper
description: "Customer name in uppercase"
- name: account_balance
description: "Current account balance; Foreign key to OTHER_TABLE"
tests:
- relationships:
to: ref('OTHER_TABLE')
field: OTHER_TABLE_KEY
- name: created_date
description: "Date the customer record was created"
- name: loaded_at
description: "Timestamp when the record was loaded by dbt"
## dbt_project.yml (excerpt)
models:
my_project:
+materialized: view
domain_name:
+schema: target_schema_name
Specific Translation Rules
dbt Specific Requirements
- If the source is a view, use a view materialization in dbt
- Include appropriate dbt model configuration (materialization type)
- Add documentation blocks for a schema.yml
- Add descriptions for tables and columns
- Include relevant tests
- Define primary keys and relationships
- Assume that upstream objects are models
- Comprehensively provide all the columns in the output
- Break complex procedures into multiple models if needed
- Implement appropriate incremental strategies for large tables
- Use Snowflake SQL functions rather than macros whenever possible
- Always cast columns with explicit precision/scale using
::TYPE syntax (e.g.,
column_name::VARCHAR(100), amount::NUMBER(18,2)) to ensure output matches expected data types
- Always provide explicit column aliases for clarity and documentation
Performance Optimization
- Suggest clustering keys if needed
- Recommend materialization strategy (view vs table)
- Identify potential performance improvements
DB2 to Snowflake Syntax Conversion
- Convert FETCH FIRST n ROWS ONLY to LIMIT n
- Replace compound statements with Snowflake Scripting
- Convert CONTINUE/EXIT handlers to exception handling
- Translate inline SQL PL to Snowflake Scripting
- Handle CURRENT DATE/TIMESTAMP differences
- Convert DB2 string functions
- Replace EXCEPT/INTERSECT if using non-ANSI syntax
Key Data Type Mappings
| DB2 |
Snowflake |
Notes |
| INTEGER/INT |
INTEGER |
|
| BIGINT |
BIGINT |
|
| SMALLINT |
SMALLINT |
|
| DECIMAL/NUMERIC |
DECIMAL |
|
| REAL/FLOAT |
FLOAT |
|
| DOUBLE |
DOUBLE |
|
| CHAR/VARCHAR |
Same |
|
| CLOB |
VARCHAR |
Max 16MB |
| BLOB |
BINARY |
Max 8MB |
| DATE |
DATE |
|
| TIME |
TIME |
|
| TIMESTAMP |
TIMESTAMP |
|
| XML |
VARIANT |
|
Key Syntax Conversions
-- FETCH FIRST -> LIMIT
SELECT * FROM table FETCH FIRST 10 ROWS ONLY -> SELECT * FROM table LIMIT 10
-- CURRENT DATE/TIMESTAMP (no parentheses in DB2)
CURRENT DATE -> CURRENT_DATE()
CURRENT TIMESTAMP -> CURRENT_TIMESTAMP()
-- CONTINUE/EXIT handlers -> Exception handling
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION ... ->
EXCEPTION WHEN OTHER THEN ...
-- VALUES clause
VALUES (1, 'a'), (2, 'b') -> SELECT 1, 'a' UNION ALL SELECT 2, 'b'
Common Function Mappings
| DB2 |
Snowflake |
Notes |
COALESCE(a, b) |
COALESCE(a, b) |
Same |
IFNULL(a, b) |
IFNULL(a, b) |
Same |
VALUE(a, b) |
NVL(a, b) |
|
NULLIF(a, b) |
NULLIF(a, b) |
Same |
SUBSTR(str, pos, len) |
SUBSTR(str, pos, len) |
Same |
TRIM(str) |
TRIM(str) |
Same |
UPPER/LOWER |
Same |
|
LENGTH(str) |
LENGTH(str) |
Same |
LOCATE(search, str) |
POSITION(search IN str) |
|
DECIMAL(val, p, s) |
val::NUMBER(p, s) |
|
INTEGER(val) |
val::INTEGER |
|
VARCHAR(val) |
val::VARCHAR |
|
DATE(val) |
val::DATE |
|
CURRENT DATE |
CURRENT_DATE() |
Add parentheses |
CURRENT TIMESTAMP |
CURRENT_TIMESTAMP() |
Add parentheses |
DAYS(d) |
DATEDIFF('day', '0001-01-01', d) |
Days since epoch |
YEAR/MONTH/DAY(d) |
YEAR/MONTH/DAY(d) |
Same |
Dependencies
- List any upstream dependencies
- Suggest model organization in dbt project
Validation Checklist
- [] Every DDL statement has been accounted for in the dbt models
- [] SQL in models is compatible with Snowflake
- [] DB2-specific syntax converted (FETCH FIRST, compound statements, handlers)
- [] All business logic preserved
- [] All columns included in output
- [] Data types correctly mapped
- [] Functions translated to Snowflake equivalents
- [] Materialization strategy selected
- [] Tests added
- [] SQL logic description complete
- [] Table descriptions added
- [] Column descriptions added
- [] Dependencies correctly mapped
- [] Incremental logic (if applicable) verified
- [] Inline comments added for converted syntax
Related Skills
- $dbt-migration - For the complete migration workflow (discovery, planning, placeholder models,
testing, deployment)
- $dbt-modeling - For CTE patterns and SQL structure guidance
- $dbt-testing - For implementing comprehensive dbt tests
- $dbt-architecture - For project organization and folder structure
- $dbt-materializations - For choosing materialization strategies (view, table, incremental,
snapshots)
- $dbt-performance - For clustering keys, warehouse sizing, and query optimization
- $dbt-commands - For running dbt commands and model selection syntax
- $dbt-core - For dbt installation, configuration, and package management
- $snowflake-cli - For executing SQL and managing Snowflake objects
Supported Source Database
| Database |
Key Considerations |
| IBM DB2 |
Inline SQL PL, FETCH FIRST, CONTINUE/EXIT handlers, compound statements |
Translation References
Detailed syntax translation guides are available in the translation-references/ folder.
Copyright Notice: The translation reference documentation in this repository is derived from
Snowflake SnowConvert Documentation
and is © Copyright Snowflake Inc. All rights reserved. Used for reference purposes only.
Reference Index
- Continue Handler
- Create Function
- Create Procedure
- Create Table
- Create View
- Data Types
- Exit Handler
- From Clause
- Overview (README)
- Select Statement
- Subqueries
Converted and distributed by TomeVault — claim your Tome and manage your conversions.
1---2name: dbt-migration-db23description: Transform IBM DB2 DDL (views, tables, stored procedures) into production-quality dbt models Use when this capability is needed.4---56# IBM DB2 to dbt Model Conversion78## Purpose910Transform IBM DB2 DDL (views, tables, stored procedures) into production-quality dbt models11compatible with Snowflake, maintaining the same business logic and data transformation steps while12following dbt best practices.1314## When to Use This Skill1516Activate this skill when users ask about:1718- Converting DB2 views or tables to dbt models19- Migrating DB2 stored procedures to dbt20- Translating DB2 SQL syntax to Snowflake21- Generating schema.yml files with tests and documentation22- Handling DB2-specific syntax conversions (Inline SQL PL, FETCH FIRST, CONTINUE/EXIT handlers)2324---2526## Task Description2728You are a database engineer working for a hospital system. You need to convert IBM DB2 DDL to29equivalent dbt code compatible with Snowflake, maintaining the same business logic and data30transformation steps while following dbt best practices.3132## Input Requirements3334I will provide you the DB2 DDL to convert.3536## Audience3738The code will be executed by data engineers who are learning Snowflake and dbt.3940## Output Requirements4142Generate the following:43441. One or more dbt models with complete SQL for every column452. A corresponding schema.yml file with appropriate tests and documentation463. A config block with materialization strategy474. Explanation of key changes and architectural decisions485. Inline comments highlighting any syntax that was converted4950## Conversion Guidelines5152### General Principles5354- Replace procedural logic with declarative SQL where possible55- Break down complex procedures into multiple modular dbt models56- Implement appropriate incremental processing strategies57- Maintain data quality checks through dbt tests58- Use Snowflake SQL functions rather than macros whenever possible5960### Sample Response Format6162```sql63-- dbt model: models/[domain]/[target_schema_name]/model_name.sql64{{ config(materialized='view') }}6566/* Original Object: [database].[schema].[object_name]67 Source Platform: IBM DB268 Purpose: [brief description]69 Conversion Notes: [key changes]70 Description: [SQL logic description] */7172WITH source_data AS (73 SELECT74 customer_id::INTEGER AS customer_id,75 customer_name::VARCHAR(100) AS customer_name,76 account_balance::NUMBER(18,2) AS account_balance,77 created_date::TIMESTAMP_NTZ AS created_date78 FROM {{ ref('upstream_model') }}79),8081transformed_data AS (82 SELECT83 customer_id,84 UPPER(customer_name)::VARCHAR(100) AS customer_name_upper,85 account_balance,86 created_date,87 CURRENT_TIMESTAMP()::TIMESTAMP_NTZ AS loaded_at88 FROM source_data89)9091SELECT92 customer_id,93 customer_name_upper,94 account_balance,95 created_date,96 loaded_at97FROM transformed_data98```99100```yaml101## models/[domain]/[target_schema_name]/_models.yml102version: 2103104models:105 - name: model_name106 description: "Table description; converted from IBM DB2 [Original object name]"107 columns:108 - name: customer_id109 description: "Primary key - unique customer identifier"110 tests:111 - unique112 - not_null113 - name: customer_name_upper114 description: "Customer name in uppercase"115 - name: account_balance116 description: "Current account balance; Foreign key to OTHER_TABLE"117 tests:118 - relationships:119 to: ref('OTHER_TABLE')120 field: OTHER_TABLE_KEY121 - name: created_date122 description: "Date the customer record was created"123 - name: loaded_at124 description: "Timestamp when the record was loaded by dbt"125```126127```yaml128## dbt_project.yml (excerpt)129models:130 my_project:131 +materialized: view132 domain_name:133 +schema: target_schema_name134```135136### Specific Translation Rules137138#### dbt Specific Requirements139140- If the source is a view, use a view materialization in dbt141- Include appropriate dbt model configuration (materialization type)142- Add documentation blocks for a schema.yml143- Add descriptions for tables and columns144- Include relevant tests145- Define primary keys and relationships146- Assume that upstream objects are models147- Comprehensively provide all the columns in the output148- Break complex procedures into multiple models if needed149- Implement appropriate incremental strategies for large tables150- Use Snowflake SQL functions rather than macros whenever possible151- **Always cast columns with explicit precision/scale** using `::TYPE` syntax (e.g.,152 `column_name::VARCHAR(100)`, `amount::NUMBER(18,2)`) to ensure output matches expected data types153- **Always provide explicit column aliases** for clarity and documentation154155#### Performance Optimization156157- Suggest clustering keys if needed158- Recommend materialization strategy (view vs table)159- Identify potential performance improvements160161#### DB2 to Snowflake Syntax Conversion162163- Convert FETCH FIRST n ROWS ONLY to LIMIT n164- Replace compound statements with Snowflake Scripting165- Convert CONTINUE/EXIT handlers to exception handling166- Translate inline SQL PL to Snowflake Scripting167- Handle CURRENT DATE/TIMESTAMP differences168- Convert DB2 string functions169- Replace EXCEPT/INTERSECT if using non-ANSI syntax170171#### Key Data Type Mappings172173| DB2 | Snowflake | Notes |174| --------------- | --------- | -------- |175| INTEGER/INT | INTEGER | |176| BIGINT | BIGINT | |177| SMALLINT | SMALLINT | |178| DECIMAL/NUMERIC | DECIMAL | |179| REAL/FLOAT | FLOAT | |180| DOUBLE | DOUBLE | |181| CHAR/VARCHAR | Same | |182| CLOB | VARCHAR | Max 16MB |183| BLOB | BINARY | Max 8MB |184| DATE | DATE | |185| TIME | TIME | |186| TIMESTAMP | TIMESTAMP | |187| XML | VARIANT | |188189#### Key Syntax Conversions190191```sql192-- FETCH FIRST -> LIMIT193SELECT * FROM table FETCH FIRST 10 ROWS ONLY -> SELECT * FROM table LIMIT 10194195-- CURRENT DATE/TIMESTAMP (no parentheses in DB2)196CURRENT DATE -> CURRENT_DATE()197CURRENT TIMESTAMP -> CURRENT_TIMESTAMP()198199-- CONTINUE/EXIT handlers -> Exception handling200DECLARE CONTINUE HANDLER FOR SQLEXCEPTION ... ->201EXCEPTION WHEN OTHER THEN ...202203-- VALUES clause204VALUES (1, 'a'), (2, 'b') -> SELECT 1, 'a' UNION ALL SELECT 2, 'b'205```206207#### Common Function Mappings208209| DB2 | Snowflake | Notes |210| ----------------------- | ---------------------------------- | ---------------- |211| `COALESCE(a, b)` | `COALESCE(a, b)` | Same |212| `IFNULL(a, b)` | `IFNULL(a, b)` | Same |213| `VALUE(a, b)` | `NVL(a, b)` | |214| `NULLIF(a, b)` | `NULLIF(a, b)` | Same |215| `SUBSTR(str, pos, len)` | `SUBSTR(str, pos, len)` | Same |216| `TRIM(str)` | `TRIM(str)` | Same |217| `UPPER/LOWER` | Same | |218| `LENGTH(str)` | `LENGTH(str)` | Same |219| `LOCATE(search, str)` | `POSITION(search IN str)` | |220| `DECIMAL(val, p, s)` | `val::NUMBER(p, s)` | |221| `INTEGER(val)` | `val::INTEGER` | |222| `VARCHAR(val)` | `val::VARCHAR` | |223| `DATE(val)` | `val::DATE` | |224| `CURRENT DATE` | `CURRENT_DATE()` | Add parentheses |225| `CURRENT TIMESTAMP` | `CURRENT_TIMESTAMP()` | Add parentheses |226| `DAYS(d)` | `DATEDIFF('day', '0001-01-01', d)` | Days since epoch |227| `YEAR/MONTH/DAY(d)` | `YEAR/MONTH/DAY(d)` | Same |228229#### Dependencies230231- List any upstream dependencies232- Suggest model organization in dbt project233234---235236## Validation Checklist237238- [] Every DDL statement has been accounted for in the dbt models239- [] SQL in models is compatible with Snowflake240- [] DB2-specific syntax converted (FETCH FIRST, compound statements, handlers)241- [] All business logic preserved242- [] All columns included in output243- [] Data types correctly mapped244- [] Functions translated to Snowflake equivalents245- [] Materialization strategy selected246- [] Tests added247- [] SQL logic description complete248- [] Table descriptions added249- [] Column descriptions added250- [] Dependencies correctly mapped251- [] Incremental logic (if applicable) verified252- [] Inline comments added for converted syntax253254---255256## Related Skills257258- $dbt-migration - For the complete migration workflow (discovery, planning, placeholder models,259 testing, deployment)260- $dbt-modeling - For CTE patterns and SQL structure guidance261- $dbt-testing - For implementing comprehensive dbt tests262- $dbt-architecture - For project organization and folder structure263- $dbt-materializations - For choosing materialization strategies (view, table, incremental,264 snapshots)265- $dbt-performance - For clustering keys, warehouse sizing, and query optimization266- $dbt-commands - For running dbt commands and model selection syntax267- $dbt-core - For dbt installation, configuration, and package management268- $snowflake-cli - For executing SQL and managing Snowflake objects269270---271272## Supported Source Database273274| Database | Key Considerations |275| ----------- | ----------------------------------------------------------------------- |276| **IBM DB2** | Inline SQL PL, FETCH FIRST, CONTINUE/EXIT handlers, compound statements |277278## Translation References279280Detailed syntax translation guides are available in the `translation-references/` folder.281282> **Copyright Notice:** The translation reference documentation in this repository is derived from283> [Snowflake SnowConvert Documentation](https://docs.snowflake.com/en/migrations/snowconvert-docs)284> and is © Copyright Snowflake Inc. All rights reserved. Used for reference purposes only.285286### Reference Index287288- [Continue Handler](translation-references/db2-continue-handler.md)289- [Create Function](translation-references/db2-create-function.md)290- [Create Procedure](translation-references/db2-create-procedure.md)291- [Create Table](translation-references/db2-create-table.md)292- [Create View](translation-references/db2-create-view.md)293- [Data Types](translation-references/db2-data-types.md)294- [Exit Handler](translation-references/db2-exit-handler.md)295- [From Clause](translation-references/db2-from-clause.md)296- [Overview (README)](translation-references/db2-readme.md)297- [Select Statement](translation-references/db2-select-statement.md)298- [Subqueries](translation-references/db2-subqueries.md)299300---301> Converted and distributed by [TomeVault](https://tomevault.io/claim/sfc-gh-dflippo) — claim your Tome and manage your conversions.302<!-- tomevault:4.0:skill_md:2026-04-11 -->