Find value in any column in a table

Adil
Databricks Partner

Hi,

I'm not sure if this is a possible scenario, but is there, by any chance a way to query all the columns of a table for searching a value? 

Explanation: I want to search for a specific value in all the columns of a databricks table. I don't know which column that value might be present, or not. I just am not sure what column holds that value. 

Thoughts?


sashiDatabricks
New Contributor II

I also have this same requirement now and cant find the solution for this yet. Any help would be good. thanks

HAVIC
New Contributor II

Select * from table as t where '1234' in (t.*)

 

iyashk-DB
Databricks Employee
Databricks Employee

You can do something like this, but first run the following code in Python:

table = "my_table"
value = "foo"

cols = spark.table(table).columns
sql = f"""
SELECT *
FROM {table}
WHERE 
  {" OR ".join([f"CAST({c} AS STRING) LIKE '%{value}%'" for c in cols])}
"""
print(sql)

, then run this sql query. 

Hubert-Dudek
Databricks MVP

HubertDudek_0-1764794875823.gif

 


My blog: https://databrickster.medium.com/

Thanks Hubert, using this wild card seems really helpful