Search code examples
mysqltoadcoalesceifnull

MySql Toad if now rows return value


I am using mysql toad, i have a table which is empty and i am trying to return a string value if no records are found i have tried the under mention queries however none works, only an empty row is returned.

Query

select coalesce(stDate,'0')as StDate from tblStudents where studentNumber = 12213123;

select IFNULL(stDate,'0')as StDate from tblStudents where studentNumber = 12213123;

The results are only empty strings the column stDate is of type varchar.


Solution

  • Try this

     select Cast(count(*) as char(10)) as StDate
     from tblStudents where studentNumber = 12213123;
    

    You can also use the CASE statement, and if the Student Number is unique, the max() or min() functions to get the stDate from the table.

     select Cast(count(*) as char(10)) as NumRows,max(stDate) as stDate
     from tblStudents where studentNumber = 12213123;
    

    If you only want a single field, try something like this

    select 
    CASE count(*)
        WHEN 0 THEN ''
        ELSE CAST(max(stDate) as char(12))
    END CASE as StartDate
         from tblStudents where studentNumber = 12213123;