How to exclude zero from list of values.

Rod Martin 136 Reputation points
2021-09-14T11:13:28.187+00:00

This statement works.. however, I want it to provide the min of the values, but NOT if it is zero. How would I exclude zero from this list?

CASE
    WHEN (Patindex('%-%', od._snvalve) > 0
          OR Patindex('%,%', od._snvalve) > 0) THEN Substring(od._snvalve, Len(od._snvalve)-
                                                                (SELECT min(xxx)
                                                                 FROM (
                                                                       VALUES (Patindex('%-%', Reverse(od._snvalve))), (Patindex('%,%', Reverse(od._snvalve))), (Patindex('% %', Reverse(od._snvalve)))) AS value(xxx)), Len(od._snvalve))
ELSE ''
END AS Last_SN

Rod

SQL Server Other
{count} votes

Accepted answer
  1. Rod Martin 136 Reputation points
    2021-09-14T14:00:41.85+00:00

    I need to exclude zeros from inside the values() statement. I need the minimum that is not zero.


3 additional answers

Sort by: Most helpful
  1. Rod Martin 136 Reputation points
    2021-09-14T11:46:03.92+00:00

    Well, i'm sure its a good idea. But, I dont have any idea what DDL and DML is.

    in short, in a list of Values(), how do I exclude one that is zero?


  2. Viorel 122.5K Reputation points
    2021-09-14T13:02:19.127+00:00

    To exclude zeroes from min(xxx), I think that there are two methods:

    select min(xxx) from … where xxx <> 0

    or

    select min(nullif(xxx, 0)) from …

    After you provide the details, I hope that these approaches will work.

    0 comments No comments

  3. Rod Martin 136 Reputation points
    2021-09-14T21:19:19.33+00:00

    @Tom Phillips , @Viorel ,

    Great help here. THe WHERE method did not work. I could not get the full sql statement to resolve. However, the min(nullif(xxx,0)) produced EXACTLY what was needed! Well done. Thank you!

    Rod


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.