379716 visitors has seen the Tim-Carter.com site. Thanks!
321 visitors has seen the Tim-Carter.com site today.
 
 
Search :  
 
>>> Programming >> MSSQL > How to do an IF inside an Select in SQL? (This page has been seen 648 times)

How to do an IF inside an Select in SQL?

SQL Has something called CASE that you use instead of IF. So if you have an query, where you want SQL to do something with a field if the value is for example NULL then you could do something like this

SELECT
      CASE WHEN SUM(FIELD1) = 0 THEN 0 ELSE SUM(FIELD1)/SUM(FIELD2) END
FROM
      MyTable


This would be a good way of handling a 0 value, because you cannot DIVIDE 0 with 0. This will give you a DIVIDED BY ZERO ERROR



Like (1)
 
Dislike (1)


Keywords for this article:
DIVIDED BY ZERO || CASE || WHEN || SUM

Post comment

Name:


Comment:

Code Language:

Code:

Here you can paste a code example. It will then be processed by SyntaxHighlighter and formatted for easier readability.
Please remember to select the correct Code Language in the select above so the SyntaxHighlighter can highlight the code properly.




Code:

Please enter the code you see above

What is 3 + 7 =

Submit Comment


No one has commented on this page yet. You could be the first.


Advertisement by Google


Sponsors :