<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>SQL &#8211; Other Things</title>
	<atom:link href="https://blog.adamzolo.com/tag/sql/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.adamzolo.com</link>
	<description>Blog about Things by Adam Zolotarev</description>
	<lastBuildDate>Fri, 13 Jan 2023 01:23:28 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>
	<item>
		<title>Comparing PostgreSQL timestamps without timezones with dates with a timezone</title>
		<link>https://blog.adamzolo.com/comparing-postgresql-timestamps-without-timezones-with-dates-with-a-timezone/</link>
					<comments>https://blog.adamzolo.com/comparing-postgresql-timestamps-without-timezones-with-dates-with-a-timezone/#respond</comments>
		
		<dc:creator><![CDATA[Adam Zolo]]></dc:creator>
		<pubDate>Thu, 12 Jan 2023 23:04:33 +0000</pubDate>
				<category><![CDATA[SQL]]></category>
		<guid isPermaLink="false">https://blog.adamzolo.com/?p=1041</guid>

					<description><![CDATA[Problem: you have a timestamp field and you need to compare it with something that has a timezone. Let&#8217;s assume your database default timezone is UTC (can check it with show timezone;). Thus, we are making the assumption that your dates are stored in UTC. Since timestamp does not have any timezone data, we first&#8230;<p><a class="more-link" href="https://blog.adamzolo.com/comparing-postgresql-timestamps-without-timezones-with-dates-with-a-timezone/" title="Continue reading &#8216;Comparing PostgreSQL timestamps without timezones with dates with a timezone&#8217;">Continue reading <span class="meta-nav">&#8594;</span></a></p>]]></description>
										<content:encoded><![CDATA[
<p>Problem: you have a <code>timestamp</code> field and you need to compare it with something that has a timezone.</p>



<p>Let&#8217;s assume <strong>your database default timezone is UTC</strong> (can check it with <code>show timezone</code>;). Thus, we are making the assumption that your dates are stored in UTC.</p>



<p>Since <code>timestamp</code> does not have any timezone data, we first need to read it in UTC, so it adds the timezone data. Then we can convert it to the desired timezone:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: sql; title: ; notranslate">
select timestamp_field AT TIME ZONE &#039;UTC&#039; AT TIME ZONE &#039;America/New_York&#039;
</pre></div>


<p>Now, we have the timezone and proper daylight-saving offset.</p>



<p>Just for fun, let&#8217;s check if the current hour matches the hour from the saved timestamp field in the &#8216;America/New_York&#8217; timezone:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: sql; title: ; notranslate">
EXTRACT ( HOUR FROM timestamp_field at time zone &#039;UTC&#039; at time zone &#039;America/New_York&#039;) = EXTRACT ( HOUR FROM now() at time zone &#039;America/New_York&#039;)
</pre></div>


<p>Notice, that we do not read now() in UTC timezone first, because our DB default timezone is UTC and now() already has all of the timezone data.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.adamzolo.com/comparing-postgresql-timestamps-without-timezones-with-dates-with-a-timezone/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Getting Min Max ids after splitting SQL table into n equal parts using ntile</title>
		<link>https://blog.adamzolo.com/getting-min-max-ids-after-splitting-sql-table-into-n-equal-parts-using-ntile/</link>
					<comments>https://blog.adamzolo.com/getting-min-max-ids-after-splitting-sql-table-into-n-equal-parts-using-ntile/#respond</comments>
		
		<dc:creator><![CDATA[Adam Zolo]]></dc:creator>
		<pubDate>Wed, 10 Jun 2020 13:11:10 +0000</pubDate>
				<category><![CDATA[SQL]]></category>
		<guid isPermaLink="false">http://blog.adamzolo.com/?p=929</guid>

					<description><![CDATA[This will split your_table into 10 equal parts, and give you the minimum and maximum id for each part:]]></description>
										<content:encoded><![CDATA[
<p>This will split your_table into 10 equal parts, and give you the minimum and maximum id for each part:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: sql; title: ; notranslate">
with cte as
(
select
id,
ntile(10) over(order by a.id) as bucket_id
from your_table as a
group by a.id
)

select bucket_id, min(id), max(id)
from cte
group by bucket_id
order by bucket_id
</pre></div>]]></content:encoded>
					
					<wfw:commentRss>https://blog.adamzolo.com/getting-min-max-ids-after-splitting-sql-table-into-n-equal-parts-using-ntile/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Removing Duplicate Data from SQL Query Caused by New Line Character</title>
		<link>https://blog.adamzolo.com/removing-duplicate-data-sql-newline-character/</link>
					<comments>https://blog.adamzolo.com/removing-duplicate-data-sql-newline-character/#respond</comments>
		
		<dc:creator><![CDATA[Adam Zolo]]></dc:creator>
		<pubDate>Mon, 16 Sep 2013 21:08:03 +0000</pubDate>
				<category><![CDATA[SQL]]></category>
		<guid isPermaLink="false">http://eazolo.com/blog/?p=24</guid>

					<description><![CDATA[We had a query retrieving data from a linked Oracle server. We needed unique rows only. This is the original query: However, this still returned some duplicates despite using DISTINCT. As it turned out, some rows had a new line character in them. The solution:]]></description>
										<content:encoded><![CDATA[<p>We had a query retrieving data from a linked Oracle server. We needed unique rows only. This is the original query: </p>
<pre class="brush: sql; title: ; notranslate">
SELECT Column
     FROM OPENQUERY( LinkedServer, 'SELECT DISTINCT Column from TABLE;' );
</pre>
<p>However, this still returned some duplicates despite using DISTINCT. As it turned out, some rows had a new line character in them. The solution:</p>
<pre class="brush: sql; title: ; notranslate">
SELECT distinct replace(replace(Column,CHAR(13),''),CHAR(10),'')
     FROM OPENQUERY( LinkedServer, 'SELECT DISTINCT Column from TABLE;' )
</pre>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.adamzolo.com/removing-duplicate-data-sql-newline-character/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
