T.Rose T.Rose - 5 months ago 54
MySQL Question

select distinct (extract(year from saledate) || extract(month from saledate)) as ym from trnsact order by ym;

Working on an exercise question 'How many distinct dates are there in the saledate column of the transaction table for each month/year combination in the database?' I found a query that works and it is obviously combining the YEAR and MONTH

SELECT DISTINCT (extract(year from saledate) || extract(month from saledate)) as SaleDate
FROM trnsact
ORDER BY SaleDate;

But the || is not something we have learned and I cannot find syntax, description or examples anywhere. Can someone please explain?


The || is the string concatenation command. In this query you are giving us, the numbers are converted into string and after that, concatenated.

The solution is valid, I'm afraid.

Another solution would be:

select extract( year from saledate) as year_num, 
       extract( month from saledate) as month_num
  from trnsact
group by extract( year from saledate ), 
         extract( month from saledate);