Results 1 to 2 of 2

Thread: Multiple if-else conditions in MySQL

  1. #1

    Thread Starter
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Multiple if-else conditions in MySQL

    Hi All,

    I want to use multiple if-else conditions on the following query.

    Code:
    UPDATE tblTimeZone
    SET GMTZoneName = '(GMT+' +  CAST(ZoneValue AS varchar(5)) + ') ' + ZoneName
    WHERE ZoneValue > 0
    
    UPDATE tblTimeZone
    SET GMTZoneName = '(GMT' +  CAST(ZoneValue AS varchar(5)) + ') ' + ZoneName
    WHERE ZoneValue < 0
    
    UPDATE tblTimeZone
    SET GMTZoneName = '(GMT) ' + ZoneName
    WHERE ZoneValue = 0
    
    UPDATE tblTimeZone
    SET GMTZoneName = 'Select TimeZone'
    WHERE ZoneValue = 100
    Can someone help to convert this into if-else statement.

    I tried to use case statement as follows, but messit

    Code:
    SELECT CASE
    WHEN ZoneValue > 0 
    THEN 
    SET GMTZoneName = '(GMT+' +  CAST(ZoneValue AS varchar(5)) + ') ' + ZoneName
    ELSE 'Negative'
    END GMTZoneName
    FROM tblTimeZone
    “victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Multiple if-else conditions in MySQL

    Maybe something like
    Code:
    DECLARE @ZoneValue int
    SET @ZoneValue = 0
    
    SELECT
    	CASE @ZoneValue
    		WHEN 0 THEN '(GMT) ' + ZoneName'
    		WHEN > 0 THEN '(GMT+' +  CAST(ZoneValue AS varchar(5)) + ') ' + ZoneName
                    WHEN < 0 THEN '(GMT' +  CAST(ZoneValue AS varchar(5)) + ') ' + ZoneName
    		WHEN 100 THEN 'Select TimeZone'
    		ELSE 'Other'
    	END

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width