mysql concatenating extract results with string
-
07-07-2019 - |
Question
Current Code:
WHERE EXTRACT(YEAR_MONTH FROM timestamp_field) = EXTRACT(YEAR_MONTH FROM now())")
Instead of EXTRACT(YEAR_MONTH FROM now())
, I want EXTRACT(YEAR FROM now())
, but I want to hard code the month in. How do I go about concatenating the extract results with the MM month, for example 09.
I tried a few options below, with no luck.
(EXTRACT(YEAR FROM now())09)
CONCAT(EXTRACT(YEAR FROM now()), 09)
'EXTRACT(YEAR FROM now())' + '09'
Solution
You almost had it:
SELECT CONCAT(EXTRACT(YEAR FROM now()), '09');
The "+
" operator is not for string concatenation, unless you use Microsoft SQL Server.
In MySQL, use CONCAT()
or if you set SQL mode to ANSI
or PIPES_AS_CONCAT
you can use the standard "||
" operator:
SET SQL_MODE := 'ANSI';
SELECT EXTRACT(YEAR FROM now()) || '09';
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow