Skip to content
Advertisement

How do I convert to date data_type after concatenating?

I have a query where I am concatenating month and year and saving it as MY, which converts it to string. Need help converting to date data_type from string after concatenating.

select concat (month(date),'-', year(date)) as MY, post_evar10, device_type, sum(pageviews) as pageviews, count(distinct uniquevisitors) as uniquevisitors
from temp.MS_Adobe_Discover1
group by MY, post_evar10, device_type
order by MY asc;

Advertisement

Answer

If you want to convert to a date in Sparksql:

select to_date(concat(year(date), '-', month(date), '-01')) as yyyymmdd,
       post_evar10, device_type,
       sum(pageviews) as pageviews, count(distinct uniquevisitors) as uniquevisitors
from temp.MS_Adobe_Discover1
group by yyyymmdd, post_evar10, device_type
order by yyyymmdd asc;
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement