RSS Forums RSS
Please support our MySQL advertiser: Programming Forums
Views: 1357 | Replies: 11
Reply
Join Date: Jun 2008
Posts: 47
Reputation: kvdd is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 2
kvdd kvdd is offline Offline
Light Poster

IF THEN statement would not work

  #1  
Jul 8th, 2008
hello, I try the following query:
  1. SELECT IF TIME(hour_end) > '21:00:00' THEN TIMEDIFF(TIME(hour_end),'21:00') AS diff ENDIF FROM `hours`

Why whould this not work?
Any help would be appreciated.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2008
Posts: 47
Reputation: kvdd is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 2
kvdd kvdd is offline Offline
Light Poster

Re: IF THEN statement would not work

  #2  
Jul 8th, 2008
Now I have this working query, but I preffer one result instead of two in the query below. And I would perform that with a IF else, but that is not working.

Here is the query:
  1. SELECT
  2. hour_id,
  3. TIMEDIFF( TIME( hour_end ) , '21:00' ) AS diff1,
  4. TIMEDIFF( TIME( hour_end_o ) , '21:00' ) AS diff2
  5. FROM `hours`
  6. WHERE (
  7. TIME( hour_end ) > '21:00'
  8. OR
  9. TIME( hour_end_o ) > '21:00'
  10. )
  11. AND (
  12. TIME( hour_end ) != '00:00:00'
  13. OR TIME( hour_end_o ) != '00:00:00'
  14. )

My question is now, I only want one diff as total, not two differencess(diff1 and diff2 in the query) as in the query above. How do I do that?
Last edited by kvdd : Jul 8th, 2008 at 8:27 am.
Reply With Quote  
Join Date: Jun 2008
Posts: 47
Reputation: kvdd is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 2
kvdd kvdd is offline Offline
Light Poster

Re: IF THEN statement would not work

  #3  
Jul 8th, 2008
Code optimalisation:

  1. SELECT
  2. hour_id,
  3. TIMEDIFF( TIME( hour_end ) , '21:00:00' ) AS diff1,
  4. TIMEDIFF( TIME( hour_end_o ) , '21:00:00' ) AS diff2
  5. FROM `hours`
  6. WHERE (
  7. TIME(hour_end) > '21:00:00'
  8. OR
  9. TIME(hour_end_o) > '21:00:00'
  10. )

Daniweb where is your responsetime within 3 hours!!


BTW: question is the same: How to get one diff instead of two difference (e.g. diff1 and diff2 in the query).
Reply With Quote  
Join Date: Dec 2004
Location: London or Slovakia
Posts: 2,712
Reputation: peter_budo is just really nice peter_budo is just really nice peter_budo is just really nice peter_budo is just really nice 
Rep Power: 12
Solved Threads: 322
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: IF THEN statement would not work

  #4  
Jul 8th, 2008
Originally Posted by kvdd View Post

Daniweb where is your responsetime within 3 hours!!


Who told you there is 3h respond time? Everybody here has his/her own life beside dani who is here 24/7
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, JAVAWUG (Java Web User Group), Coding the Architecture
Reply With Quote  
Join Date: Jun 2008
Posts: 65
Reputation: varmadba is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 7
varmadba varmadba is offline Offline
Junior Poster in Training

Re: IF THEN statement would not work

  #5  
Jul 8th, 2008
Try This
 
SELECT 
HOUR_ID,
IF(TIME(hour_end) > '21:00:00',     
   TIMEDIFF(TIME( hour_end ),'21:00:00'),
   IF(TIME(hour_end_o) > '21:00:00',TIMEDIFF( TIME( hour_end_o ) , '21:00:00'),'hour_end_o LESS THAN 21:00:00 HOURS')) AS DIFF
FROM HOURS 

this query will assume that both hour_end and hour_end_o wont be greater than 21:00:00
:- Varma

Please Join Mysql Community:
http://www.orkut.com/Community.aspx?cmm=52345386
Reply With Quote  
Join Date: Apr 2008
Posts: 295
Reputation: tesuji is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 41
tesuji tesuji is offline Offline
Posting Whiz in Training

Re: IF THEN statement would not work

  #6  
Jul 8th, 2008
Originally Posted by kvdd View Post
  1. SELECT IF TIME(hour_end) > '21:00:00' THEN TIMEDIFF(TIME(hour_end),'21:00') AS diff ENDIF FROM `hours`



