Search code examples
mysqlsqlcastingtype-conversion

Convert a SELECT column to Date in MySql


I have this SQL query is MySql, not working:

select email, cast(select tag from user_tags where userid=users.id limit 1,date) as date
from users

The error is: "select" is not valid at this position for this server version, and it highlights the select right after cast.


Solution

  • You can cast a column but not a whole query. So consider using

    SELECT email, 
           (SELECT CAST(tag AS DATE) FROM user_tags WHERE userid=u.id LIMIT 1) AS date
      FROM users AS u
    

    as a correlated subquery containing a column with type conversion