Home > database >  trying to cast an int data type into date
trying to cast an int data type into date

Time:07-08

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

CodePudding user response:

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.

  • Related