Strange query grouping in sqlite3 - sql

I have a table like this:
CREATE TABLE prices2
(
id_price integer primary key,
date text,
value real
);
And let's say I have this values:
insert into prices2( '01/01/2017', 1 );
insert into prices2( '02/01/2017', 2 );
insert into prices2( '02/01/2017', 3 );
insert into prices2( '04/01/2017', 7 );
insert into prices2( '04/01/2017', 6 );
I wonder why this select is correct and returns some results, and what is returning:
select date,
count(*)
from prices2
where date
group by date like '%/01/2017';
Maybe "date like "%/01/2017" is a boolean expression that returns some kind of value???

date like '%/01/2017' indeed is a boolean expression; in SQLite, it returns either 0 or 1:
> select date, date like '%/01/2017' from prices2;
01/01/2017|1
02/01/2017|1
02/01/2017|1
04/01/2017|1
04/01/2017|1
So when you use this expression in the GROUP BY clause, you get one group for 1 values, and one group each for 0 and NULL values (but there are none in this example).
As for where date, the documentation says:
The SQL language features several contexts where an expression is evaluated and the result converted to a boolean (true or false) value. These contexts are:
the WHERE clause of a SELECT, UPDATE or DELETE statement,
[...]
To convert the results of an SQL expression to a boolean value, SQLite first casts the result to a NUMERIC value in the same way as a CAST expression. A numeric zero value (integer value 0 or real value 0.0) is considered to be false. A NULL value is still NULL. All other values are considered true.
The date values begin with a number that is not zero, so they are interpreted as true.

Related

Zero length ROW (ROW()) misbehaviour

I use PostgreSQL 10.3.
The following statements give the same result: true;
SELECT ROW() IS NULL;
SELECT ROW() IS NOT NULL;
and the next one
SELECT ROW() = ROW();
gives:
"ERROR: cannot compare rows of zero length"
So, if a row is of zero length, then is it unknown, not unknown or both?
And, how can I check if a row is of zero length or not?
Tia
https://www.postgresql.org/docs/current/static/functions-comparison.html
If the expression is row-valued, then IS NULL is true when the row
expression itself is null or when all the row's fields are null, while
IS NOT NULL is true when the row expression itself is non-null and all
the row's fields are non-null. Because of this behavior, IS NULL and
IS NOT NULL do not always return inverse results for row-valued
expressions
regarding the way to tell the row attributes count. I'm not sure how to do it neat, but you can think of many monkey ways, eg:
t=# SELECT count(1) from (select json_object_keys(row_to_json(ROW(1,2,3)))) a;
count
-------
3
(1 row)
t=# SELECT count(1) from (select json_object_keys(row_to_json(ROW()))) a;
count
-------
0
(1 row)
PostgreSQL does not allow comparison of rows of zero length.
To check the size of a row, try pg_column_size:
CREATE TEMPORARY TABLE t (a TEXT, b TEXT);
INSERT INTO t VALUES ('foo','bar'),('foo','bar2');
SELECT pg_column_size(a)+pg_column_size(b) FROM t;
To check if it is zero:
SELECT pg_column_size(ROW())-24=0

Select from table with 2 condition and 2 operations

I have a Table BoxTrans
the table Contain Rows (ID,Date,FromBox,ToBox,Value)
I want to make a View like (ID,Date,Box,ValueIn,ValueOut)
select when frombox Give Value to ValueOut
and when tobox Give Value to ValueIN
You can use a CASE statement to check the value of a different column when populating a column. The below query will return your output as long as either ToBox or FromBox is NULL, if they are both not null you may get unexpected results.
SELECT ID,
Date,
COALESCE(ToBox,FromBox) as Box,
CASE WHEN ToBox IS NOT NULL THEN value ELSE NULL as ValueIn,
CASE WHEN FromBox IS NOT NULL THEN value ELSE NULL as ValueOut
FROM BoxTrans

Is Null Greater Than Any Date Data Type?

I have this query in DB2
SELECT * FROM SOMESCHEMA.SOMETABLE WHERE SYSDATE > #A
If the SYSDATE is NULL, would it be greater than any value of #A, assuming that #A and SOMESCHEMA.SOMETABLE.SYSDATE is a Date data type?
Please help. Thanks in advance.
Another predicate that is useful for comparing values that can contain the NULL value is the DISTINCT predicate. Comparing two columns using a normal equal comparison (COL1 = COL2) will be true if both columns contain an equal non-null value. If both columns are null, the result will be false because null is never equal to any other value, not even another null value. Using the DISTINCT predicate, null values are considered equal. So COL1 is NOT DISTINCT from COL2 will be true if both columns contain an equal non-null value and also when both columns are the null value.
DB2 Null Handling
That means that all comparison operations will be false because you are comparing an unknown value to something. So no matter which comparison you use (only the IS NULL/IS NOT NULL operation do work!), it will be false.
If you want the query to work you should use something like
SELECT *
FROM SOMESCHEMA.SOMETABLE
WHERE COALESCE(SYSDATE, TIMESTAMP_FORMAT('0001-01-01 23:59:59', 'YYYY-MM-DD HH24:MI:SS')) > #A
Another possibility is to use IS NULL to test if a value is null:
SELECT * FROM SOMESCHEMA.SOMETABLE WHERE SYSDATE > #A OR SYSDATE IS NULL
will include the value in your set of returned values, instead of COALESCE function. It works only with simple cases though.
You can use this solution for comparing of two nullable dates (P.EndDate,C.EndDate):
[MinDate] =
CASE
WHEN
ISNULL(C.EndDate,P.EndDate) <= ISNULL(P.EndDate,C.EndDate)
THEN
ISNULL(C.EndDate,P.EndDate)
ELSE
ISNULL(P.EndDate,C.EndDate)
END

