Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change datetime to INT in snowflake?

I have a table that contains a datetime column in datetime format, see picture for example.

enter image description here

I need to convert this timestamp to YYYYMMDD in int, something like this:

enter image description here

I'm trying this query, but it does not work.

select Cast(Cast(Cast (DATETIME AS DATE Format 'YYYYMMDD') AS VARCHAR(8))AS INT) AS DATE_KEY
  FROM TBL_A
like image 304
lalaland Avatar asked Dec 31 '25 17:12

lalaland


1 Answers

  • to_char converts a date using a format
  • ::int casts to int
with data as (
    select current_timestamp() datetime
)


select to_char(datetime, 'YYYYMMDD')::int
from data

(with that said, I wouldn't recommend this type of int representation for a date)

like image 179
Felipe Hoffa Avatar answered Jan 02 '26 09:01

Felipe Hoffa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!