user3828311
user3828311

Reputation: 967

Casting date to varchar in mysql throws syntax error

Why does MYSQL throw the following error on this simple query

select
  cast(cast(ts as DATE) as varchar)
from
  table_name
limit
  1;

You have an error in your SQL syntax; it seems the error is around: 'varchar) from table_name limit 1' at line 2

Upvotes: 1

Views: 941

Answers (1)

Bill Karwin
Bill Karwin

Reputation: 562368

The CAST() and CONVERT() functions don't support VARCHAR as a type.

They supports BINARY, CHAR, DATE, DATETIME, DECIMAL, DOUBLE, FLOAT, JSON, NCHAR, REAL, SIGNED [INTEGER], TIME, UNSIGNED [INTEGER]. See the manual.

You shouldn't have to cast a DATE to a string type anyway. In many client interfaces, it'll become a string in the result set.

Some interfaces may convert to language-specific types, like java.sql.Date.

Upvotes: 3

Related Questions