Display value from column B if column A is NULL - sql

I am wondering how i can display data from column B if Column A is null. The reason is if we get a product from one of our manufactures it is put in a different column. However, when i go to build the report the two columns essentially the same thing and it is throwing the graph way off. Any help would be appreciated.
Something like this maybe?
Case When column A isnull then column B?

Use ISNULL() or COALESCE(), or CASE
SELECT ISNULL(ColumnA, ColumnB) AS [YourColumn]
FROM FOO
OR
SELECT COALESCE(ColumnA, ColumnB) AS [YourColumn]
FROM FOO
OR
SELECT CASE WHEN ColumnA IS NULL THEN
ColumnB
ELSE
ColumnA
END AS [YourColumn]
FROM FOO

Related

SQL select statement to change two other column values based on a column that contains null

I would like to use a SQL select statement that has the condition 'where column A is NULL change column B values to be equal to column C values'. How would I be able to incorporate this logic into a SELECT statement (Not an UPDATE statement as I cant change the tables on the server but want to query them from the server).
SELECT final.*
FROM final
The actual table is in the image below, here I want to change column Old to match column DirectUse if the Change column is null.
Try Case statement:
SELECT
Name, NameSimple, DirectUse, Year, Month,
CASE WHEN Change IS NULL THEN DirectUse ELSE Old END AS Old,
CurrentCons, Change
FROM final;
CASE: https://www.w3schools.com/sql/sql_case.asp
Can also be incorporated by UNION ALL:
SELECT Old
FROM final where Change is not null
UNION ALL
SELECT DirectUse
FROM final where Change is null
Use a CASE expression:
SELECT Name, NameSimple, DirectUse, Year, Month,
CASE WHEN Change IS NULL THEN DirectUse ELSE Old END AS Old,
CurrentCons, Change
FROM final;
I think you basically you want:
SELECT
ColumnA
, CASE WHEN ColumnA IS NULL THEN ColumnC ELSE ColumnB END AS ColumnB
, ColumnC
, <any other columns>
FROM Final

Not In operator eliminates NULL values rows in a table

I would like to retrieve all rows with values except 1,2,3,4,5 in my COLUMNA in TABLEA .
SELECT * FROM TABLEA WHERE COLUMNA NOT IN (1,2,3,4,5)
But this eliminates the rows with NULL values in COLUMNA too.
I don't want to eliminate NULL values rows and would like to include those rows in the resultset.
Alternatively, I can try below query for the same
SELECT * FROM TABLEA WHERE COLUMNA NOT IN (1,2,3,4,5) OR COLUMNA IS NULL.
But I would like to know, why is it necessary to add this OR condition?
Why is the additional necessary?
NULL comparisons almost always results in NULL, rather than true or false. A WHERE clause filters out all non-true values, so they get filtered out.
This is how SQL defines NULL. Why is NULL defined like this?
NULL does not mean "missing" in SQL. It means "unknown". So, the result of <unknown> not in (1, 2, 3, 4, 5) is "unknown", because the value could be 1 or it might be 0. Hence it gets filtered.
I will also note that the SQL standard includes NULL-safe comparisons, IS NOT DISTINCT FROM and IS DISTINCT FROM corresponding to = and <> respectively. These treat NULL as just "any other value", so two NULL values are considered equal. However, there is no construct for NULL-safe IN and NOT IN, as far as I know.
Try the following:
SELECT * FROM TABLEA WHERE ISNULL(COLUMNA,0) NOT IN (1,2,3,4,5)

Compare varchar values postgresql

