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
Related
So I have EXISTS in huge query which looks like this:
EXISTS(
SELECT
*
FROM
ExistTable
WHERE
ExTableFieldA = #SomeGuid AND
ExTableFieldB = MainTableFieldB AND
ExTableFieldA <> (
CASE
WHEN MainTableFieldZ = 10 THEN MainTableFieldYYY
ELSE NULL
END
)
)
The problem comes from ELSE part of CASE statement, this ExTableFieldA <> NULL will be always false. I could easily write another parameter #EmptyGuid and make it equal to '00000000-0000-0000-0000-000000000000' and everything will work but is this the best approach ?
Pretty much I want to execute another check into the exist for the small size of the records which return the "main" query.
How about removing the case and just using boolean logic?
WHERE ExTableFieldA = #SomeGuid AND
ExTableFieldB = MainTableFieldB AND
(MainTableFieldZ <> 10 OR ExTableFieldA <> MainTableFieldYYY)
I would also recommend that you qualify the column names by including the table alias.
Note: This does assume that MainTableFieldZ is not NULL. If that is a possibility than that logic can easily be incorporated.
ELSE NULL is implied even if you don't list it, but you could use ISNULL here.
ISNULL(ExTableFieldA,'') <> (
CASE
WHEN MainTableFieldZ = 10 THEN MainTableFieldYYY
ELSE ''
END
)
You may need to use some other value like 9999 instead of ''
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.
I would like to replace the total value to 0 when it is null
Here is the query:
SELECT DISTINCT(location), (
SELECT Count(a.location) as total
FROM table_fo a
LEFT JOIN table_info b ON a.TRADEID = b.TRADEID AND a.asofdate = b.asofdate
WHERE (b.TERMSTATUS <> 'TRAN' OR b.TERMSTATUS is NULL) AND b.asofdate = '20110105' AND a.location = pfo.location
GROUP BY a.LOCATION
) AS total
FROM table_fo pfo
WHERE asofdate = '20110105';
You can use the ISNULL function.
Here is how you will use the function (for SQL Server).
ISNULL(columnName, 0)
I would like to replace the total
value to 0 when it is null
This is an impossible situation because:
The COUNT function always returns an integer. The result cannot be NULL!
As for coalescing an expression to a certain default in case it is NULL there are functions that do this in all major databases (ex.: COALESCE, NVL, ISNULL, IFNULL). The typical use is
FUNCTION_NAME(ExpressionThatMayBeNULL, DefaultWhenNull)
For specifics you should consult you database manufacturers documentation (you can find it online).
COALESCE will do this in PL/SQL (Oracle) and T-SQL (SQL Server)
Syntax is COALESCE(field1, field2[, fieldN]) - it will select the first column from the left to have a non-null value.
Modifying the query you had:
SELECT DISTINCT(location), COALESCE((
SELECT Count(a.location) as total
FROM table_fo a
LEFT JOIN table_info b ON a.TRADEID = b.TRADEID AND a.asofdate = b.asofdate
WHERE (b.TERMSTATUS <> 'TRAN' OR b.TERMSTATUS is NULL) AND b.asofdate = '20110105' AND a.location = pfo.location
GROUP BY a.LOCATION
),0) AS total
FROM table_fo pfo
WHERE asofdate = '20110105';
As dparker said, ISNULL(...) will work for some types of sql, though the name of the function can vary among database providers.
The function in IBM DB2 is called COALESCE(...), and in Oracle SQL it is NVL(...) for example.
This may be usefull
http://www.w3schools.com/sql/sql_isnull.asp
If you are using SQL server, the following are the 3 ways that can be used to replace a null with any user defined substitute value.
1. ISNULL
2. COALESCE
3. CASE
This question is also asked in one of the interviews I attended. Here is the link for an article with examples. By the way, there's also a video on the same in this article.
Different ways to replace NULLS in SQL Server
What DB system are you using?
SQL-Server, MySQL: IFNULL(value, 0)
Oracle: NVL(value, 0)
PostgreSQL: COALESCE(value, 0)
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))
I've got some blank values in my table, and I can't seem to catch them in an IF statement.
I've tried
IF #value = '' and if #value = NULL and neither one catches the blank values. Is there any way to test whether or not a varchar is entirely whitespace?
AHA! Turns out I was testing for null wrong. Thanks.
ltrim(rtrim(isNull(#value,''))) = ''
To compare with NULL, use the IS NULL keyword.
--Generic example:
SELECT *
FROM MY_TABLE
WHERE SOME_FIELD IS NULL;
--Instead of
SELECT *
FROM MY_TABLE
WHERE SOME_FIELD = NULL;
if length(#value) = 0 or #value is null
(LTRIM(RTRIM(#Value))=''
should do the trick.
where length(rtrim(ltrim(yourcolumnname))) = 0 OR yourcolumnname is null
I just did some testing, and found out something interesting.
I used to write my queries like so:
SELECT *
FROM TableA
WHERE Val IS NOT NULL
AND LEN(RTRIM(LTRIM(Val))) > 0
But, you don't actually need to check for null, all you need to do is check the length after trimming the value.
SELECT *
FROM TableA
WHERE LEN(RTRIM(LTRIM(Val))) > 0
This select weeds out nulls as well as any columns with just white space.
As it turns out, you don't need to trim the value because SQL Server ignores trailing whitespace, so all you actually need it this:
SELECT *
FROM TableA
WHERE LEN(Val) > 0
Rather then performing excessive string manipulation with LTRIM AND RTRIM, just search the expression for the first "non-space".
SELECT
*
FROM
[Table]
WHERE
COALESCE(PATINDEX('%[^ ]%', [Value]), 0) > 0
You may have fields with multiple spaces (' ') so you'll get better results if you trim that:
where ltrim(yourcolumnname) = ''