<?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>T-SQL Operators - Techyaz.com</title>
	<atom:link href="https://techyaz.com/tag/t-sql-operators/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>Tips, Tutorials and How-to Topics</description>
	<lastBuildDate>Sat, 24 Jul 2021 18:47:42 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.1</generator>

<image>
	<url>https://techyaz.com/wp-content/uploads/2017/11/cropped-Site-icon-150x150.png</url>
	<title>T-SQL Operators - Techyaz.com</title>
	<link></link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Learn How to use SQL Server BETWEEN Operator</title>
		<link>https://techyaz.com/sql-server/t-sql/learn-how-to-use-sql-server-between-operator/</link>
					<comments>https://techyaz.com/sql-server/t-sql/learn-how-to-use-sql-server-between-operator/#respond</comments>
		
		<dc:creator><![CDATA[Editorial Staff]]></dc:creator>
		<pubDate>Sat, 17 Jul 2021 16:06:06 +0000</pubDate>
				<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[T-SQL Operators]]></category>
		<guid isPermaLink="false">https://techyaz.com/?p=2563</guid>

					<description><![CDATA[<p>SQL BETWEEN operator is used to get values between a range of data inputs. We can use SQL BETWEEN operator with SELECT, UPDATE, INSERT and DELETE statements. Range can be defined based on text, date or numbers values. Here i&#46;&#46;&#46;</p>
<p>The post <a href="https://techyaz.com/sql-server/t-sql/learn-how-to-use-sql-server-between-operator/">Learn How to use SQL Server BETWEEN Operator</a> appeared first on <a href="https://techyaz.com">Techyaz.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>SQL BETWEEN operator is used to get values between a range of data inputs. We can use SQL BETWEEN operator with SELECT, UPDATE, INSERT and DELETE statements. Range can be defined based on text, date or numbers values. Here i will show you multiple use cases of this operator like get values between two date ranges using SQL BETWEEN operator, get details between two numbers using SQL Server operator BETWEEN etc.</p>



<h2 class="wp-block-heading">Get Values between Two Dates using SQL BETWEEN</h2>



<p>Let&#8217;s say you have a requirement to get all values between two date ranges or specific date time ranges. You can use SQL BETWEEN operator to get this done. Here is the example to get all values between date 19th March 2021 and 20th March 2021. Below example is also an use case of how to use BETWEEN operator in WHERE clause. You can remove &#8216;-&#8216; from date ranges if you want and you can directly mention date ranges as <em>&#8216;20210319&#8217; AND &#8216;20210320&#8217;.</em></p>



<p class="has-text-color" style="color:#0a43eb"><strong>SELECT name, type_desc,create_date, modify_date<br>FROM sys.objects<br>WHERE modify_date BETWEEN &#8216;2021-03-19&#8217; AND &#8216;2021-03-20&#8217;</strong></p>



<p>Here, i have fetched all modified objects on my SQL Server between given date range. Here is the output. We can see there are more than 730 rows returned. You can compare the dates in modify_date column whether output has returned correct data or not. </p>



<figure class="wp-block-image size-large is-style-default"><img fetchpriority="high" decoding="async" width="1024" height="609" src="//i0.wp.com/techyaz.com/wp-content/uploads/2021/07/1-BETWEEN-Operator-1024x609.jpg" alt="SQL BETWEEN Operator with Date ranges" class="wp-image-2580" srcset="https://techyaz.com/wp-content/uploads/2021/07/1-BETWEEN-Operator-1024x609.jpg 1024w, https://techyaz.com/wp-content/uploads/2021/07/1-BETWEEN-Operator-300x178.jpg 300w, https://techyaz.com/wp-content/uploads/2021/07/1-BETWEEN-Operator-768x456.jpg 768w, https://techyaz.com/wp-content/uploads/2021/07/1-BETWEEN-Operator.jpg 1489w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p>Now, Next requirement could be to retrieve some data between two date time ranges like you want details between specific time of dates. I will take same above query to show case this example as well. When we don&#8217;t specify the time along with date range then it defaults to 12:00 A.M. </p>



<p class="has-text-color" style="color:#0a43eb"><strong>SELECT name, type_desc,create_date, modify_date<br>FROM sys.objects<br>WHERE modify_date BETWEEN &#8216;2021-03-19 4:38:00&#8217; AND &#8216;2021-03-19 4:38:30&#8217;</strong></p>



<p>Here, i have retrieved all modified objects between 30 seconds of above given time (Time 4:38:00 to 4:38:30). Here is the output and it is showing only 35 rows.</p>



<figure class="wp-block-image size-large is-style-default"><img decoding="async" width="1024" height="608" src="//i0.wp.com/techyaz.com/wp-content/uploads/2021/07/2-BETWEEN-Operator-1024x608.jpg" alt="SQL BETWEEN Operator with two date time ranges" class="wp-image-2581" srcset="https://techyaz.com/wp-content/uploads/2021/07/2-BETWEEN-Operator-1024x608.jpg 1024w, https://techyaz.com/wp-content/uploads/2021/07/2-BETWEEN-Operator-300x178.jpg 300w, https://techyaz.com/wp-content/uploads/2021/07/2-BETWEEN-Operator-768x456.jpg 768w, https://techyaz.com/wp-content/uploads/2021/07/2-BETWEEN-Operator.jpg 1485w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p>You can use this SQL between two date ranges or datetime ranges to fetch your data. You can use this operator in INSERT, UPDATE or DELETE statements as well.</p>



<h2 class="wp-block-heading">Get Values between Two Numbers</h2>



<p>This section will explain about getting details between two number ranges. Let me take same table which we have taken for above section sys.objects. Now, i want to get all objects having object id between 25 to 30 or you can mention any range. This is just an example to showcase. These number ranges will change as per your need and nature of data. </p>



<p class="has-text-color" style="color:#0a43eb"><strong>SELECT name, object_id, type_desc, create_date<br>FROM sys.objects<br>WHERE object_id BETWEEN &#8217;25&#8217; AND &#8217;30&#8217;</strong></p>



<p>Note, above object_id is column name and not the T-SQL function. Have a look at its output in below image. We have got only rows which are assigned with these object ids.</p>



<figure class="wp-block-image size-large is-style-default"><img decoding="async" width="500" height="305" src="//i0.wp.com/techyaz.com/wp-content/uploads/2021/07/3-BETWEEN-Operator.jpg" alt="SQL BETWEEN Operator with number ranges" class="wp-image-2582" srcset="https://techyaz.com/wp-content/uploads/2021/07/3-BETWEEN-Operator.jpg 500w, https://techyaz.com/wp-content/uploads/2021/07/3-BETWEEN-Operator-300x183.jpg 300w" sizes="(max-width: 500px) 100vw, 500px" /></figure>



<h2 class="wp-block-heading">Use BETWEEN with NOT Operator</h2>



<p>Above all examples shows how to get data between two ranges but we can use NOT operator with BETWEEN to return all values which is not between specified two ranges. Let&#8217;s take above example where BETWEEN operator has returned only 5 rows based on their specified two ranges. Now if i will use NOT operator in above T-SQL statement then output will return all rows of table sys.objects except above 4 rows which will be prevented by NOT operator. Let me show you this example.</p>



<p class="has-text-color" style="color:#0a43eb"><strong>SELECT name, object_id, type_desc, create_date<br>FROM sys.objects<br>WHERE object_id NOT BETWEEN &#8217;25&#8217; AND &#8217;30&#8217;</strong></p>



<p>Have  a look at below highlighted are where objects ids between 25 to 30 are missing.</p>



<figure class="wp-block-image size-large is-style-default"><img loading="lazy" decoding="async" width="547" height="597" src="//i0.wp.com/techyaz.com/wp-content/uploads/2021/07/4-BETWEEN-Operator.jpg" alt="SQL BETWEEN Operator with NOT operator" class="wp-image-2583" srcset="https://techyaz.com/wp-content/uploads/2021/07/4-BETWEEN-Operator.jpg 547w, https://techyaz.com/wp-content/uploads/2021/07/4-BETWEEN-Operator-275x300.jpg 275w" sizes="auto, (max-width: 547px) 100vw, 547px" /></figure>



<p>Here, i have shown you few use cases of SQL Server T-SQL operator BETWEEN statement. You can go head and try this operator for your business needs. For more information on this operator, visit <a href="https://docs.microsoft.com/en-us/sql/t-sql/language-elements/between-transact-sql?view=sql-server-ver15" target="_blank" rel="noreferrer noopener">MSDN</a> article.</p>



<p class="has-vivid-red-color has-text-color"><strong><em>Read More</em></strong></p>



<ul class="wp-block-list"><li><strong><a href="https://techyaz.com/sql-server/troubleshooting/fixview-server-state-permission-was-denied-on-object-server-database-master/" target="_blank" rel="noreferrer noopener">Fix:VIEW SERVER STATE permission was denied on object ‘server’, database ‘master’</a></strong></li><li><strong><a href="https://techyaz.com/sql-server/t-sql/difference-between-union-and-union-all-operators/" target="_blank" rel="noreferrer noopener">Difference between UNION and UNION ALL T-SQL Operators</a></strong></li><li><a href="https://techyaz.com/interview-questions/sql-server-interview-questions/sql-server-interview-questions-answers-indexes/" target="_blank" rel="noreferrer noopener"><strong>SQL Server Interview Questions &amp; Answers on Indexes</strong></a></li></ul>



<p></p>
<p>The post <a href="https://techyaz.com/sql-server/t-sql/learn-how-to-use-sql-server-between-operator/">Learn How to use SQL Server BETWEEN Operator</a> appeared first on <a href="https://techyaz.com">Techyaz.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://techyaz.com/sql-server/t-sql/learn-how-to-use-sql-server-between-operator/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Difference between UNION and UNION ALL T-SQL Operators</title>
		<link>https://techyaz.com/sql-server/t-sql/difference-between-union-and-union-all-operators/</link>
					<comments>https://techyaz.com/sql-server/t-sql/difference-between-union-and-union-all-operators/#respond</comments>
		
		<dc:creator><![CDATA[Manvendra Deo Singh]]></dc:creator>
		<pubDate>Wed, 29 Nov 2017 09:55:36 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[T-SQL Operators]]></category>
		<category><![CDATA[union]]></category>
		<guid isPermaLink="false">http://techyaz.com/?p=1269</guid>

					<description><![CDATA[<p>If you want to combine output from multiple tables or queries into one result set, you can do this using an operator known as UNION. A UNION is used to extract rows based on some conditions specified whereas UNION ALL&#46;&#46;&#46;</p>
<p>The post <a href="https://techyaz.com/sql-server/t-sql/difference-between-union-and-union-all-operators/">Difference between UNION and UNION ALL T-SQL Operators</a> appeared first on <a href="https://techyaz.com">Techyaz.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>If you want to combine output from multiple tables or queries into one result set, you can do this using an operator known as UNION. A UNION is used to extract rows based on some conditions specified whereas UNION ALL will extract all rows from both tables. Here, i will discuss difference between UNION and UNION ALL operators ( UNION vs UNION ALL ) in this article along with their use cases.</p>
<h3><span style="color: #333399;">UNION</span></h3>
<p>The UNION operator allows us to combine the results of two or more SELECT statements into a single result set and removes duplicate rows from that result set. Basically, it is performing a DISTINCT operation across all columns in the result set. The UNION operator is different from using joins that combine columns from two tables.</p>
<p>The following are basic rules for combining the result sets of two queries by using UNION:</p>
<ul>
<li>The number and the order of the columns must be the same in all queries.</li>
<li>The data types must be compatible.</li>
<li>Corresponding result set columns in the individual statements that are being combined with UNION must occur in the same order, because UNION compares the columns one-to-one in the order given in the individual queries.</li>
<li>The column names in the result from UNION are taken from the first individual query in the UNION statement.</li>
</ul>
<p>Below example shows how can you use UNION to combine multiple outputs into one result set.</p>
<pre><strong><span style="color: #0000ff;">SELECT * FROM Table1
<span style="color: #008000;">UNION</span>
SELECT * FROM Table2
<span style="color: #008000;">UNION</span>
SELECT * FROM Table3
</span></strong></pre>
<p>If tables would not have same structure or same data types, you will get below error while using UNION:</p>
<p><img loading="lazy" decoding="async" class="aligncenter wp-image-1272 size-full" src="http://techyaz.com/wp-content/uploads/2017/11/Union-Error.jpg" alt="error 205" width="799" height="157" srcset="https://techyaz.com/wp-content/uploads/2017/11/Union-Error.jpg 799w, https://techyaz.com/wp-content/uploads/2017/11/Union-Error-300x59.jpg 300w, https://techyaz.com/wp-content/uploads/2017/11/Union-Error-768x151.jpg 768w" sizes="auto, (max-width: 799px) 100vw, 799px" /></p>
<div>
<p><em><span style="color: #ff0000;">Msg 205, Level 16, State 1, Line 1</span></em><br />
<em><span style="color: #ff0000;"> All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists.</span></em></p>
</div>
<h3><span style="color: #333399;">UNION ALL</span></h3>
<p>UNION ALL operator also works like UNION operator that is used to combine multiple result sets into one result set but duplicate rows will not be removed from the result set using UNION ALL unlike the result set of UNION operator that gives distinct rows.</p>
<pre><span style="color: #0000ff;"><strong>SELECT * FROM Table1
<span style="color: #008000;">UNION ALL</span>
SELECT * FROM Table2
<span style="color: #008000;">UNION ALL</span>
SELECT * FROM Table3
</strong></span></pre>
<h3><span style="color: #333399;">Difference Between UNION and UNION ALL ( UNION vs UNION ALL )</span></h3>
<p>Please find important points if we compare the difference between UNION vs UNION ALL result sets and their uses.</p>
<ul>
<li>The UNION operator removes duplicate rows from the result set and gives us distinct rows whereas UNION ALL operator does not remove duplicate rows and give all outputs in the result set.</li>
<li>UNION ALL operator works faster than UNION operator because it does not remove duplicate rows. UNION operator performs distinct sort to remove duplicate rows so its performance is little slower than UNION ALL.</li>
<li>Operator UNION ALL works with all data type columns whereas UNION operator does not work with a column that has a TEXT data type and it will throw error during execution.</li>
</ul>
<h3><span style="color: #333399;">Guidelines to use UNION  and UNION ALL</span></h3>
<p>I have discussed difference between UNION and UNION ALL in above section. Here i will explain below guidelines you should consider while using UNION and UNION ALL operators with other T-SQL statements. Below rules are also apply to EXCEPT and INTERSECT operator:</p>
<ul>
<li>The first query can contain an INTO clause that creates a table to hold the final result set. Only the first query can use an INTO clause. If the INTO clause appears anywhere else, SQL Server displays an error message.</li>
<li>ORDER BY is allowed only at the end of the statement. It cannot be used within the individual queries that make up the statement.</li>
<li>GROUP BY and HAVING clauses can be used only within individual queries; they cannot be used to affect the final result set.</li>
<li>UNION, EXCEPT and INTERSECT can be used within an INSERT statement.</li>
<li>The FOR BROWSE clause cannot be used in statements that involve the UNION, EXCEPT and INTERSECT operators.</li>
</ul>
<p><em><span style="color: #800000;"><strong>Related Articles:</strong></span></em></p>
<ul>
<li><strong><a href="https://techyaz.com/sql-server/how-to-protect-my-stored-procedure-code/" target="_blank" rel="noopener">How to Protect Stored Procedure Code?</a></strong></li>
<li><strong><a href="https://techyaz.com/sql-server/create-logon-trigger-restrict-sysadmin-logins-connect-sql-server-given-time-interval/" target="_blank" rel="noopener">Create a Logon Trigger to Restrict sysadmin logins to Connect to SQL Server during a given time Interval</a></strong></li>
<li><strong><a href="https://techyaz.com/sql-server/get-row-counts-tables-database/" target="_blank" rel="noopener">How to get Total Row Count of all tables of a database</a></strong></li>
</ul>
<p>I hope you like this article. Please follow our <a href="https://www.facebook.com/Techyaz/">Facebook</a> page and <a href="https://twitter.com/Tech_yaz">Twitter </a>handle to get latest updates.</p>
<p>The post <a href="https://techyaz.com/sql-server/t-sql/difference-between-union-and-union-all-operators/">Difference between UNION and UNION ALL T-SQL Operators</a> appeared first on <a href="https://techyaz.com">Techyaz.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://techyaz.com/sql-server/t-sql/difference-between-union-and-union-all-operators/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