I'm new in the db world and I'm trying to do my first queries.
I have two tables and I need compare the value of the columnA, in the first table, with the value of the columnB in the second table.
I would like to know what values in the columnA are in the columnB. Both columns are varchar type.
I've tried this two queries:
select *
from tableA
join tableB
on (tableA.columnA = tableB.columnB);
select *
from tableA
where columnA in (select columnB
from tableB);
But both get me back empty table.
I checked the values manually, and there are many equal values.
Maybe the = isn't the right operator with the string values?
This is a simple example of what I would do, with the expected result at the end.
TableA
columnA descriptionA
EF8236PA xyx
EF7843DV dgfd
EF6535MD dshr
EF3274LK hghg
EF6940BN fdtsg
EF3405TJ dsbfbs
TableB
columnB
EF3405TJ
EF6940BN
EF6535MD
Result:
EF3405TJ
EF6940BN
EF6535MD
Both of your queries are looking fine. you can add few more conditions in the where clause that will make it work.
Example:
select *
from tableA
join tableB
on (upper(trim(tableA.columnA)) = upper(trim(tableB.columnB)));
Trim will cut down extra spaces and upper will make the search case insensitive.
Hope this will help.
I would like to know what values in the columnA are in the columnB.
Strictly speaking, this query is a correct (and fast) query to implement what you ask:
SELECT DISTINCT columnA
FROM tableA a
WHERE EXISTS (SELECT 1 FROM tableB WHERE columnB = a.columnA);
The manual about EXISTS.
But neither of your queries should return empty sets. There may be whitespace or other invisible characters fooling you. Test with:
SELECT * FROM tableA WHERE columnA = 'EF6940BN';
SELECT * FROM tableB WHERE columnB = 'EF6940BN';
I tried all your suggestions, but no one works.
So I tried run the code on another postgre and the same code that I wrote above works.
I don't understand why, the postgre's versions are the same

Adding together two column sums in SQL Server2005

Ive come across a problem when trying to add together two column sums.
Ive created a view with all the correct data in but when i try to execute a query like:
Select ID, Sum(ColumnA),
Sum(ColumnB)
Sum(ColumnA) + Sum(ColumnB) AS ColumnC
From View1
Group by ID
The ColumnC figure is only correct when there is data in both columns, if there is only data in ColumnB then it displays that but if there is only data in ColumnA then it doesnt.
Sometime when there isnt any data in ColumnA or B it will be NULL, so maybe this is the problem.
Hope there is a way around this.
Cheers
Try using ISNULL to substitute zeros for NULLs:
Select ID, Sum(ColumnA),
Sum(ColumnB)
ISNULL(Sum(ColumnA),0) + ISNULL(Sum(ColumnB),0) AS ColumnC
From View1
Group by ID
Adding something to a null value gives a null result, the null is not converted to zero. You have to do that conversion explicitly:
Select ID, Sum(ColumnA),
Sum(ColumnB)
isnull(Sum(ColumnA), 0) + isnull(Sum(ColumnB), 0) AS ColumnC
From View1
Group by ID
You can use COALESCE to replace NULL inputs to the calculation with zero as below.
COALESCE(Sum(ColumnA),0) + COALESCE(Sum(ColumnB),0) + AS ColumnC
Or ISNULL as in the other 2 answers. Doesn't matter which if portability is not a concern.

how to select a row where one of several columns equals a certain value?

Say I have a table that includes column A, column B and column C. How do I write I query that selects all rows where either column A OR column B OR column C equals a certain value? Thanks.
Update: I think forgot to mention my confusion. Say there is another column (column 1) and I need to select based on the following logic:
...where Column1 = '..' AND (ColumnA='..' OR ColumnB='..' OR ColumnC='..')
Is it valid to group statements as I did above with parenthesis to get the desired logic?
Unless I'm missing something here...
SELECT * FROM MYTABLE WHERE COLUMNA=MyValue OR COLUMNB=MyValue OR COLUMNC=MyValue
I prefer this way as its neater
select *
from mytable
where
myvalue in (ColumnA, ColumnB, ColumnC)
SELECT *
FROM myTable
WHERE (Column1 = MyOtherValue) AND
((ColumnA = MyValue) OR (ColumnB = MyValue) OR (ColumnC = MyValue))
Yes, it's valid to use parentheses. However, if you're searching multiple columns for the same value, you may want to consider normalizing the database.