Mysql concatenate extract results using string

Current code:

WHERE EXTRACT(YEAR_MONTH FROM timestamp_field) = EXTRACT(YEAR_MONTH FROM now())")

      

Instead EXTRACT(YEAR_MONTH FROM now())

, I want EXTRACT(YEAR FROM now())

, but I want to write down the month code. How do I combine the extraction results with MM month like 09.

I tried several options below with no luck.

(EXTRACT(YEAR FROM now())09)

CONCAT(EXTRACT(YEAR FROM now()), 09) 

'EXTRACT(YEAR FROM now())' + '09'

      

+2


source to share


1 answer


You almost had this:

SELECT CONCAT(EXTRACT(YEAR FROM now()), '09');

      

The " +

" operator is not intended for string concatenation unless you are using Microsoft SQL Server.



In MySQL use CONCAT()

or if you set the SQL mode to ANSI

or PIPES_AS_CONCAT

, you can use the standard " ||

" operator :

SET SQL_MODE := 'ANSI';
SELECT EXTRACT(YEAR FROM now()) || '09';

      

+2


source







All Articles