SQL DB2 Replace value in a column - sql

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

Related

Need to fetch Boolean type column as varchar from source table and store as varchar in target table in Amazon redshift

I have been working with redshift for a month. I need your help regarding this;
I need to create a target table using source table (create table target as select * from source), there is a column in source table type of boolean, I need to store that column as varchar in target table. Tried lots of methods like cast, convert..etc nothing worked for me. After done with lots of search, I got to know that boolean cannot be converted to another data type in redshift.
It shows me below error;
Amazon Invalid operation: column "indicator" is of
type boolean but expression is of type character varying; [SQL
State=42804, DB Errorcode=500310] 1 statement failed.
I shall be grateful for your help.
The following approach might work for your case.
When loading the data from source table, instead of selecting all the columns using SELECT *, you explicitly call out the individual columns and for the boolean column use a case expression and evaluate the actual data and return the result as string 'true' or 'false'.
create table target as select colA, colB, case boolColC when true then 'true' else 'false' end from source
If you can't convert directly from Boolean to varchar, you could use
CASE
WHEN boolCol= TRUE THEN 'True'
WHEN boolCol= FALSE THEN 'False'
ELSE 'Unknown'
END AS boolCol
to sidestep the need for conversion.
I am able to resolve the issue using nested case statement at the time of table creation - I am posting the solution here so that It can be helpful for others as well.
case
when name in ('a', 'b') then
case offline_crt when true then 'true' else 'false' end
else 'N/A'
end as indicator
The same issue I was also facing ,please check below how I fixed it.
In my original code one column having Boolean data type and when I was doing union of two table it was failing due to below error.
[Amazon](500310) Invalid operation: UNION types character varying and boolean cannot be matched;
I have attached the screenshot for your reference:
Now I have just used the case statement inside the select for the Boolean data type column and it's working fine.

Cast string as "Column Reference" in HIVE SQL

I have a query that is meant to use bind values to retrieve information from a table and test if any field is NULL. The user enters a column name for the bind value and that column is then tested for any NULL values. Here is a simplified version of the query:
SELECT
CASE
WHEN ISNULL(bind.value)
THEN 'PASS'
ELSE 'FAIL'
END AS Solution
This keeps returning 'FAIL' I think because ISNULL() is testing the column entered as a string. Instead, I need it to test the fields in the column, rather than the string holding the column's name. Is there anyway to cast this string as a reference or pointer (I know SQL doesn't have pointer but a pointer-like object) to the a column?
NOTE: When I replace bind.value with the column name it returns 'PASS.' I'm really trying keep this as dynamic as possible so can utilize it with other tables without having to write a new query for each table I use this on.
Most probably you are passing empty strings instead of NULL.
empty string '' is not the same as NULL in Hive, add test for empty string:
SELECT
CASE
WHEN ISNULL(bind.value) OR cast(bind.value as string)='' THEN 'PASS'
ELSE 'FAIL'
END AS Solution

Postgres SQL - IN(..) AS... in Select

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).

SQL CASE with Alias

I am trying to change a column name based on the results of a case statement, is this possible and how would I do it...here is what I have so far but I am not good enough at SQL yet.
I want the change the column name of VALUE to become NUMVALUE if the data is numeric and ALNVALUE if the data isn't numeric. Essentially making a three column datatable a four column datatable. Is this possible?
CASE WHEN ISNUMERIC([Value])=1 THEN SELECT [VALUE] AS [NUMVALUE] ELSE SELECT [VALUE] AS [ALNVALUE] END
No, it isn't possible.
Think about the issues you'd run into when you try and use the result set. Anytime you tried to access the data, you would get an exception stating that the column couldn't be found or similar.
Dim data = Results["ColumnName"] would become unreliable.
You will need to make a separate column for each of them or put them all under the same name.
You need two CASEs = two columns:
SELECT
CASE WHEN ISNUMERIC([Value])= 1 THEN [VALUE] END AS [NUMVALUE],
CASE WHEN ISNUMERIC([Value])<>1 THEN [VALUE] END AS [ALNVALUE],
...
FROM myTable

is there any difference between the queries

select field from table where field = 'value'
select field from table where field in ('value')
The reason I'm asking is that the second version allow me to use the same syntax for null values, while in the first version I need to change the condition to 'where field is null'...
When you are comparing a field to a null like field_name=NULL you are comparing to a known data type from a field say varchar to not only an unknown value but also an unknown data type as well, that is, for NULL values. When comparison like field_name=NULL again implies therefore a checking of data type for both and thus the two could not be compared even if the value of the field is actually NULL thus it will always result to false. However, using the IS NULL you are only comparing for the value itself without the implied comparison for data type thus it could result either to false or true depending on the actual value of the field.
See reference here regarding the issue of NULL in computer science and here in relation to the similarity to your question.
Now, for the IN clause (i.e. IN(NULL)) I don't know what RDBMS you are using because when I tried it with MS SQL and MySQL it results to nothing.
See MS SQL example and MySQL example.
There is no difference in your example. The second, slightly longer, query is not usually used for a single value, it is usally seen for multiple values, such as
select field from table where field in ('value1', 'value2')
yes there is difference in both this queries. In first statment you can insert only 1 value in where clause "where field = 'value'" but in second statement in where field you can insert many values using IN clause "where field in (value1,value2..)"
Examples:
1) select field from table where field ='value1';
2) select field from table where field in ('value1', 'value2')
To check null values
SELECT field
FROM tbl_name
WHERE
(field IN ('value1', 'value2', 'value3') OR field IS NULL)