Can '%VALUE%' be written so it gets current year automaticlly

71 Views Asked by At

The query looks like this: Datatype for month.ty is text

month.ty LIKE '%2024%'

I am trying to make it get year without having to change it when year ends. Like get current year.

I have tryied with plus special character:

month.ty LIKE '%' + YEAR(current_timestamp) + '%'

This way it pops an error what ever I put inside the plus characters. Error is: Warning: #1292 Truncated incorrect DOUBLE value: '%'

Also like this:

month.ty LIKE YEAR(CURRENT_TIMESTAMP)

In this case it returns nothing. But this is probbaby since it does not go into comparing cause there is no wildcard sign '%'.

And also like this:

month.ty LIKE '%' + CAST(year(CURRENT_TIMESTAMP) AS varchar) + '%'

In this case it throws an error #1064 - You have an error in your SQL syntax;

Also tryed:

month.ty LIKE '%' + YEAR(GETDATE()) + '%'

Throws error #1558 - Column count of mysql.proc is wrong. Expected 21, found 20. Created with MariaDB 100108, now running 100421. Please use mysql_upgrade to fix this error

I am not sure if I am missing something or there is some other way for it to work. XAMPP version is 8.1.6-0 / Server version: 10.4.21-MariaDB

1

There are 1 best solutions below

4
SelVazi On BEST ANSWER

As mentioned in the comments, it's not advisable to keep dates in a text format. Instead, always use a date or timestamp type.

The problem with your query is that you're attempting to concatenate strings using the operator +, when you should be using the CONCAT() function instead :

WHERE month.ty LIKE CONCAT('%', YEAR(current_timestamp), '%')