<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to create a widget in SQL with variables? in Get Started Discussions</title>
    <link>https://community.databricks.com/t5/get-started-discussions/how-to-create-a-widget-in-sql-with-variables/m-p/126650#M10420</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/176758"&gt;@zc&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;Unfortunately, I think in case of sql widgets default value needs to be string literals. So above approach won't work.&lt;/P&gt;&lt;P&gt;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:&lt;/P&gt;&lt;P&gt;"&lt;SPAN&gt;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.&lt;/SPAN&gt;"&lt;/P&gt;&lt;P&gt;&lt;A href="https://docs.databricks.com/aws/en/notebooks/notebooks-code" target="_blank" rel="noopener"&gt;Develop code in Databricks notebooks | Databricks Documentation&lt;/A&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 28 Jul 2025 08:56:01 GMT</pubDate>
    <dc:creator>szymon_dybczak</dc:creator>
    <dc:date>2025-07-28T08:56:01Z</dc:date>
    <item>
      <title>How to create a widget in SQL with variables?</title>
      <link>https://community.databricks.com/t5/get-started-discussions/how-to-create-a-widget-in-sql-with-variables/m-p/126521#M10414</link>
      <description>&lt;P&gt;I want to create a widget in SQL and use it in R later. Below is my code&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;%sql&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;declare&lt;/SPAN&gt; &lt;SPAN&gt;or&lt;/SPAN&gt; &lt;SPAN&gt;replace&lt;/SPAN&gt;&lt;SPAN&gt; date1&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;date&lt;/SPAN&gt; &lt;SPAN&gt;=&lt;/SPAN&gt; &lt;SPAN&gt;"2025-01-31"&lt;/SPAN&gt;&lt;SPAN&gt;;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;declare&lt;/SPAN&gt; &lt;SPAN&gt;or&lt;/SPAN&gt; &lt;SPAN&gt;replace&lt;/SPAN&gt;&lt;SPAN&gt; date2 &lt;/SPAN&gt;&lt;SPAN&gt;date&lt;/SPAN&gt;&lt;SPAN&gt; ;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;set&lt;/SPAN&gt; &lt;SPAN&gt;var&lt;/SPAN&gt;&lt;SPAN&gt; date2&lt;/SPAN&gt;&lt;SPAN&gt;=&lt;/SPAN&gt;&lt;SPAN&gt;add_months(date1,&lt;/SPAN&gt;&lt;SPAN&gt;5&lt;/SPAN&gt;&lt;SPAN&gt;);&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;What's the correct syntax of using date2 to create a widget? I have tried&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;CREATE&lt;/SPAN&gt;&lt;SPAN&gt; WIDGET &lt;/SPAN&gt;&lt;SPAN&gt;TEXT&lt;/SPAN&gt;&lt;SPAN&gt; date_end &amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;DEFAULT&lt;/SPAN&gt;&lt;SPAN&gt; date2;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;CREATE WIDGET TEXT date_end &amp;nbsp;DEFAULT&amp;nbsp;$date2;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;CREATE WIDGET TEXT date_end &amp;nbsp;DEFAULT&amp;nbsp;:date2;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;and none worked.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;If there are other ways to let R access a variable in SQL, I'd love to learn that too.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;Thank you,&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Sat, 26 Jul 2025 05:19:15 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/how-to-create-a-widget-in-sql-with-variables/m-p/126521#M10414</guid>
      <dc:creator>zc</dc:creator>
      <dc:date>2025-07-26T05:19:15Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a widget in SQL with variables?</title>
      <link>https://community.databricks.com/t5/get-started-discussions/how-to-create-a-widget-in-sql-with-variables/m-p/126646#M10418</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/176758"&gt;@zc&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This should work.&lt;/P&gt;&lt;P&gt;&lt;A href="https://docs.databricks.com/aws/en/notebooks/widgets" target="_blank"&gt;https://docs.databricks.com/aws/en/notebooks/widgets&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 28 Jul 2025 08:29:07 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/how-to-create-a-widget-in-sql-with-variables/m-p/126646#M10418</guid>
      <dc:creator>Khaja_Zaffer</dc:creator>
      <dc:date>2025-07-28T08:29:07Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a widget in SQL with variables?</title>
      <link>https://community.databricks.com/t5/get-started-discussions/how-to-create-a-widget-in-sql-with-variables/m-p/126647#M10419</link>
      <description>&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;The correct syntax for your scenario is:&lt;/P&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;SPAN class=""&gt;SQL&lt;/SPAN&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;PRE&gt;&lt;SPAN class=""&gt;CREATE&lt;/SPAN&gt; WIDGET TEXT date_end &lt;SPAN class=""&gt;DEFAULT&lt;/SPAN&gt; :date2;&lt;/PRE&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Mon, 28 Jul 2025 08:32:59 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/how-to-create-a-widget-in-sql-with-variables/m-p/126647#M10419</guid>
      <dc:creator>Khaja_Zaffer</dc:creator>
      <dc:date>2025-07-28T08:32:59Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a widget in SQL with variables?</title>
      <link>https://community.databricks.com/t5/get-started-discussions/how-to-create-a-widget-in-sql-with-variables/m-p/126650#M10420</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/176758"&gt;@zc&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;Unfortunately, I think in case of sql widgets default value needs to be string literals. So above approach won't work.&lt;/P&gt;&lt;P&gt;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:&lt;/P&gt;&lt;P&gt;"&lt;SPAN&gt;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.&lt;/SPAN&gt;"&lt;/P&gt;&lt;P&gt;&lt;A href="https://docs.databricks.com/aws/en/notebooks/notebooks-code" target="_blank" rel="noopener"&gt;Develop code in Databricks notebooks | Databricks Documentation&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 28 Jul 2025 08:56:01 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/how-to-create-a-widget-in-sql-with-variables/m-p/126650#M10420</guid>
      <dc:creator>szymon_dybczak</dc:creator>
      <dc:date>2025-07-28T08:56:01Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a widget in SQL with variables?</title>
      <link>https://community.databricks.com/t5/get-started-discussions/how-to-create-a-widget-in-sql-with-variables/m-p/126715#M10423</link>
      <description>&lt;P&gt;Thank you for responding, Khaja. But the code you posted won't work as I described in my question.&lt;/P&gt;</description>
      <pubDate>Mon, 28 Jul 2025 15:10:43 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/how-to-create-a-widget-in-sql-with-variables/m-p/126715#M10423</guid>
      <dc:creator>zc</dc:creator>
      <dc:date>2025-07-28T15:10:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a widget in SQL with variables?</title>
      <link>https://community.databricks.com/t5/get-started-discussions/how-to-create-a-widget-in-sql-with-variables/m-p/126716#M10424</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/110502"&gt;@szymon_dybczak&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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?&lt;/P&gt;</description>
      <pubDate>Mon, 28 Jul 2025 15:14:44 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/how-to-create-a-widget-in-sql-with-variables/m-p/126716#M10424</guid>
      <dc:creator>zc</dc:creator>
      <dc:date>2025-07-28T15:14:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a widget in SQL with variables?</title>
      <link>https://community.databricks.com/t5/get-started-discussions/how-to-create-a-widget-in-sql-with-variables/m-p/126724#M10425</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/176758"&gt;@zc&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;Following works in python cell:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;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)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 28 Jul 2025 16:06:10 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/how-to-create-a-widget-in-sql-with-variables/m-p/126724#M10425</guid>
      <dc:creator>szymon_dybczak</dc:creator>
      <dc:date>2025-07-28T16:06:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a widget in SQL with variables?</title>
      <link>https://community.databricks.com/t5/get-started-discussions/how-to-create-a-widget-in-sql-with-variables/m-p/126736#M10426</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/110502"&gt;@szymon_dybczak&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;</description>
      <pubDate>Mon, 28 Jul 2025 16:55:58 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/how-to-create-a-widget-in-sql-with-variables/m-p/126736#M10426</guid>
      <dc:creator>zc</dc:creator>
      <dc:date>2025-07-28T16:55:58Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a widget in SQL with variables?</title>
      <link>https://community.databricks.com/t5/get-started-discussions/how-to-create-a-widget-in-sql-with-variables/m-p/126794#M10431</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/110502"&gt;@szymon_dybczak&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;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?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Jul 2025 12:30:20 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/how-to-create-a-widget-in-sql-with-variables/m-p/126794#M10431</guid>
      <dc:creator>Khaja_Zaffer</dc:creator>
      <dc:date>2025-07-29T12:30:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a widget in SQL with variables?</title>
      <link>https://community.databricks.com/t5/get-started-discussions/how-to-create-a-widget-in-sql-with-variables/m-p/126796#M10432</link>
      <description>&lt;P&gt;2/2 and why it doesnt work any idea?&lt;/P&gt;</description>
      <pubDate>Tue, 29 Jul 2025 12:30:49 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/how-to-create-a-widget-in-sql-with-variables/m-p/126796#M10432</guid>
      <dc:creator>Khaja_Zaffer</dc:creator>
      <dc:date>2025-07-29T12:30:49Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a widget in SQL with variables?</title>
      <link>https://community.databricks.com/t5/get-started-discussions/how-to-create-a-widget-in-sql-with-variables/m-p/126803#M10433</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/173840"&gt;@Khaja_Zaffer&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;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.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="szymon_dybczak_0-1753795752838.png" style="width: 400px;"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/18593i86050A2653CD15F5/image-size/medium?v=v2&amp;amp;px=400" role="button" title="szymon_dybczak_0-1753795752838.png" alt="szymon_dybczak_0-1753795752838.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Jul 2025 13:29:33 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/how-to-create-a-widget-in-sql-with-variables/m-p/126803#M10433</guid>
      <dc:creator>szymon_dybczak</dc:creator>
      <dc:date>2025-07-29T13:29:33Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a widget in SQL with variables?</title>
      <link>https://community.databricks.com/t5/get-started-discussions/how-to-create-a-widget-in-sql-with-variables/m-p/126912#M10442</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/110502"&gt;@szymon_dybczak&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Indeed it is. Sorry that I could resolve the issue at first instance. but yeah, it accepts strings no matter which widget it is.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 30 Jul 2025 09:02:34 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/how-to-create-a-widget-in-sql-with-variables/m-p/126912#M10442</guid>
      <dc:creator>Khaja_Zaffer</dc:creator>
      <dc:date>2025-07-30T09:02:34Z</dc:date>
    </item>
  </channel>
</rss>

