<?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: Add timestamp to table name using SQL Editor in Warehousing &amp; Analytics</title>
    <link>https://community.databricks.com/t5/warehousing-analytics/add-timestamp-to-table-name-using-sql-editor/m-p/57494#M1124</link>
    <description>&lt;P data-unlink="true"&gt;Hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/616"&gt;@shan_chandra&lt;/a&gt;,&amp;nbsp;&lt;/P&gt;&lt;P data-unlink="true"&gt;that worked perfectly.&amp;nbsp; For my specific case I wanted the previous month as the table name suffix so used the below.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# import datetime module
import datetime
# get current date
curr_date = datetime.date.today()
# get first day of current month
first =curr_date.replace(day=1)
# get last day of previous month
last_month = first-datetime.timedelta(days=1)
# create last month suffix formatted as YYYYMM
suffix = last_month.strftime("%Y%m")
# create time stamped table by renaming temp table
table_name = f"hive_metastore.db.table_name_{suffix}"
spark.sql(f"ALTER TABLE hive_metastore.db.table_name RENAME TO {table_name};")&lt;/LI-CODE&gt;&lt;DIV&gt;&lt;DIV&gt;Thanks for your help,&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Bruno&lt;/DIV&gt;&lt;/DIV&gt;</description>
    <pubDate>Tue, 16 Jan 2024 16:27:31 GMT</pubDate>
    <dc:creator>BobDobalina</dc:creator>
    <dc:date>2024-01-16T16:27:31Z</dc:date>
    <item>
      <title>Add timestamp to table name using SQL Editor</title>
      <link>https://community.databricks.com/t5/warehousing-analytics/add-timestamp-to-table-name-using-sql-editor/m-p/56856#M1117</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;&lt;P&gt;am sure am missing something as this should be something trivial but am struggling to find how to add a suffix with a date to a table name.&lt;/P&gt;&lt;P&gt;Does anyone have a way to do this?&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Wed, 10 Jan 2024 11:15:44 GMT</pubDate>
      <guid>https://community.databricks.com/t5/warehousing-analytics/add-timestamp-to-table-name-using-sql-editor/m-p/56856#M1117</guid>
      <dc:creator>BobDobalina</dc:creator>
      <dc:date>2024-01-10T11:15:44Z</dc:date>
    </item>
    <item>
      <title>Re: Add timestamp to table name using SQL Editor</title>
      <link>https://community.databricks.com/t5/warehousing-analytics/add-timestamp-to-table-name-using-sql-editor/m-p/56967#M1118</link>
      <description>&lt;P&gt;Could you share more details on the issue you are facing?&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jan 2024 17:13:57 GMT</pubDate>
      <guid>https://community.databricks.com/t5/warehousing-analytics/add-timestamp-to-table-name-using-sql-editor/m-p/56967#M1118</guid>
      <dc:creator>Lakshay</dc:creator>
      <dc:date>2024-01-11T17:13:57Z</dc:date>
    </item>
    <item>
      <title>Re: Add timestamp to table name using SQL Editor</title>
      <link>https://community.databricks.com/t5/warehousing-analytics/add-timestamp-to-table-name-using-sql-editor/m-p/56973#M1119</link>
      <description>&lt;P&gt;Hi &lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/97915"&gt;@BobDobalina&lt;/a&gt;&amp;nbsp; - Dynamic naming of table name is not allowed in DBSQL. However, you can try something similar&lt;/P&gt;
&lt;LI-CODE lang="python"&gt;%python
from datetime import datetime
date_suffix = datetime.now().strftime("%Y%m%d")
table_name = f"students{date_suffix}"
spark.sql(f"CREATE TABLE IF NOT EXISTS hive_metastore.db.{table_name} AS SELECT * FROM hive_metastore.db.`students`;")&lt;/LI-CODE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screenshot 2024-01-11 at 11.19.16 AM.png" style="width: 796px;"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/5788iF8F6DEEF2C3951B4/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screenshot 2024-01-11 at 11.19.16 AM.png" alt="Screenshot 2024-01-11 at 11.19.16 AM.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Hope this helps!!!&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;Shan&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jan 2024 17:20:23 GMT</pubDate>
      <guid>https://community.databricks.com/t5/warehousing-analytics/add-timestamp-to-table-name-using-sql-editor/m-p/56973#M1119</guid>
      <dc:creator>shan_chandra</dc:creator>
      <dc:date>2024-01-11T17:20:23Z</dc:date>
    </item>
    <item>
      <title>Re: Add timestamp to table name using SQL Editor</title>
      <link>https://community.databricks.com/t5/warehousing-analytics/add-timestamp-to-table-name-using-sql-editor/m-p/57494#M1124</link>
      <description>&lt;P data-unlink="true"&gt;Hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/616"&gt;@shan_chandra&lt;/a&gt;,&amp;nbsp;&lt;/P&gt;&lt;P data-unlink="true"&gt;that worked perfectly.&amp;nbsp; For my specific case I wanted the previous month as the table name suffix so used the below.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# import datetime module
import datetime
# get current date
curr_date = datetime.date.today()
# get first day of current month
first =curr_date.replace(day=1)
# get last day of previous month
last_month = first-datetime.timedelta(days=1)
# create last month suffix formatted as YYYYMM
suffix = last_month.strftime("%Y%m")
# create time stamped table by renaming temp table
table_name = f"hive_metastore.db.table_name_{suffix}"
spark.sql(f"ALTER TABLE hive_metastore.db.table_name RENAME TO {table_name};")&lt;/LI-CODE&gt;&lt;DIV&gt;&lt;DIV&gt;Thanks for your help,&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Bruno&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Tue, 16 Jan 2024 16:27:31 GMT</pubDate>
      <guid>https://community.databricks.com/t5/warehousing-analytics/add-timestamp-to-table-name-using-sql-editor/m-p/57494#M1124</guid>
      <dc:creator>BobDobalina</dc:creator>
      <dc:date>2024-01-16T16:27:31Z</dc:date>
    </item>
  </channel>
</rss>

