I have column1, column2, column3, column4, column5 and so on.
If I know that one column is not null, I can use this query:
SELECT *
FROM table
WHERE columnA IS NOT NULL;
But now I want to select rows where all the columns are not null.
How to write is query easily?
Use the query look like this
SELECT *
FROM table_name
WHERE NOT column1 IS NULL AND NOT column2 IS NULL...
Related
I have two tables, one is populated with data
CREATE TABLE `tableX` (`column1` REAL);
INSERT INTO tableX VALUES (5);
INSERT INTO tableX VALUES (-3);
CREATE TABLE `tableY`
(`rowid` INTEGER PRIMARY KEY, `column2` REAL);
The INSERT CASE statement is
INSERT INTO tableY SELECT column1,
CASE WHEN column1 >=0 THEN 1 ELSE 2 END as column2 FROM tableX;
Error is "datatype mismatch" in my DB Browser but it seems to work in sqlfiddle...not error at least
Any help is greatly appreciated! Thanks :D!
Not the most elegant solution, but it is a work around.
CREATE TABLE `tableX` (`column1` REAL);
INSERT INTO tableX VALUES (5);
INSERT INTO tableX VALUES (-3);
CREATE TABLE `tableZ` (`column1` REAL, `column3` REAL);
INSERT INTO tableZ SELECT column1,
CASE WHEN column1 >=0 THEN 1 ELSE 2 END as column3 FROM tableX;
CREATE TABLE 'tableY' (`rowid` INTEGER PRIMARY KEY, `column2` REAL)
INSERT INTO tableY (column2) SELECT column3 FROM tableZ;
DROP TABLE tableZ;
If an entry in column1 is larger than or equal to 0 then it should print 1 in column2. Else it should print a 0.
The table looks like this:
create table table1 (column1 REAL);
insert into table1 values (8);
insert into table1 values (-10);
insert into table1 values (53);
And this is the query:
SELECT column1
CASE
WHEN column1 >=0 THEN 1
ELSE 0
END as column2
FROM table1;
So simple, but not working. Any help is greatly appreciated!!!
Result columns must be separated with a comma:
SELECT column1,
CASE ... END AS column2
FROM table1;
You are missing a comma after the column1 column name.
SELECT column1,
CASE
WHEN column1 >=0 THEN 1
ELSE 0
END as column2
FROM table1;
I have an update query that when I run it instead of matching each unique row it is replicating the same row through the entire column.
Any help would be massively appreciated.
I have got
UPDATE Table1
SET Column1 = (SELECT Column1 FROM [Table2] WHERE Column2 = [Table2].Column2)
[Table2].Column2 referes to the column in Table2.
Column2 refers to the column in some table that has such a column. The innermost such table is Table2.
So this ends up being the same as Table2.Column2 = Table2.Column2.
To refer to the column in Table1, specify that table:
UPDATE Table1
SET Column1 = (SELECT Column1
FROM Table2
WHERE Table2.Column2 = Table1.Column2);
Do you know how to create an Oracle query that will list my table name, followed by all the column names in that table? I have 5 tables in total.
Something like:
Table
Column1
Column2
Column3
Table2
Column1
Table3
Column1
Column2
I'm hard-pressed to imagine why you would want a single-column result that doesn't do anything to differentiate between what is a column name and what is a table name. You can do something like
select pseudo_column_name
from (
select table_name, table_name pseudo_column_name, 0 column_id
from user_tables
union all
select table_name, column_name, column_id
from user_tab_columns
)
order by table_name, column_id
I want to select the value from another column if there are no records in the first column (is null).
I want something like this:
SELECT (if Column1 IS NULL THEN Column2), Column3, Column4 FROM MyTable
How can I do that in SQLite?
Use the COALESCE function, which returns the first non-null argument:
SELECT COALESCE(Column1, Column2), Column3, Column4
FROM MyTable
In this case, you will get Column1 but if it is NULL, Column2 will be returned instead.
From Safari Books
Name
coalesce() — Return first non-NULL argument
Common Usage
coalesce( param1, param2, ... )
Description
The coalesce() function takes two or more parameters and returns the first non-NULL parameter. If all of the parameter values are NULL, a NULL is returned.
See Also
ifnull(), nullif()
You can use IFNULL function , try this
SELECT IFNULL(Column1, Column2), Column3, Column4 FROM MyTable
Use COALESCE(column1,column2) .
SELECT COALESCE(Column1, Column2), Column3, Column4 FROM MyTable