OR shorthand in Oracle SQL? - sql

I have a query that I need to check around 20 different columns for a 0 value.
Rather than doing:
WHERE
BOOK <> 0
OR ALLO <> 0
OR ...
Is there a quicker way of doing it?
Something like:
WHERE
(BOOK,ALLO,...) <> 0

Although it doesn't run anywhere else (MySQL, SQL-Server, Postgres) and it's probably not SQL-standard, it works in Oracle:
WHERE 0 <> ANY (BOOK, ALLO, ...)
Tested in SQL-Fiddle
There is also another way that is standard and works in MySQL and Postgres, but not in Oracle:
WHERE (0, 0, ...) <> (BOOK, ALLO, ...)
And another standard way (using a Table Values Constructor) that works in Postgres and SQL-Server 2012:
WHERE 0 <> ANY (VALUES (BOOK), (ALLO), ...)

You could use:
WHERE (book + allo + ...) > 0

If negative values are not possible, you can do this:
WHERE BOOK + ALLO + ... > 0
If negative values are possible, this is the most concise way I can think of to express it:
WHERE ABS(BOOK) + ABS(ALLO) + ... > 0
Also, this is database agnostic.
These solutions will only work if null values are not possible, If they are, it would get quite messy.

Related

how to use is null with case statements

Hi can any one say me how to write query in sql server management studio for statement =>if(isnull("GROSS_SF")=1 or "GROSS_SF"=0,0,"GROSS_SALES"/"GROSS_SF")
by using case statement :
CASE
WHEN ("GROSS_SF"=1 or "GROSS_SF"=0) isnull then 0
else "GROSS_SALES"/"GROSS_SF"
end/* i am getting error if i write it like this */
thanks in advance
If you want to avoid a divide by zero, while at the same time also handling NULL, then you may try the following logic:
CASE WHEN COALESCE(GROSS_SF, 0) = 0
THEN 0
ELSE GROSS_SALES / GROSS_SF END AS output
To be clear, the above logic would return a zero value should either the GROSS_SF be zero or NULL.
I think what are you looking for is IFNULL, look here https://www.w3schools.com/sql/sql_isnull.asp
You are close. You can do:
(CASE WHEN "GROSS_SF" IS NULL OR "GROSS_SF" = 0 THEN 0
ELSE "GROSS_SALES" / "GROSS_SF"
END)
In SQL, divide by zero errors are often avoided using NULLIF():
"GROSS_SALES" / NULLIF("GROSS_SF", 0)
However, that returns NULL rather than 0. If you really want zero:
COALESCE("GROSS_SALES" / NULLIF("GROSS_SF", 0), 0)
This is a bit shorter to write and almost equivalent to your version (this returns 0 if "GROSS_SALES" is NULL, which your version does not).
You can use NULLIF() to prevent arithmetic divide by zero errors :
select . . . ,
GROSS_SALES / nullif(GROSS_SF, 0)
from table t;
However, this would return null instead of error, if you want to display 0 instead then you need to use isnull() (MS SQL Specific) or coalesce() (SQL Standard).
So, you can express it :
select . . . ,
isnull(GROSS_SALES / nullif(GROSS_SF, 0), 0)
from table t;

SQL Server: interpreting 'y' as BIT value

