07-25-2025 10:19 PM
I want to create a widget in SQL and use it in R later. Below is my code
%sql
07-28-2025 01:55 AM - edited 07-28-2025 01:56 AM
Hi @zc ,
Unfortunately, I think in case of sql widgets default value needs to be string literals. So above approach won't work.
Regarding your second question about accessing variables decalared in SQL in R cell, you cannot do such a thing. Here's an excerpt from documentation:
"When you invoke a language magic command, the command is dispatched to the REPL in the execution context for the notebook. Variables defined in one language (and hence in the REPL for that language) are not available in the REPL of another language. REPLs can share state only through external resources such as files in DBFS or objects in object storage."
Develop code in Databricks notebooks | Databricks Documentation
07-28-2025 01:29 AM
07-28-2025 01:32 AM
when using SQL to create a widget and set its default value to a previously declared variable, you should use a colon (:) prefix to reference the variable.
The correct syntax for your scenario is:
CREATE WIDGET TEXT date_end DEFAULT :date2;
07-28-2025 08:10 AM
Thank you for responding, Khaja. But the code you posted won't work as I described in my question.
07-28-2025 01:55 AM - edited 07-28-2025 01:56 AM
Hi @zc ,
Unfortunately, I think in case of sql widgets default value needs to be string literals. So above approach won't work.
Regarding your second question about accessing variables decalared in SQL in R cell, you cannot do such a thing. Here's an excerpt from documentation:
"When you invoke a language magic command, the command is dispatched to the REPL in the execution context for the notebook. Variables defined in one language (and hence in the REPL for that language) are not available in the REPL of another language. REPLs can share state only through external resources such as files in DBFS or objects in object storage."
Develop code in Databricks notebooks | Databricks Documentation
07-28-2025 08:14 AM
Thank you for the information. If I understand correctly, you meant one cannot use a SQL variable for a SQL widget, but only hard coded strings?
07-28-2025 09:05 AM - edited 07-28-2025 09:06 AM
Hi @zc ,
Exactly, this is what I meant. What is funny though, that in python cell you can define variable and then later use it as a widget's default value. So there's a bit of inconsistency here between different types of cells.
Following works in python cell:
from datetime import datetime
from dateutil.relativedelta import relativedelta
date1_str = "2025-01-31"
date2_str = None
date1_obj = datetime.strptime(date1_str, "%Y-%m-%d").date()
date2_obj = date1_obj + relativedelta(months=5)
date2_str = date2_obj.strftime("%Y-%m-%d")
print(f"date1: {date1_str}") # 2025-01-31
print(f"date2: {date2_str}") # 2025-06-30
dbutils.widgets.text("date1", date1_str)
dbutils.widgets.text("date2", date2_str)
07-28-2025 09:55 AM
Thank you for the details. I figured that part out using R as well. Turns out it's easy to share variables from Python/R to SQL, but not the other way around.
07-29-2025 05:30 AM
Hello @szymon_dybczak
Thanks for the information, I did a repo of this, indeed I was using all purpose cluster and found that while SQL commands doesnt help in creating widgets if used as date format. but it worked when I converted date into string. while microsoft didnt explicitly mentioned this, I wonder hwo did you figured it out?
07-29-2025 06:29 AM
Hi @Khaja_Zaffer ,
Sure, I just checked what are expected data types for arguments using dbutils.widgets.help(). As you can see, for defaultValue they require String data type.
07-30-2025 02:02 AM
Hello @szymon_dybczak
Indeed it is. Sorry that I could resolve the issue at first instance. but yeah, it accepts strings no matter which widget it is.
07-29-2025 05:30 AM
2/2 and why it doesnt work any idea?
Passionate about hosting events and connecting people? Help us grow a vibrant local community—sign up today to get started!
Sign Up Now