1

Can anyone help me convert this SQL statement into MySQL, because i'm getting syntax error when i tried to run it from MySQL.

SELECT
    a.userid,
    CAST(a.checktime AS VARCHAR) AS timein,
    ISNULL(CAST(b.checktime AS VARCHAR),'NO TIMEOUT') AS timeout
FROM    timein a
LEFT JOIN timeout b ON a.userid = b.userid
WHERE   ISNULL(CAST(b.checktime),'NO TIMEOUT') = 'NO TIMEOUT'
2
  • Replace ISNULL By IFNULL Commented May 30, 2016 at 6:38
  • Add the error message please Commented May 30, 2016 at 6:39

2 Answers 2

1

Use COALESCE() instead of ISNULL() which is SQL-Server syntax.

SELECT
    a.userid,
    a.checktime AS timein,
    COALESCE(b.checktime,'NO TIMEOUT') AS timeout
FROM    timein a
LEFT JOIN timeout b ON a.userid = b.userid
WHERE   COALESCE(b.checktime,'NO TIMEOUT') = 'NO TIMEOUT'
Sign up to request clarification or add additional context in comments.

4 Comments

thanks, but still getting syntax error near here CAST(a.checktime as varchar) AS timein, COALESCE( CAST(b.checktime as varchar),'NO TIMEOUT') AS timeout
What type is checktime column ?
i declared it as Varchar
@Seryu Then why the hell do you cast it to varchar? Try the query now.
1
SELECT
    a.userid,
    CAST(a.checktime AS CHAR(100)) AS timein,
    IFNULL(CAST(b.checktime AS CHAR(100)),'NO TIMEOUT') AS timeout
FROM    timein a
LEFT JOIN timeout b ON a.userid = b.userid
WHERE   IFNULL(CAST(b.checktime),'NO TIMEOUT') = 'NO TIMEOUT';
  • You need to cast as char datatype. (you need to put the correponding length of the varchar column in CHAR(LENGTH) )
  • ISNULL function in MySQL takes one parameter whereas in SQL SERVER it takes two. You should use IFNULL or COALESCE (as sagi mentioned).

Note about CAST :

The CAST() function converts a value of any type into a value that has a specified type. The target type can be any one of the following types: BINARY, CHAR, DATE, DATETIME, TIME,DECIMAL, SIGNED, UNSIGNED .

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.