How to cast a value to boolean I need to order a query by the value of a field. If it is empty or not

I need to cast the value of the column order_by to a boolean and the column type is varchar

All rows where order_by is empty or null should be ordered last in the query

SELECT *
FROM table
ORDER BY CAST(order_by, 'boolean')
Here is a concise way using IFNULL() function

SELECT *
FROM TABLE
ORDER BY IFNULL(order_by,'')='';


or adding the IF() function

SELECT *
FROM TABLE
ORDER BY IF(IFNULL(order_by,'')='',1,0);


UPDATE 2017-03-22 14:43 EDT

ypercubeᵀᴹ has one more suggestion:

SELECT *
FROM TABLE
ORDER BY (order_by &gt; '') DESC;