cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results for 
Search instead for 
Did you mean: 

Convert date to integer

elgeo
Valued Contributor II

Hello. Is there a way in Databricks sql to convert a date to integer? In Db2, there is days function DAYS - IBM Documentation .

For example '2023-03-01' is converted to 738580 value.

Thank you in advance

1 ACCEPTED SOLUTION

Accepted Solutions

SergeRielau
Valued Contributor
Valued Contributor

TRy this:

CREATE OR REPLACE FUNCTION days(dt DATE) RETURN unix_date(dt) - unix_date(DATE'0001-01-01') + 1;

SELECT current_date, days(current_date); 

2023-03-09 738588

I verified on Db2 for LUW and it matches up.

View solution in original post

2 REPLIES 2

-werners-
Esteemed Contributor III

SergeRielau
Valued Contributor
Valued Contributor

TRy this:

CREATE OR REPLACE FUNCTION days(dt DATE) RETURN unix_date(dt) - unix_date(DATE'0001-01-01') + 1;

SELECT current_date, days(current_date); 

2023-03-09 738588

I verified on Db2 for LUW and it matches up.

Join 100K+ Data Experts: Register Now & Grow with Us!

Excited to expand your horizons with us? Click here to Register and begin your journey to success!

Already a member? Login and join your local regional user group! If there isn’t one near you, fill out this form and we’ll create one for you to join!