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 bkrike 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
Jan 18, 2003
2
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top