Someone gave me this code:
select * from table where is_active
Is the is_active column, despite not having been set to anything, set to TRUE by default? Does the above query mean this:
select * from table where is_active = TRUE
It appears so, but couldn't find anything in the PostgreSQL documentation, so just wanted to confirm.
Yes. You are right here. When the system looks at this query select * from table_name where col1>col2, it tries to find effective boolean expression after where clause, and ends up evaluating the expression to either true or false. But since the column value is already boolean so there is no need for that. select * from table where is_active is infact better. This is true for all SQL languages
Related
How can we logically AND-connect two boolean columns in SQL in the select clause?
Pseudo-SQL of what I have in mind:
select boolean_col_A AND boolean_col_B as joint_bool from foo where id = 42
Note that I am not limiting the number of records through a boolean predicate but rather create a "synthetic" i.e. surrogate column in the result set.
I feel there must be a simple way to achieve this but I'm not seeing it. It should work for Oracle, Postgres and H2.
IMHO, use case might be a better option because Oracle is included, To the best of my knowledge Oracle can't have a boolean column so usually we just use a varchar(1) or char(1) with t/f
So instead of having boolean column, you can considerate just use string column with t/f across all database, then a simple case (like below) can work with all of them.
select case when colA = 't' and colB = 't' then 't' else 'f' end as joint_bool
from [TableName]
Thanks for all the feedback. My application uses Liquibase to abstract schema definitions. It maps the boolean type as follows:
H2: BOOLEAN
Postgres: BOOLEAN
Oracle: NUMBER(1)
Source: https://stackoverflow.com/a/28626825/131929
This is also the answer to #codeflush.dev's Q in the comments.
which datatype(s) do you use for each boolean in each RDBMS?
Given the list of effective RDBMS types above one way to go about this surrogate boolean column in the result set is to use BITAND. It is supported in the all database products relevant to this questions.
The more general CASE WHEN approach presented in the other answer is likely ok if you can't use BITAND.
select
(case when boolean_col_A =1 and boolean_col_B=1 then 'true'
else 'false'
end ) joint_bool
from foo where id = 42
try this.
I have the following column, B represents boolean and the rest are empty values. I have to change all the values in this column to the word COLUMN A.
COLUMN
-----
B
I have tried different things, for example
SELECT COLUMN
FROM TABLE
WHERE COALESCE(NULLIF(COLUMN,''), 'COLUMN A');
And I receive the error: "Invalid character found in a character string argument of the function "BOOLEAN"." I'm kind of stuck to this question and I'm getting confused with this boolean value. I will be really happy if someone can help me, thanks!
The easiest thing is to use CASE expression. I am not familiar in db2, so you may want to research it further, but in other DBMSs it works like this:
SELECT CASE
WHEN COLUMN = '' THEN 'COLUMN A' -- if COLUMN = '', replace it with 'COLUMN A'
ELSE COLUMN -- otherwise, keep COLUMN as is.
END as 'COLUMN' -- name the column in the result 'COLUMN'
FROM TABLE
This is an article that explains how it works in db2:
https://www.ibm.com/support/knowledgecenter/en/SSEPEK_11.0.0/sqlref/src/tpc/db2z_caseexpression.html
The WHERE clause is unfinished. Compare the COALESCEd value to something:
SELECT COLUMN
FROM TABLE
WHERE COALESCE(NULLIF(COLUMN,''), 'COLUMN A') = 'COLUMN A';
Or better:
SELECT COLUMN
FROM TABLE
WHERE COLUMN IS NULL OR COLUMN = ''
Doesn't require any thinking/calculating to work out your selection logic. More maintainable, nicer for peer developers
*The above is generic advice for usual cases NOT involving boolean datatypes (which typically require some different treatment)
Now, you say you have to change the value to something. That requires an UPDATE statement. If this column is a boolean then it won't have a value of empty string. The blanks will be nulls:
UPDATE TABLE SET COLUMN = (some boolean) WHERE COLUMN IS NULL
If you don't want to permanently change the table data to something, but instead want to select it out as some value where a blank occurs, but keep the blanks stored in the table:
SELECT COALESCE(column, (some boolean)) FROM TABLE
Might be worth noting that not all versions of DB2 can return a boolean in a result set - this is quite typical of database vendors. Convert the boolean to something else representable using a case when, if your DB2 version is thus restricted
SELECT CASE WHEN column = TRUE THEN 'true' ELSE 'false' END FROM TABLE
Can someone help me undertsand the IN(...) AS... in the select. This is the first time I'm seeing something like this. Also, how does the database determine the row value or newID? This is on Postgres datababse.
SELECT tbl_a.*, (
tbl_z.z_id IN (
SELECT x_id
FROM table_x
WHERE name = '...'
)
) AS newID
FROM (...)
newId will be of type boolean.
true, if
tbl_z.z_id IN (
SELECT x_id
FROM table_x
WHERE name = '...'
)
and false if not.
AS column_name is alias.
update
answering your comment.
https://www.postgresql.org/docs/current/static/sql-select.html#SQL-WHERE
where condition is any expression that evaluates to a result of type
boolean.
So if you have IN (...) in WHERE it returns same boolean as in column list. True or false. No difference - where or as column, returns either true or false
The second column is the answer to the question "is tbl_z.z_id in the list of x_id from table_x where name = '...'?"
As that's a yes/no question (which 'IN' always means), it's a boolean (either true or false).
The "AS" clause tells it what to name that column (it's not directly from a table, so it doesn't know what to call it). It's not strictly required, but it makes addressing that column in your output much easier, you can also use it if you want the column name to be different then the name in the table you're coming from (if you're joining two tables, and they happen to have each have a column with the same names is a use case for this).
I have a query here and I'm curious if there is a shorter way of writing this query, meaning to reduce the query from using an IF argument to determine if it should use the param or not in the statement. Please see below:
#Param varchar(10) = NULL
IF #Param IS NOT NULL
BEGIN
SELECT * FROM TABLE WHERE Column = #Param
END
ELSE
BEGIN
SELECT * FROM TABLE
END
Could this be reduced to one simple query instead like this?
#Param varchar(10) = NULL
SELECT * FROM TABLE WHERE Column = COALESCE( Any, #Param )
I looked at Coalesce, but didn't see if I could use an Any sort of feature. I hope this makes sense.
Question is how to acheive this. Second question is which would be better on performance?
There is no any style command, but if you use the #Param first and then the value in the column itself, that should work...
SELECT * FROM TABLE WHERE Column = COALESCE(#Param, Column)
If #Param is Null then it is ignored and the next item in the list is used.
You can also use IsNull to do the same thing...
SELECT * FROM TABLE WHERE Column = ISNULL(#Param, Column)
To answer the 2nd part of the question, my feeling is that using a separate statement will always be more efficient. The condensed version might be less code, but isn't necessarily easier to understand or quicker to run.
My question is:
In Oracle regexp_like works alone in the where clause without having to compare to 1 or 0 or a string. The function can only be called when evaluating something in a case statement or the where clause. Since it can't be described (tried searching the data dictionary for it) I'm wondering how to write a function that works the same way.
For example:
function is_prod
returns boolean
is
l_var boolean := false;
begin
if sys_context('userenv','db_unique_name') = '"PROD_SERVER"' then
l_var := true;
end if;
return l_var;
end;
That function compiles, but cannot be used in a SQL statement like the following:
select *
from table t
where is_prod
Because I get the following error: ORA-00920: invalid relational operator.
Comparing it to a number or true doesn't work either.
Where can I find the code base for regexp_like or what do I need to do to make this work like regexp_like?
Note: I've looked around for several hours and found that Oracle's regexp functions are actually java calls, but that means they still need a pl/sql wrapper.
Basically, oracle has a boolean datatype only for PLSQL.
So, as long as you stay in plsql you can use them but not in SQL.
From documentation:
Because SQL has no data type equivalent to BOOLEAN, you cannot:
Assign a BOOLEAN value to a database table column
Select or fetch the value of a database table column into a BOOLEAN
variable
Use a BOOLEAN value in a SQL statement, SQL function, or PL/SQL
function invoked from a SQL statement
If you want to find metadata about built-in functions, then maybe this post can help.
SQL Doesn't work like that. the where statement always looks for a function/column where something is. even if the function works you still have to tell the where statement which value you want True or False
I haven't used Oracle SQL, but looking at what you have there I think that if you write it like this
select *
from table t
where is_prod = True
it will work, if you change the Variable type in your function to something like a Varchar(5) or something similar.
you are actually asking that function to look at several records, so when you have it like you do it acts like a Select Statement and not like a where statement. it will give the value of the function but not filter the where.
it will look like a column with true or false values.
When you use the function in a Where statement like this:
SELECT *
FROM table t
WHERE is_Prod
it's like saying:
SELECT *
FROM table t
WHERE Column1
you have to clarify for the WHERE Statement
SELECT *
FROM table t
WHERE Column1 = 'blue' or is_Prod = 'false'
in C# you can use a String as a boolean, if it is null it returns false
in SQL Server it comes out like this
Column2 IS NULL
you still need an operator
* Separator *
as I don't use Oracle I was unable to test this.
http://docs.oracle.com/cd/B14117_01/server.101/b10759/conditions018.htm
REGEXP_LIKE is a like Statement. so it uses a comparison operator.
you could probably write a regexp in a like statement, although I am sure that it is time consuming and monotonous so they made a function that does it for you.
in other words you still have to use the '= whatever' on the function that you created.