cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results for 
Search instead for 
Did you mean: 

Pass date value as parameter in Databricks SQL notebook

sp1
New Contributor II

I want to pass yesterday date (In the example 20230115*.csv) in the csv file. Don't know how to create parameter and use it here.

CREATE OR REPLACE TEMPORARY VIEW abc_delivery_log

USING CSV

OPTIONS (

 header="true",

 delimiter=",",

 inferSchema="true",

 path="abfss://selfserv-xxxxx@xxxxx/xxxxx/abc_delivery_log_20230115080013.csv"

);

1 ACCEPTED SOLUTION

Accepted Solutions

daniel_sahal
Esteemed Contributor

You can use dbutils.widgets to pass a parameter to your notebook (https://docs.databricks.com/notebooks/widgets.html) then use Python to create and execute your parametrized sql code.

View solution in original post

5 REPLIES 5

daniel_sahal
Esteemed Contributor

You can use dbutils.widgets to pass a parameter to your notebook (https://docs.databricks.com/notebooks/widgets.html) then use Python to create and execute your parametrized sql code.

Chaitanya_Raju
Honored Contributor

Hi @Sujatha Pillai​ 

You can try using the below SQL code using widgets

CREATE WIDGET TEXT pathname DEFAULT "pathvalue"
 
CREATE OR REPLACE TEMPORARY VIEW abc_delivery_log
USING CSV
OPTIONS (
 header="true",
 delimiter=",",
 inferSchema="true",
 path = getArgument('pathname')
);

and using widgets you can pass the variable value

Happy Learning!!

Thanks for reading and like if this is useful and for improvements or feedback please comment.

Anonymous
Not applicable

SQL has a function for current_timestamp, which you can cast as a date to get todays date. You don't need to pass anything to the notebook for this logic to work. Image

sp1
New Contributor II

Thanks for all your replies.

I have used widget using date and it's working as expected.

Here is the code:

drop view if exists inputdate1;

create view inputdate1 as select date_format(current_date()-1, 'yyyyMMdd') AS date UNION ALL select date_format(current_date(), 'yyyyMMdd') AS date;

%python

dataframe = sqlContext.sql('select * from inputdate1') 

values = [row.date for row in dataframe.select('date').collect()]

default_value = str(min(values))

values_str = [str(value) for value in values]

dbutils.widgets.dropdown("date", default_value, values_str)

CREATE OR REPLACE TEMPORARY VIEW abc_delivery_log

USING CSV

OPTIONS (

 header="true",

 delimiter=",",

 inferSchema="true",

 path="abfss://selfserv-xxxxx@xxxxx/xxxxx/abc_delivery_log_delivery_log_${date}*.csv"

);

Asifpanjwani
New Contributor II

@Retired_mod @sp1 @Chaitanya_Raju @daniel_sahal 

Hi Everyone,

I need the same scenario on SQL code, because my DBR cluster not allowed me to run python code

Error: Unsupported cell during execution. SQL warehouses only support executing SQL cells.

I appreciate your help

Thanks

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you won’t want to miss the chance to attend and share knowledge.

If there isn’t a group near you, start one and help create a community that brings people together.

Request a New Group