KB-49201: Using Log10 function and Case/Else commands in an SQL query.

Question:

How can I have a Query’s Log10 function be executed only in rows different from 0 (zero), leaving rows equal to zero with value 0 (zero)?

Solution:

To apply Log10 function only to rows that are different from 0 (zero), you must use Case/Else commands inside an SQL query in order to test their values before you run the function. If the value is zero, the same value will be attributed to the field. If the value is different from zero, Log10 function will be applied.

Example of SQL query performing this:

SELECT CASE WHEN Field = 0 THEN 0 ELSE LOG10(Field) END AS Logarithm
FROM table

NOTE: This code is only valid for SQLServer databases.

Print Friendly, PDF & Email

Este artigo foi útil? Was this helpful?

Classificação média - Average rating 0 / 5. Count: 0

Deixe seu Comentário

Seu endereço de e-mail não será publicado. Campos marcados com asterisco são obrigatórios *