Null Value Statement

I have created a table called table1 and it has 4 columns named Name,ID,Description and Date.
I have created them like Name varchar(50) null, ID int null,Description varchar(50) null, Date datetime null
I have inserted a record into the table1 having ID and Description values. So Now my table1 looks like this:
Name ID Description Date
Null 1 First Null
One of them asked me to modify the table such a way that The columns Name and Date should have Null values instead of Text Null. I don't know what is the difference between those
I mean can anyone explain me the difference between these select statements:
SELECT * FROM TABLE1
WHERE NAME IS NULL
SELECT * FROM TABLE1
WHERE NAME = 'NULL'
SELECT * FROM TABLE1
WHERE NAME = ' '
Can anyone explain me?
In a CREATE TABLE, the NULL or NOT NULL here varchar(50) null is a constraint that determines whether NULLs are allowed. NOT NULL means no.
When you inserted data, which statement did you run?
INSERT TABLE1 VALUES (Null, 1, First, Null)
or
INSERT TABLE1 VALUES ('Null', 1, First, 'Null')
The first one uses the keyword NULL, inserts a NULL (not a null value: no such thing, arguably). No values is stored except in the NULL bitmap fields
The second one has a string "null" and the characters N, U, L, L + 2 bytes for length are stored
When you run SELECT * FROM TABLE1, client tools will show NULL.
To test whether you actually have NULLs or the string NULL, run this
SELECT ISNULL(name, 'fish'), ISNULL(date, GETDATE()) FROM TABLE1
For the SELECTs
--null symbols. No value stored
SELECT * FROM TABLE1 WHERE NAME IS NULL
--string null
SELECT * FROM TABLE1 WHERE NAME = 'NULL'
--empty string
SELECT * FROM TABLE1 WHERE NAME = ' '
Note: null symbol/value is not empty string. It has no value and won't compare. Even to itself.
As for your DBA, the code above with ISNULL will decide what is stored.
Edit: if you are storing null symbol/value, then your DBA should read up on "null bitmap"
The data does represent nulls. The text 'Null' is your query tool displaying the text.
One of them asked me to modify the table such a way that The columns Name and Date should have Null values instead of Text Null. I don't know what is the difference between those.
The NULL keyword indicates the absence of any value -- the value is unknown.
But that won't stop someone from storing the letters that spell out "NULL", data type providing (which INT and DATETIME will not). Because of this, operators like IS NULL would not work on text that spells out "NULL" and vice versa -- searching for strings using: ... LIKE '%NULL%' will not return records with NULL values.
The data type of the column does matter with regard to NULL in SQL Server. In a UNION statement, you need to cast NULL to be the appropriate data type -- the default for NULL is INT:
SELECT CAST('2011-01-01 00:00:00' AS DATETIME)
UNION
SELECT CAST(NULL AS DATETIME)
Based on the information provided about the columns and the output, the DBA appears to be asking you to change the text the client you are using to connect to SQL Server with displays when a NULL value is encountered in a resultset. Reminds me of my first job dishwashing, and was asked to get the lefthanded spatula...
The string "Null" is a string.
The value of NULL (or Null or null, SQL is case-insensitive when it comes to these things) is used to denote an unknown value. It's the empty set of values, if you will.
http://www.w3schools.com/sql/sql_null_values.asp
NULL, in software, is symbolic of no value. Assuming you're inserting the columns using a string with null as the value, use the null constant. e.g.
INSERT INTO table1 (Name,ID,Description,Date) VALUES (NULL,1,'First',NULL);
Note that NULL is a constant in SQL, not the word "NULL" in a string.
AFAIC, there is no different between NULLs. There are different column types. But as long as a column is a text data type, and it's NULL, it's a text NULL.
Sometimes there are questions about empty strings ("") instead of NULLs, but the description you're using doesn't seem to be referring to that.
SELECT * FROM TABLE1 WHERE NAME IS NULL
Returns all rows where the Name is NULL
SELECT * FROM TABLE1 WHERE NAME = 'NULL'
Returns all rows where the Name is equal to the string 'NULL', Null values are not returned
SELECT * FROM TABLE1 WHERE NAME = ' '
Returns all rows where the Name is equal to exactly one space ' ', Null values are not returned
If you run this statement it might help clear up when its null and when its not
select
*,
case
WHEN name is null THEN 'Its Null alright'
ELSE 'It has a value'
END
FROM TABLE1

How to alter the boolean value in SQL Server select query?

Basically I want to alter the boolean value selecting from the table:
e.g.:
SELECT otherColumns, not ysnPending FROM table
I need a column ysnPending = true if the value is false & false if the value is true.
Is there any function available to alter the Boolean value or I should use IIf or CASE...?
use CASE, or if the bit field is non-nullable you could just subtract from 1.
SELECT
otherColumns,
(1 - ysnPending) -- NOT ysnPending
FROM table
(Using CASE might lead to more understandable code.)
If ysnPending is nullable, what behaviour do you assign to NOT?
Example using a case statement :
create table table1 (id int not null, ysnPending bit null)
insert table1 values (1, 1)
insert table1 values (2, null)
insert table1 values (3, 0)
select id, cast((case when ysnPending = 1 then 0 else 1 end) as bit) as Not_ysnPending from table1
Assumes you want 1 returned when ysnPending is NULL.
The cast to bit type is to make sure that the returned column is of a BIT datatype. If you leave it out, it will return an INTEGER type. (This may or may not matter to you, depending on how exactly you are going to use the returned result set).