Search code examples
sqldatecastinginteger

trying to cast an int data type into date


How do I convert int to date using cast in the format setting below.

month column is an int type that want to be converted to date.

SELECT x y z
  FROM table 
  WHERE name = 'S'
    AND lastname = 'Y'
    AND month= startdate

Solution

  • Something like this should work for you (as you said in the comments):

    create table test(
      date_as_int int,
      date_as_date date
    );
    insert into test(date_as_int, date_as_date)
    values (20220705, '20220705'),
    (20220704, '20220604'),
    (20200507, '20200707');
    
    select * 
    from test
    where cast(cast(date_as_int as char(8)) as date) >= date_as_date
    

    db<>fiddle

    So in your case change and month >= stardate to and cast(cast(month as char(8)) as date) >= startdate.