Hi, How to update negative values as Zero in Snowflake? - sql

CASE
WHEN round(months_between(CURRENT_DATE, nvl(bpi.StartDate, cli.StartDate))) > round(months_between(nvl(bpi.EndDate, cli.EndDate), nvl(bpi.StartDate, cli.StartDate)))
THEN round(months_between(nvl(bpi.EndDate, cli.EndDate), nvl(bpi.StartDate, cli.StartDate)))
ELSE round(months_between(CURRENT_DATE, nvl(bpi.StartDate, cli.StartDate)), 1)
END AS Months_Used

Using update:
UPDATE tab
SET col = 0
WHERE col < 0;
Alternatively without actual change in base table using CASE expression:
SELECT CASE WHEN col < 0 THEN 0 ELSE col END AS col
FROM tab;

Related

How to sum values on each row with condition?

So, basically, I want to update my table by summing values on each row, but I don't want to sum negative values.
Here is some simple code that I've tried:
UPDATE recipe SET total_time = preparation_time + cooking_time;
But sometimes, preparation_time or cooking_time is equal to -1 and when it happens I won't to parse them to be equal to 0. How can i do that?
You can use case expressions:
SET total_time = ((case when preparation_time > 0 then preparation_time else 0 end) +
(case when cooking_time > 0 then cooking_time else 0 end)
)

way to have query with case statement

I have table called Numbers in that column I have values from 0 - 10 but I like to keep value of 1-10 only change record of 0 too null
Case numbers
when 0
then ''
but I found this has changed all values and not values that have 0 is there way I can say else leave value as is?
Do you want this?
update t
set number = null
where number = 0;
Or as a select:
select t.*,
(case when number <> 0 then number end)
from t;
SELECT CASE WHEN [column] = 0 THEN NULL ELSE [column] END AS [SomeName]
FROM Numbers

How to filter Even and Odd numbers and move/copy them to an empty column?

i have a simple problem. i have a column with numbers. i need to filter them to even and odd numbers, but if a number is even than i need to copy or move it to "even-number-column" in the same table. if the number is odd then move it to "odd-number-column" in the same table.
this is my code:
select distinct all_numbers, even-number-column, odd-number-column
case when all_numbers%2=0
then UPDATE my_table SET even-number-column = all_numbers
else UPDATE my_table SET odd-number-column = all_numbers
end
from my_table ;
If you want a SELECT:
select
distinct all_numbers,
CASE
WHEN when all_numbers%2 = 0 THEN all_numbers
ELSE 0 -- or NULL
END as even-number-column,
CASE
WHEN when all_numbers%2 = 1 THEN all_numbers
ELSE 0 -- or NULL
END as odd-number-column
from my_table ;`
If you want an update
UPDATE my_table
SET
even-number-column = CASE
WHEN when all_numbers%2 = 0 THEN all_numbers
ELSE even-number-column -- or change for 0 or NULL
END,
odd-number-column = CASE
WHEN when all_numbers%2 = 1 THEN all_numbers
ELSE odd-number-column -- or change for 0 or NULL
END
Use an array to make it simple
update t
set
odd = (array[null, all_numbers])[all_numbers % 2 + 1],
even = (array[all_numbers, null])[all_numbers % 2 + 1]

query sql returning custom value

I have a table with 3 columns (containing Integer values that assume only values from 0 to 10). I want extract, with a single query, a table with 1 column. This column must assume a value based on the following logic:
If one of these three columns has value 0 ----> the value of column of table generated by query must be 0 too.
If none of the last three columns has value 0 ----> the value of column must assume the value 1.
You are looking for CASE construct or IF function:
SELECT CASE WHEN (t.field1 = 0 OR t.field2 = 0 OR t.field3 = 0) THEN 0
ELSE 1 END AS value
FROM t;
In this specific case you might also use the fact that any member being zero will zero the product:
SELECT CASE WHEN (t.field1*t.field2*t.field3 = 0) THEN 0 ELSE 1 END AS value
FROM t;
Or
SELECT IF((t.field1*t.field2*t.field3)=0, 0, 1) AS value FROM t;
This is a simple case statement. Assuming there are no NULL values, try this:
select (case when col1 = 0 or col2 = 0 or col3 = 0 then 0 else 1 end)
Try this
SELECT
CASE
WHEN column1 = 0 THEN 0
WHEN column2 = 0 THEN 0
WHEN column3 = 0 THEN 0
ELSE 1
END
FROM urtable

SQL Query - Can I compare using LEN in SELECT clause?

I basically want to do this:
SELECT HasComments = CASE (LEN(Comments) > 1) WHEN 1 THEN 1 ELSE 0 END FROM TableName
In other words, return a boolean telling me whether the length of Comments is greater than 1. This gives me a syntax error.
How can I accomplish this?
SELECT HasComments = CASE WHEN LEN(Comments) > 1 THEN 1 ELSE 0 END
FROM TableName
A better way would be to make Comments NULLable and check for that. Indexes could then be leveraged instead of the table-scan LEN() will cause.
you're missing the when and end
SELECT HasComments = CASE WHEN (LEN(Comments) > 1) WHEN 1 THEN 1 ELSE 0 END
FROM TableName
Since you have no WHERE clause, you're most likely returning a column of data:
SELECT CASE WHEN LEN(Comments) > 1 THEN 1 ELSE 0 END as 'HasComments'
FROM TableName
For newer SQL versions:
SELECT CASE WHEN LEN(Comments) > 1 THEN 1 ELSE 0 END FROM TableName