In C, when you compared true/false value to 1/0, it worked very well.
I would want the similar possibility with SQL Server - when I have a bit column, I would like to compare myBitField = 'y' / myBitField = 'n'
Is there anything I can do about that? Maybe change some SQL interpreter settings or something?
Example of what I would like to do:
select * from
(
select CAST(1 AS BIT) as result
) as main
where main.result = 'y'
Currently, it throws an error, and I would like it to return 1/true/'y', whatever, but I would like it to be able to make that comparison.
I suppose you want to do it for some yes/no thing. But this is generally a wrong concept, your application which is accessing the SQL Server should interpret y as a 1 and n as a 0 and afterwards set the correct parameters for the query. You should not (actually I'm temped to write "must not") do this in SQL Server, that's what you have a business logic for.
As others have said, BIT and CHAR / VARCHAR are entirely different datatypes. But if you want to cast them during the select, you can use CASE expression like so:
-- Reading string as BIT
SELECT CAST(CASE RESULT WHEN 'Y' THEN 1 WHEN 'N' THEN 0 ELSE NULL END AS BIT) RESULT
-- Reading BIT as string
SELECT CAST(CASE RESULT WHEN 1 THEN 'Y' WHEN 0 THEN 'N' ELSE NULL END AS CHAR(1)) RESULT
And that's about as far as your options go here, far as I can understand. :)

Can somebody verify this for me in sql or teradata

I have a line of code in Oracle and I had to convert it into Teradata.
The Oracle query is
/* add to avoid invalid number due to junk in column */
AND regexp_instr(table.column, ''[^[:digit:]]'', 1, 1) = 0
The code I have written in Teradata
AND (CASE WHEN (POSITION('' '' IN TRIM(table.column)) > 0) OR (UPPER(TRIM(table.column))
(CASESPECIFIC) <> LOWER(TRIM(table.column)) (CASESPECIFIC))
THEN 1 ELSE 0 end ) = 0
The column is defined as a VARCHAR(20) but I only want to select rows where the data is all numeric. I cannot verify the Teradata query as it is a very long-running query and I don't have access to create tables or rather I can not verify the out put on the database I have. I some how tried and it looks like it works but I once wanted to verify the syntax and my understanding of REGEXP_INSTR.
If I am reading correctly and based on my testing this will break your logic (both return 1):
SELECT (CASE WHEN (POSITION('' '' IN TRIM('1234')) > 0) OR (UPPER(TRIM('1234'))
(CASESPECIFIC) <> LOWER(TRIM('1234')) (CASESPECIFIC))
THEN 1 ELSE 0 END )
SELECT (CASE WHEN (POSITION('' '' IN TRIM('abcd ef1')) > 0) OR (UPPER(TRIM('abcd ef1'))
(CASESPECIFIC) <> LOWER(TRIM('abcd ef1')) (CASESPECIFIC))
THEN 1 ELSE 0 END )
The Teradata Developer's Exchange contains a library of Oracle functions that have been converted to Teradata UDF's that may help you address this problem. With a little effort you could write your own UDF around the isdigit() C function. (isdigit)
It may not be helpful now, but one of the recently announced Teradata 14.0 features is support of regular expressions.
EDIT: Added TD 14 example with REGEXP_INSTR that should solve the problem
SELECT table.column
FROM table
WHERE REGEXP_INSTR(table.column, '[^[digit]])') = 0;

sql operator equivalent to !=

I want to select all the fields where a certain column is not a value I specify.
I tried this but it didn't work.
SELECT *
FROM table
WHERE columnname != value
My mistake.
It was another error so I thought it was wrong with != operator cause it was the first time I use it. Sorry guys!
SELECT *
FROM table
WHERE columnname <> value
For MySQL:
!= or <> are correct.
http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html
You should consider NULL columns also. You can do WHERE columnname IS NOT NULL also.
In SQL, I believe inequality is
<>
though many implementations also allow
!=
Either <> or !=
From: MySQL Manual (version 5.0)
<>, !=
Not equal:
mysql> SELECT '.01' <> '0.01';
-> 1 mysql> SELECT .01 <> '0.01';
-> 0 mysql> SELECT 'zapp' <> 'zappp';
-> 1
You need to post the query you are using, because != works fine for me in MySQL 4.1
As others mentioned, <> is equivalent. The != is ANSI standard (99 I believe).
WHERE NOT columnname = value
NOT IN is one flavor and here's a tsql negate example

How to flip bit fields in T-SQL?

I'm trying to flip a bit field in SQL Server using an update query, that is, I want to make all the 0's into 1's and vice versa. What's the most elegant solution?
There doesn't seem to be a bitwise NOT operator in T-SQL (unless I'm missing something obvious) and I haven't been able to find any other way of performing the update.
You don't need a bitwise-not for this -- just XOR it with 1 / true.
To check it:
select idColumn, bitFieldY, bitFieldY ^ 1 as Toggled
from tableX
To update:
update tableX
set bitFieldY = bitFieldY ^ 1
where ...
MSDN T-SQL Exclusive-OR (^)
Why not a simple bitfield = 1 - bitfield?
Another way is
DECLARE #thebit bit = 1, #theflipbit bit
SET #theflipbit = ~ #thebit
SELECT #theflipbit
where "~" means "NOT" operator. It's clean and you get a good code to read.
"negate the bit" is even cleaner and it does exactly what the "NOT" operator was designed for.
I was pretty sure that most SQL flavors had a bitwise NOT, so I checked and there does appear to be one in TSQL.
From the documentation, it's the character ~.
UPDATE tblTest SET MyBitField = CASE WHEN MyBitField = 1 THEN 0 ELSE 1 END
It's bland but everyone will understand what it's doing.
EDIT:
You might also need to account for nulls as suggested in the comments. Depends on your req's of course.
UPDATE tblTest SET
MyBitField = CASE
WHEN MyBitField = 1 THEN 0
WHEN MyBitField = 0 THEN 1
ELSE NULL -- or 1 or 0 depending on requirements
END
A simple bitwise NOT operator (~) worked for me in SQL Server 2014 - 12.0.2269.0
In the update clause inside your T-SQL -
Update TableName
SET [bitColumnName] = ~[bitColumnName],
....
WHERE ....
Hope this helps
Ref - https://learn.microsoft.com/en-us/sql/t-sql/language-elements/bitwise-not-transact-sql
Did you try this?
UPDATE mytable SET somecolumn =
CASE WHEN somecolumn = 0 THEN 1
WHEN somecolumn IS NULL THEN NULL
WHEN somecolumn = 1 THEN 0
END
query (vb)
x = "select x from table"
update (vb)
"update table set x=" Not(x*(1))