0

I am writing an SQL query that joins two tables. The problem that I am facing is that the column on which I am joining is blank (""," ") on one table and null on the other.

Table A

id col
1
2
3 SG

Table B

id col
a null
b null
c SG
source_alleg = spark.sql("""
    SELECT A.*,B.COL as COLB FROM TABLEA A LEFT JOIN TABLEB B 
    ON A.COL = B.COL
    """)

For my use case blank values and null are same. I want to do something like Trim(a.col) which will convert blank values to null and hence find all the matches in the join. Output:

id col colb
1 either null or blank either null or blank
2 either null or blank either null or blank
3 SG SG

1 Answer 1

1

In sql the NULL are ignored during a join unless you use a outer join or full join more information : https://www.geeksforgeeks.org/difference-between-left-right-and-full-outer-join/

if you want to convert the nulls to a string you can just use an if

select 
   if(isnull(trim(col1)),"yourstring", col1),
   if(isnull(trim(col2)),"yourstring", col2)
      from T;
Sign up to request clarification or add additional context in comments.

3 Comments

I want to compare the null values with the blank values in the ON statement.
The data is huge so I want to avoid any updates to the table.
@VanditGoel in that case you should be able to do it in the ON, for instance ON if(isnull(trim(T1.col)),"yourstring", T1.col) = if(isnull(trim(T2.col)),"yourstring", T2.col)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.