How to exclude "?" for Null value from DB2 Select - sql

I am extracting data from DB2 to Dataset via submitting the job(JCL) using DSNTIAUL. Some of column having null value but after extraction in dataset null value are placed like "?" For example:
Table1:
Select Query :
Select Column1,column2,Cloumn3
from Table1;
Output Dataset :
AAAAA......................?.......BBBBBB
CCCCC......DDDDDD.......................?
Could someone help to exclude "?" from the dataset. I tried with COALESCE but no luck. or should i need to write separate SORT step in JCL to remove "?".
And also is there any possible way extract data into CSV format

Is this what you want?
select column1, coalesce(nullif(column2, '?'), nullif(column3, '?'))
If both columns have '?'s, then this will return NULL.

Related

Hive sql extract one to multiple values from key value pairs

I have a column that looks like:
[{"key_1":true,"key_2":true,"key_3":false},{"key_1":false,"key_2":false,"key_3":false},...]
There can be 1 to many items described by parameters in {} in the column.
I would like to extract values only of parameters described by key_1. Is there a function for that? I tried so far json related functions (json_tuple, get_json_object) but each time I received null.
Consider below json path.
WITH sample_data AS (
SELECT '[{"key_1":true,"key_2":true,"key_3":false},{"key_1":false,"key_2":false,"key_3":false}]' json
)
SELECT get_json_object(json, '$[*].key_1') AS key1_values FROM sample_data;
Query results

How to do a regex compare of string values in 2 columns in big query

So let’s say I have 2 columns, both containing string values,
ColA , ResultCol
I want to check if In a row, the string in ColA is a substring of string in ResultCol
I know about ‘WHERE REGEXP_CONTAINS(col, regex)’ , but how do I do this comparison with string in another column?
Please let me know if I missed explaining any criteria of the question
Thanks!
For your requirement, you can use like operator in BigQuery which will compare the strings of two columns.I have created a sample table Product and ran the below code:
Code
SELECT * from `project.dataset.Product` where product like concat('%',country,'%')
Sample Table
Output

ignoring rows where a column has non-numeric characters in its value

I have a table with a column that has some variable data. I would like to select only the rows that have values with numerical characters [0-9]
The column would look someting like this:
time
1545123
none
1565543
1903-294
I would want the rows with the first and third values only (1545123 and 1565543). None of my approaches have worked.
I've tried:
WHERE time NOT LIKE '%[^0-9]+%'
WHERE NOT regexp_like(time, '%[^0-9]+%')
WHERE regexp_like(time, '[0-9]+')
I've also tried these expressions in a CASE statement, but that was also a no go. Am I missing something here?
This is on Amazon Athena, which uses an older version of Presto
Thanks in advance
You can use regexp matching only numbers like '^[0-9]+$' or '^\d+$':
-- sample data
WITH dataset (time) AS (
VALUES
('1545123'),
('none'),
('1565543'),
('1903-294')
)
--query
select *
from dataset
WHERE regexp_like(time, '^[0-9]+$')
Output:
time
1545123
1565543
Another option which I would say should not be used in this case but can be helpful in some others is using try with cast:
--query
select *
from (
select try(cast(time as INTEGER)) time
from dataset
)
where time is not null

Need to find string using bigquery

We have below string column and having below data
and I want to find Null count present in string columns means how many times null value('') present in front of id column present in select statement
using big query.
Don't use string position.
Expected output:
count of null ('')id =3
1st row,2nd row and 5th row
Below is for BigQuery Standard SQL
#standardSQL
SELECT
FORMAT(
"count of null ('')id = %d. List of id is: %s",
COUNT(*),
STRING_AGG(CAST(ID AS STRING))
) AS output
FROM `project.dataset.table`
WHERE REGEXP_CONTAINS(String, r"(?i)''\s+(?:as|)\s+(?:id|\[id\])")
if to apply to sample data from your question - the output is
Row output
1 count of null ('')id = 3. List of id is: 1,2,5
The idea is to unify all strings to something you can query with like = "%''asid%" or regex
First replace all spaces with ''
replace "[", "]" with ''.
Make the use of " or ' consistent.
Then query with like.
For example:
select 1 from (select replace(replace(replace(replace('select "" as do, "" as [id] form table1',' ',''),'[',''),']',''),'"',"'") as tt)
where tt like ("%''asid%")
Its not a "smart" idea but its simple.
A better idea will be to save the query columns in a repeat column '"" as id' and the table in another column.
You don't need to save 'select' and 'from' this way you can query easily and also assemble a query from the data.
If I understand correctly, you want to count the number of appearances of '' in the string column.
If so, you can use regexp_extract_all():
select t.*,
(select count(*)
from unnest(regexp_extract_all(t.string, "''")) u
) as empty_string_count
from t;

BigQuery Error In where Clause with a String type column

I have a column that contains strings like this '0 0PAA01 CF101 -S07'. I have some records in the database and when I tried to retrieve it using BigQuery the query is not returning records.
I am doing
select *
from table
where column='0 0PAA01 CF101 -S07'
Is a BigQuery Problem?
I'm 99% sure this is not a BigQuery problem - but that the strings are in fact different.
Look at this:
SELECT MD5('my name is Felipe Hoffa') from_keyboard
, MD5(str) from_db
, str='my name is Felipe Hoffa' equal_are_they
FROM (
SELECT 'my name is Felipe Hoffa' str
)
Why are they different? One has a tab instead of a space.