Home > Net >  Convert a SELECT column to Date in MySql
Convert a SELECT column to Date in MySql

Time:04-25

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.

CodePudding user response:

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

CodePudding user response:

You can cast an expression not a statement.

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