Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mysql and select if

Status
Not open for further replies.

dsafar

IS-IT--Management
Joined
Jan 18, 2003
Messages
2
Location
US
I have been able to get the following sql to work - &quot;select if(month(policydate)<=6),month(policydate),month(policydate)-6) as mth from mytable&quot;. I would like to be able to do the following
&quot;select if(month(now()<6,(if(month(policydate)>6 ,month(policydate)-6,month(policydate))),
(if(month(policydate)<=6 ,month(policydate)+6,month(policydate))) as mth from mytable&quot;. Anyone been able to get similar syntax to work with mysql?
 
Why not use a case expression?

select case when month(current_date) < 6 then
case when month(policydate) > 6 then
month(policydate) - 6
else
month(policydate) end
else
case when month(policydate) <= 6 then
month(policydate) + 6
else
month(policydate) end
end

It is much easier to read and it is not specific to Mysql.
 
Thank you. Great solution.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top