What do you think of correct syntax?
  1. SELECT IF ... THEN ... ELSE ... ENDIF AS diff

krs,
tesu
Information is moving—you know, nightly news is one way, of course, but it's also moving through the blogosphere and through the Internets. I promise you I will listen to what has been said here, even though I wasn't here. Ann and I will carry out this equivocal message to the world. I'm the master of low expectations.
Reply With Quote  
Join Date: Jun 2008
Posts: 47
Reputation: kvdd is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 2
kvdd kvdd is offline Offline
Light Poster

Re: IF THEN statement would not work

  #7  
Jul 9th, 2008
@varmadba: that did not work.

@peter_budo: you know it was a little joke

@tesuji: you are right, only the AS diff must outside the IF?

This code does not work:
  1. SELECT
  2. IF (TIME(hour_end) > '21:00:00')
  3. THEN TIMEDIFF(TIME(hour_end),'21:00:00')
  4. AS diff
  5. ENDIF
  6. FROM
  7. `hours`
Error: You use the wrong syntax, look at the manual .. for the right syntax to use near ')
THEN TIMEDIFF(TIME(' .. blabla

This does also not work:
  1. SELECT
  2. CASE WHEN (
  3. TIME( hour_end ) > '21:00:00'
  4. )
  5. THEN (
  6. TIMEDIFF( TIME( hour_end) , '21:00:00' )
  7. )
  8. END AS diff
  9. FROM `hours`
Error : I get only NULL results.

Anybody knows something about IF THEN statements or CASE WHEN THEN statements?
Reply With Quote  
Join Date: Apr 2008
Posts: 295
Reputation: tesuji is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 41
tesuji tesuji is offline Offline
Posting Whiz in Training

Re: IF THEN statement would not work

  #8  
Jul 9th, 2008
hi kvdd,

if statement is correct, also your case statement seams to be correct. You can put an else part to it to assign a value instead of getting NULL if if condition evaluates false. Two reasons why you might not get the expected results:

1. no hour_end value satisfies the condition TIME( hour_end ) > '21:00:00' (possibly already checked)

2. incompatible time formats, check the result from this select statement:
select hour_end, TIME( hour_end ), '21:00:00', TIMEDIFF( TIME( hour_end) , '21:00:00' )
from 'hours' .

You may choose sql strict mode to get all error messages.

krs,
tesu
Information is moving—you know, nightly news is one way, of course, but it's also moving through the blogosphere and through the Internets. I promise you I will listen to what has been said here, even though I wasn't here. Ann and I will carry out this equivocal message to the world. I'm the master of low expectations.
Reply With Quote  
Join Date: Jun 2008
Posts: 47
Reputation: kvdd is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 2
kvdd kvdd is offline Offline
Light Poster

Re: IF THEN statement would not work

  #9  
Jul 9th, 2008
2) returns:
2008-05-19 09:00:00
09:00:00
21:00:00
-12:00:00
.. and here more of course

I have set mysql to strict mode, but I dont get a better error with this query. My goal is to get only a result back when hour_end is more then 21:00. Otherwise, I cannot calculate with 21:00.
But this query returns an error, and I think the syntax is correct
  1. SELECT
  2. IF (TIME(hour_end) > '21:00:00')
  3. THEN TIMEDIFF(TIME(hour_end),'21:00:00')
  4. AS diff
  5. ENDIF
  6. FROM
  7. `hours`
Last edited by kvdd : Jul 9th, 2008 at 6:59 am.
Reply With Quote  
Join Date: Apr 2008
Posts: 295
Reputation: tesuji is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 41
tesuji tesuji is offline Offline
Posting Whiz in Training

Re: IF THEN statement would not work

  #10  
Jul 9th, 2008
Dear kvdd

Your IF - statement is again WRONG. Pls, look at syntax in post #6 again.

>> 2) returns:
2008-05-19 09:00:00
09:00:00
21:00:00
-12:00:00
.. and here more of course

Yeah, here is more ! Did you ever see a + sign in front of the time difference?

Of course, so what about item #1, did you ever check whether time part of hour_end really satisfies condition TIME( hour_end ) > '21:00:00' ?

krs,
tesu
Information is moving—you know, nightly news is one way, of course, but it's also moving through the blogosphere and through the Internets. I promise you I will listen to what has been said here, even though I wasn't here. Ann and I will carry out this equivocal message to the world. I'm the master of low expectations.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 1:54 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC