Jump to content

SQL query syntax wrong?


mastubbs

Recommended Posts

Hi all,

 

So im quite new to sql queries and ive been reading how to use if else but i ccant figure out what im getting wrong in this one. Can anyone help?

 

I want to use a datetime variable (which is an automatic timestamp 'datetime') but only if the user has not manually over-ridden this by entering their own datetime ('datetime_man). I have tried to use IF ELSE but im getting it wrong somewhere. Is the syntax right?

$Find_Query1 = mysql_query("SELECT DATE_FORMAT(datetime,'%Y/%m/%d %H:%i:%s') AS datetime2 IF datetime_man='null' ELSE DATE_FORMAT(datetime_man,'%Y/%m/%d %H:%i:%s') AS datetime2, DATE_FORMAT(datetime,'%H:%i on %d/%m/%y') AS datetime3 IF datetime_man='null' ELSE DATE_FORMAT(datetime,'%H:%i on %d/%m/%y') AS datetime3, SBP, SBP_R, SBP_B, DBP, DBP_R, DBP_B, HR, HR_R, HR_B, RR, RR_R, RR_B, SpO2, SpO2_R, SpO2_B, O2flow, O2flow_R, O2flow_B, O2device, O2deviceOther, TEMP, TEMP_R, TEMP_B, AVPU FROM addobs WHERE mrn='$Search' AND DATE(datetime)='$Date' AND NOT hidden = 'yes' order by datetime2 ASC");

Thanks in advance for the help,

 

Matt

Link to comment
Share on other sites

Assuming datetime_man is set to NULL when it hasn't been entered, then you can just use the COALESCE function.

 

SELECT 
   DATE_FORMAT(COALESCE(datetime_man, datetime),'%Y/%m/%d %H:%i:%s') AS datetime2 
   , DATE_FORMAT(COALESCE(datetime_man, datetime),'%H:%i on %d/%m/%y') AS datetime3 
   , SBP, SBP_R, SBP_B, DBP, DBP_R, DBP_B, HR, HR_R, HR_B, RR, RR_R, RR_B, SpO2, SpO2_R, SpO2_B, O2flow, O2flow_R, O2flow_B, O2device, O2deviceOther, TEMP, TEMP_R, TEMP_B, AVPU FROM addobs 
WHERE mrn='$Search' AND DATE(datetime)='$Date' AND NOT hidden = 'yes' order by datetime2 ASC");
Edited by kicken
Link to comment
Share on other sites

Assuming datetime_man is set to NULL when it hasn't been entered, then you can just use the COALESCE function.

 

SELECT 
   DATE_FORMAT(COALESCE(datetime_man, datetime),'%Y/%m/%d %H:%i:%s') AS datetime2 
   , DATE_FORMAT(COALESCE(datetime_man, datetime),'%H:%i on %d/%m/%y') AS datetime3 
   , SBP, SBP_R, SBP_B, DBP, DBP_R, DBP_B, HR, HR_R, HR_B, RR, RR_R, RR_B, SpO2, SpO2_R, SpO2_B, O2flow, O2flow_R, O2flow_B, O2device, O2deviceOther, TEMP, TEMP_R, TEMP_B, AVPU FROM addobs 
WHERE mrn='$Search' AND DATE(datetime)='$Date' AND NOT hidden = 'yes' order by datetime2 ASC");

 

Kicken you legend that did the trick. For future reference what was wrong with my IF ELSE syntax?

 

Im guessing from Jessica it should have been

"SELECT IF datetime_man='null' DATE_FORMAT(datetime,'%Y/%m/%d %H:%i:%s') AS datetime2 ELSE DATE_FORMAT(datetime_man,'%Y/%m/%d %H:%i:%s') AS datetime2
Edited by mastubbs
Link to comment
Share on other sites

For one, to compare something to NULL you have to use IS NULL or IS NOT NULL. Using = or != will not work since null is not any particular value, but rather the concept of "no value".

 

Second, I'm not as familar with mysql anymore so not sure if it supports that type of if/else syntax. I know there is the IF() function which you'd use as

SELECT IF(datetime_man IS NOT NULL, DATE_FORMAT(datetime_man,'%Y/%m/%d %H:%i:%s'), DATE_FORMAT(datetime,'%Y/%m/%d %H:%i:%s'))
or, the way I do it for sql server using CASE would work also:

SELECT
  CASE 
    WHEN datetime_man IS NOT NULL THEN DATE_FORMAT(datetime_man,'%Y/%m/%d %H:%i:%s')
    ELSE DATE_FORMAT(datetime,'%Y/%m/%d %H:%i:%s')
  END
Link to comment
Share on other sites

For one, to compare something to NULL you have to use IS NULL or IS NOT NULL. Using = or != will not work since null is not any particular value, but rather the concept of "no value".

 

Second, I'm not as familar with mysql anymore so not sure if it supports that type of if/else syntax. I know there is the IF() function which you'd use as

SELECT IF(datetime_man IS NOT NULL, DATE_FORMAT(datetime_man,'%Y/%m/%d %H:%i:%s'), DATE_FORMAT(datetime,'%Y/%m/%d %H:%i:%s'))
or, the way I do it for sql server using CASE would work also:

SELECT
  CASE 
    WHEN datetime_man IS NOT NULL THEN DATE_FORMAT(datetime_man,'%Y/%m/%d %H:%i:%s')
    ELSE DATE_FORMAT(datetime,'%Y/%m/%d %H:%i:%s')
  END

 

Ah ok, thanks.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.