DATEDIFF - Replace NULL with NOW()

<p>I got following SQL Query</p><pre><br /><code>SELECT<br /> e.id,<br /> c.name,<br /> e.location,<br /> e.designation,<br /> e.time_period_from,<br /> e.time_period_to,<br /> DATEDIFF(e.time_period_to, time_period_from) AS tenure_in_days<br />FROM<br /> employment e<br />LEFT JOIN<br /> company c ON (c.id = e.company_id)<br />LIMIT<br /> 0, 10<br /></code><br /></pre><p>This is working perfectly, i have a scenario where <code>time_period_to</code> can have <code>NULL</code> values, in this case, i want to replace it with current date.</p><p>Here is what i tried.</p><pre><br /><code>SELECT<br /> e.id,<br /> c.name,<br /> e.location,<br /> e.designation,<br /> e.time_period_from,<br /> e.time_period_to,<br /> DATEDIFF(IF(ISNULL(e.time_period_to), NOW(), e.time_period_from)) AS tenure_in_days<br />FROM<br /> employment e<br />LEFT JOIN<br /> company c ON (c.id = e.company_id)<br />LIMIT<br /> 0, 10<br /></code><br /></pre><p>This gives me the following error</p><pre><br /><code>ERROR 1582 (42000): Incorrect parameter count in the call to native function 'DATEDIFF'<br /></code><br /></pre><p>Where am i going wrong?</p><br /><h2>Click here view more answers <a href="%20http://stackoverflow.com/questions/34525443/datediff-replace-null-with-now">http://ift.tt/1Jfjf29</a></h2>

Nhận xét

Bài đăng phổ biến từ blog này

Align div to bottom of the page in bootstrap

Align elements inside an tag vertically

Javascript is unable to stop page loading