<?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>MySQL - TheCodeBuzz</title>
	<atom:link href="https://thecodebuzz.com/category/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>https://thecodebuzz.com</link>
	<description>Best Practices for Software Development</description>
	<lastBuildDate>Thu, 15 Feb 2024 00:38:17 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://thecodebuzz.com/wp-content/uploads/2022/11/cropped-android-chrome-512x512-1-1-51x51.jpg</url>
	<title>MySQL - TheCodeBuzz</title>
	<link>https://thecodebuzz.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>MySQL String Contains: LIKE, LOCATE and FULLTEXT Examples</title>
		<link>https://thecodebuzz.com/mysql-query-string-contains-examples/</link>
					<comments>https://thecodebuzz.com/mysql-query-string-contains-examples/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Sat, 03 Feb 2024 03:41:04 +0000</pubDate>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[MySQL Query String contains]]></category>
		<guid isPermaLink="false">https://www.thecodebuzz.com/?p=30022</guid>

					<description><![CDATA[<p>MySQL has no CONTAINS function. Use LIKE '%word%', multiple words with OR or AND, case-sensitive matches, LOCATE and FULLTEXT, with real MySQL output.</p>
<p>The post <a href="https://thecodebuzz.com/mysql-query-string-contains-examples/">MySQL String Contains: LIKE, LOCATE and FULLTEXT Examples</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></description>
										<content:encoded><![CDATA[<p class="wp-block-paragraph">For a <strong>MySQL select where a string contains</strong> a word, there is no <code>CONTAINS</code> function. The answer is <code>LIKE</code> with a percent sign on both sides: <code>WHERE column LIKE '%word%'</code>. Most mistakes come after that first query, when you add a second word, need the match to ignore or respect upper case, or look for a literal <code>%</code>. This article covers each case with a small table you can create yourself, and every result shown comes from a real MySQL server.</p>



<figure class="wp-block-image size-large"><img decoding="async" src="https://thecodebuzz.com/wp-content/uploads/2026/11/mysql-contains-forms-1.gif" alt="Animated table of what you want and which MySQL query to use: contains a word uses LIKE with percent signs, contains A or B uses two LIKE conditions with OR, contains A and B uses AND, a case-sensitive match uses LIKE BINARY, and a large table uses FULLTEXT MATCH AGAINST. It is an illustration, not a screenshot."/><figcaption>Which query form to use (illustration, not a screenshot).</figcaption></figure>



<div class="wp-block-aioseo-table-of-contents"><ul><li><a href="#aioseo-mysql-query-string-contains-with-examples">MySQL where contains: query string contains with examples</a></li><li><a href="#aioseo-mysql-string-contains-multiple-words">MySQL string contains multiple words</a></li><li><a href="#case-sensitive-and-case-insensitive-contains">Case-sensitive and case-insensitive contains</a></li><li><a href="#search-for-a-literal-percent-or-underscore">Search for a literal % or underscore</a></li><li><a href="#locate-instr-and-fulltext">LOCATE, INSTR and FULLTEXT</a></li><li><a href="#why-a-leading-percent-is-slow">Why a leading % is slow</a></li><li><a href="#what-was-tested-and-what-was-not">What was tested and what was not</a></li><li><a href="#summary">Summary</a></li><li><a href="#faq">MySQL string contains FAQ</a></li></ul></div>



<h2 class="wp-block-heading" id="aioseo-mysql-query-string-contains-with-examples">MySQL where contains: query string contains with examples</h2>



<p class="wp-block-paragraph">Every example below uses this table, which has the same columns the first version of this article used from a WordPress posts table. Run it in the <code>mysql</code> client to follow along:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: sql; title: ; notranslate">
CREATE TABLE posts (
  id INT PRIMARY KEY AUTO_INCREMENT,
  post_name VARCHAR(100),
  post_title VARCHAR(200),
  post_content TEXT,
  KEY idx_title (post_title),
  FULLTEXT KEY ft_content (post_content)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

INSERT INTO posts (post_name, post_title, post_content) VALUES
(&#039;send-email-mailkit&#039;, &#039;Send email with MailKit&#039;, &#039;Use MailKit to send mail over SMTP from .NET.&#039;),
(&#039;smtp-basics&#039;, &#039;SMTP basics&#039;, &#039;SMTP is the protocol used to send mail.&#039;),
(&#039;mailkit-imap&#039;, &#039;Read mail with IMAP&#039;, &#039;MAILKIT can also read mail over IMAP.&#039;),
(&#039;discount&#039;, &#039;Sales report&#039;, &#039;We gave a 50% discount this quarter.&#039;),
(&#039;snake&#039;, &#039;Naming style&#039;, &#039;Use snake_case for column names.&#039;),
(&#039;plain&#039;, &#039;Plain post&#039;, &#039;Nothing relevant here.&#039;);
</pre></div>


<h3 class="wp-block-heading" id="aioseo-command-pattern">Query pattern</h3>



<p class="wp-block-paragraph">The percent sign matches any number of characters, including none. Put it on both sides of the word to match it anywhere in the text. This is the pattern, with your own column, table and word:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: sql; title: ; notranslate">
SELECT column_a
FROM table_o
WHERE column_b LIKE &#039;%search_word%&#039;;
</pre></div>


<p class="wp-block-paragraph">Note the old version of this pattern had a stray comma after <code>COLUMN-A</code>, which is a syntax error. The query above has none. The <code>LIKE</code> operator is described in the <a href="https://docs.oracle.com/cd/E17952_01/mysql-8.0-en/string-comparison-functions.html" target="_blank" rel="noopener">MySQL 8.0 Reference Manual: String Comparison Functions</a>.</p>



<h3 class="wp-block-heading" id="aioseo-example">Example</h3>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: sql; title: ; notranslate">
SELECT post_name, post_title
FROM posts
WHERE post_content LIKE &#039;%mailkit%&#039;;
</pre></div>


<p class="wp-block-paragraph">Result:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
+--------------------+-------------------------+
| post_name          | post_title              |
+--------------------+-------------------------+
| send-email-mailkit | Send email with MailKit |
| mailkit-imap       | Read mail with IMAP     |
+--------------------+-------------------------+
</pre></div>


<p class="wp-block-paragraph">Both rows match, including <code>MAILKIT</code> in capitals. That is not an accident; see the case section below.</p>



<h3 class="wp-block-heading">Rows that do not contain a word</h3>



<p class="wp-block-paragraph">Use <code>NOT LIKE</code>. On the sample table it returned <code>smtp-basics</code>, <code>discount</code>, <code>snake</code> and <code>plain</code>:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: sql; title: ; notranslate">
SELECT post_name FROM posts WHERE post_content NOT LIKE &#039;%mailkit%&#039;;
</pre></div>


<p class="wp-block-paragraph">Be aware that rows where the column is <code>NULL</code> do not match <code>NOT LIKE</code> either, because the comparison result is NULL, not true. Add <code>OR post_content IS NULL</code> if you want them.</p>



<h2 class="wp-block-heading" id="aioseo-mysql-string-contains-multiple-words">MySQL string contains multiple words</h2>



<p class="wp-block-paragraph">Write the full <code>LIKE</code> condition for every word and join them with <code>OR</code> (either word) or <code>AND</code> (both words):</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: sql; title: ; notranslate">
-- contains mailkit OR smtp
SELECT post_name FROM posts
WHERE post_content LIKE &#039;%mailkit%&#039; OR post_content LIKE &#039;%smtp%&#039;;

-- contains mailkit AND smtp
SELECT post_name FROM posts
WHERE post_content LIKE &#039;%mailkit%&#039; AND post_content LIKE &#039;%smtp%&#039;;
</pre></div>


<p class="wp-block-paragraph">On the sample table the first query returned <code>send-email-mailkit</code>, <code>smtp-basics</code> and <code>mailkit-imap</code>. The second returned only <code>send-email-mailkit</code>, the one row that has both words.</p>



<h3 class="wp-block-heading">The old example was wrong</h3>



<p class="wp-block-paragraph">The first version of this article wrote the second word without its own column and <code>LIKE</code>, as <code>LIKE '%mailkit%' OR 'smtp'</code>. MySQL accepts that without an error, which hides the mistake. It treats <code>'smtp'</code> as a number, which becomes 0, so the second half is always false. I ran it:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: sql; title: ; notranslate">
SELECT post_name FROM posts WHERE post_content LIKE &#039;%mailkit%&#039; OR &#039;smtp&#039;;
-- send-email-mailkit, mailkit-imap      (smtp-basics is missing)
-- Warning 1292: Truncated incorrect DOUBLE value: &#039;smtp&#039;
</pre></div>


<p class="wp-block-paragraph">The row <code>smtp-basics</code> is missing, so the query did not search for smtp at all. Always repeat the column and <code>LIKE</code> for each word. For many words, a regular expression is shorter:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: sql; title: ; notranslate">
SELECT post_name FROM posts WHERE post_content REGEXP &#039;mailkit|smtp&#039;;
</pre></div>


<p class="wp-block-paragraph">That returned the same three rows as the <code>OR</code> query. The syntax is in the <a href="https://docs.oracle.com/cd/E17952_01/mysql-8.0-en/regexp.html" target="_blank" rel="noopener">MySQL 8.0 Reference Manual: Regular Expressions</a>.</p>



<h2 class="wp-block-heading" id="case-sensitive-and-case-insensitive-contains">Case-sensitive and case-insensitive contains</h2>



<p class="wp-block-paragraph"><code>LIKE</code> follows the collation of the column. The table above uses the MySQL default for utf8mb4, <code>utf8mb4_0900_ai_ci</code>, which ignores case and accents. That is why <code>LIKE '%MAILKIT%'</code> found both rows. To require the exact case, use <code>LIKE BINARY</code> or a case-sensitive collation:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: sql; title: ; notranslate">
SELECT post_name FROM posts WHERE post_content LIKE BINARY &#039;%MailKit%&#039;;
-- send-email-mailkit

SELECT post_name FROM posts WHERE post_content COLLATE utf8mb4_0900_as_cs LIKE &#039;%MAILKIT%&#039;;
-- mailkit-imap
</pre></div>


<p class="wp-block-paragraph">The first matched only the row written <code>MailKit</code>. The second, searching for <code>MAILKIT</code>, matched only the row written in capitals. If your column or database uses a different collation, the default behavior changes with it. The comparison rules are in the <a href="https://docs.oracle.com/cd/E17952_01/mysql-8.0-en/string-comparison-functions.html" target="_blank" rel="noopener">MySQL 8.0 Reference Manual: String Comparison Functions</a>.</p>



<h2 class="wp-block-heading" id="search-for-a-literal-percent-or-underscore">Search for a literal % or underscore</h2>



<p class="wp-block-paragraph">In a <code>LIKE</code> pattern, <code>%</code> means any text and <code>_</code> means exactly one character. To find the characters themselves, escape them with a backslash, or choose your own escape character:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: sql; title: ; notranslate">
SELECT post_name FROM posts WHERE post_content LIKE &#039;%50\%%&#039;;             -- discount
SELECT post_name FROM posts WHERE post_content LIKE &#039;%snake\_case%&#039;;      -- snake
SELECT post_name FROM posts WHERE post_content LIKE &#039;%snake!_case%&#039; ESCAPE &#039;!&#039;;   -- snake
</pre></div>


<p class="wp-block-paragraph">Each returned its single expected row. An unescaped <code>snake_case</code> also matched here, but only because the underscore stands for any one character and the text happens to contain an underscore, so it would also match <code>snakeXcase</code>.</p>



<h2 class="wp-block-heading" id="locate-instr-and-fulltext">LOCATE, INSTR and FULLTEXT</h2>



<p class="wp-block-paragraph"><code>LOCATE</code> and <code>INSTR</code> return the position of a word, so <code>&gt; 0</code> means contains. They need no escaping, which makes them handy when the search text comes from a variable or contains wildcards:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: sql; title: ; notranslate">
SELECT post_name FROM posts WHERE LOCATE(&#039;mailkit&#039;, post_content) &gt; 0;
SELECT post_name FROM posts WHERE INSTR(post_content, &#039;smtp&#039;) &gt; 0;
</pre></div>


<p class="wp-block-paragraph">The first returned the two mailkit rows, the second <code>send-email-mailkit</code> and <code>smtp-basics</code>. For word search in large text columns, a <code>FULLTEXT</code> index with <code>MATCH ... AGAINST</code> is built for the job (<a href="https://docs.oracle.com/cd/E17952_01/mysql-8.0-en/fulltext-search.html" target="_blank" rel="noopener">MySQL 8.0 Reference Manual: Full-Text Search Functions</a>):</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: sql; title: ; notranslate">
SELECT post_name FROM posts
WHERE MATCH(post_content) AGAINST (&#039;mailkit&#039; IN NATURAL LANGUAGE MODE);

SELECT post_name FROM posts
WHERE MATCH(post_content) AGAINST (&#039;+mailkit +smtp&#039; IN BOOLEAN MODE);
</pre></div>


<p class="wp-block-paragraph">The natural language query returned both mailkit rows, and the boolean query with a plus sign on each word (both words required) returned only <code>send-email-mailkit</code>. FULLTEXT matches whole words, not parts of words, and ignores very short and very common words by default, so it is not a drop-in replacement for <code>LIKE '%part%'</code>.</p>



<h3 class="wp-block-heading">Searching with a variable</h3>



<p class="wp-block-paragraph">Build the pattern with <code>CONCAT</code> and pass the search text as a parameter, never by gluing it into the SQL string from user input:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: sql; title: ; notranslate">
SET @term = &#039;smtp&#039;;
PREPARE s FROM &#039;SELECT post_name FROM posts WHERE post_content LIKE CONCAT(&#039;&#039;%&#039;&#039;, ?, &#039;&#039;%&#039;&#039;)&#039;;
EXECUTE s USING @term;
-- send-email-mailkit, smtp-basics
</pre></div>


<p class="wp-block-paragraph">In application code, use your driver&#8217;s parameters in the same way. For connecting from .NET, see <a href="https://thecodebuzz.com/use-net-c-to-connect-database-for-mysql/">use .NET C# to connect to a MySQL database</a>.</p>



<h2 class="wp-block-heading" id="why-a-leading-percent-is-slow">Why a leading % is slow</h2>



<p class="wp-block-paragraph">A pattern that starts with a wildcard cannot use a normal B-tree index. I ran <code>EXPLAIN</code> on the indexed <code>post_title</code> column:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: sql; title: ; notranslate">
EXPLAIN SELECT post_name FROM posts WHERE post_title LIKE &#039;SMTP%&#039;;
-- Index range scan on posts using idx_title

EXPLAIN SELECT post_name FROM posts WHERE post_title LIKE &#039;%SMTP%&#039;;
-- Table scan on posts
</pre></div>


<p class="wp-block-paragraph">The prefix search used the index. The contains search scanned the table. On six rows that does not matter, but on millions it does. If you search the start of a value, drop the leading <code>%</code>. If you need contains on a large table, use FULLTEXT or a search engine. More production guidance is in <a href="https://thecodebuzz.com/mysql-best-practices-production/">MySQL best practices for production</a>. To page through the matches, see <a href="https://thecodebuzz.com/pagination-in-sql-guidelines-examples/">pagination in SQL</a>.</p>



<h2 class="wp-block-heading" id="what-was-tested-and-what-was-not">What was tested and what was not</h2>



<ul class="wp-block-list">
<li><strong>Run on macOS against a local MySQL server (version 26.10.0 from the Homebrew mysql package), default utf8mb4_0900_ai_ci collation:</strong> every query above on the six-row sample table. The result lines are real output. The reference links point to the MySQL 8.0 manual hosted by Oracle, since the MySQL site rejects automated link checks.</li>

<li><strong>Not tested:</strong> MariaDB, MySQL 5.7, other collations, large tables and timings (the <code>EXPLAIN</code> output only shows which access path was chosen), and FULLTEXT stopword and minimum word length settings.</li>
</ul>



<h2 class="wp-block-heading" id="summary">Summary</h2>



<ul class="wp-block-list">
<li>Contains is <code>column LIKE '%word%'</code>. Repeat the full condition for each word and join with <code>OR</code> or <code>AND</code>.</li>

<li>Case follows the collation. Use <code>LIKE BINARY</code> for exact case, and escape <code>%</code> and <code>_</code> when you mean the characters.</li>

<li>A leading <code>%</code> scans the table. Use FULLTEXT for large text columns.</li>
</ul>



<div class="wp-block-group has-background" style="border-radius:8px;background-color:#b6d9ac;padding-top:20px;padding-right:24px;padding-bottom:20px;padding-left:24px"><div class="wp-block-group__inner-container is-layout-constrained wp-container-core-group-is-layout-0d2d72cd wp-block-group-is-layout-constrained">
<p class="wp-block-paragraph" style="font-size:20px"><strong>Found this useful?</strong></p>



<p class="wp-block-paragraph">Bookmark this page and share it with your team. Get new .NET, cloud and database tutorials in your inbox.</p>



<div class="wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex">
<div class="wp-block-button"><a class="wp-block-button__link wp-element-button" href="https://www.thecodebuzz.com/subscription/" target="_blank" rel="noopener">Subscribe to TheCodeBuzz</a></div>
</div>
</div></div>



<h2 class="wp-block-heading" id="faq">MySQL string contains FAQ</h2>



<h3 class="wp-block-heading">How do I write a MySQL SELECT where a column contains a string?</h3>



<p class="wp-block-paragraph">There is no CONTAINS function. Use <code>WHERE column LIKE '%word%'</code>, or LIKE with percent signs, LOCATE or INSTR for a position test, REGEXP for patterns, or MATCH &#8230; AGAINST with a FULLTEXT index for word search.</p>



<h3 class="wp-block-heading">Is MySQL LIKE case-sensitive?</h3>



<p class="wp-block-paragraph">It depends on the collation. With the default utf8mb4_0900_ai_ci it ignores case. Use LIKE BINARY or a case-sensitive collation such as utf8mb4_0900_as_cs to match exact case.</p>



<h3 class="wp-block-heading">How do I check that a column contains several words?</h3>



<p class="wp-block-paragraph">Write one LIKE condition per word and join them with AND for all words or OR for any word. Writing LIKE &#8216;%a%&#8217; OR &#8216;b&#8217; is a mistake: the second operand is not compared with the column.</p>



<p class="wp-block-paragraph">Which query did you need? Tell us in the comments if a case is missing.</p>



<script type="application/ld+json">{"@context": "https://schema.org", "@type": "FAQPage", "mainEntity": [{"@type": "Question", "name": "How do I write a MySQL SELECT where a column contains a string?", "acceptedAnswer": {"@type": "Answer", "text": "There is no CONTAINS function. Use WHERE column LIKE '%word%', or LIKE with percent signs, LOCATE or INSTR for a position test, REGEXP for patterns, or MATCH ... AGAINST with a FULLTEXT index for word search."}}, {"@type": "Question", "name": "Is MySQL LIKE case-sensitive?", "acceptedAnswer": {"@type": "Answer", "text": "It depends on the collation. With the default utf8mb4_0900_ai_ci it ignores case. Use LIKE BINARY or a case-sensitive collation such as utf8mb4_0900_as_cs to match exact case."}}, {"@type": "Question", "name": "How do I check that a column contains several words?", "acceptedAnswer": {"@type": "Answer", "text": "Write one LIKE condition per word and join them with AND for all words or OR for any word. Writing LIKE '%a%' OR 'b' is a mistake: the second operand is not compared with the column."}}]}</script><p>The post <a href="https://thecodebuzz.com/mysql-query-string-contains-examples/">MySQL String Contains: LIKE, LOCATE and FULLTEXT Examples</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://thecodebuzz.com/mysql-query-string-contains-examples/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>SQL &#8211; Time or Date range query with examples</title>
		<link>https://thecodebuzz.com/sql-time-or-date-range-query-with-examples/</link>
					<comments>https://thecodebuzz.com/sql-time-or-date-range-query-with-examples/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Sat, 11 Nov 2023 22:54:58 +0000</pubDate>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[SQL]]></category>
		<guid isPermaLink="false">https://www.thecodebuzz.com/?p=28837</guid>

					<description><![CDATA[<p>SQL &#8211; Time or Date range query with examples Today in this article, we will cover SQL &#8211; Time or Date range query with examples. We will see how to get records with greater than date or less than date/time or in between 2 given dates. We will see SQL queries for greater than OR [&#8230;]</p>
<p>The post <a href="https://thecodebuzz.com/sql-time-or-date-range-query-with-examples/">SQL – Time or Date range query with examples</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></description>
										<content:encoded><![CDATA[<h1 class="wp-block-heading"><strong>SQL &#8211; Time or Date range query with examples</strong></h1>



<figure class="wp-block-image size-full is-resized"><img fetchpriority="high" decoding="async" width="710" height="519" src="https://www.thecodebuzz.com/wp-content/uploads/2023/11/specific-time-range-query-in-sql-server.jpg" alt="sql-server-datetime-range-query" class="wp-image-28839" style="aspect-ratio:1.3680154142581888;width:738px;height:auto" srcset="https://thecodebuzz.com/wp-content/uploads/2023/11/specific-time-range-query-in-sql-server.jpg 710w, https://thecodebuzz.com/wp-content/uploads/2023/11/specific-time-range-query-in-sql-server-300x219.jpg 300w" sizes="(max-width: 710px) 100vw, 710px" /></figure>



<p class="wp-block-paragraph">Today in this article, we will cover SQL &#8211; Time or Date range query with examples.</p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">We will see how to get records with greater than date or less than date/time or in between 2 given dates.</p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">We will see SQL queries for greater than OR  Less than or equal to dates or specific time ranges.</p>



<p class="wp-block-paragraph"></p>



<div class="wp-block-aioseo-table-of-contents"><ul><li><a href="#aioseo-sql-query-less-than-specific-date-range">SQL Query &#8211; Less Than Specific Date Range</a><ul><li><a href="#aioseo-command">Command</a></li><li><a href="#aioseo-example">Example</a></li></ul></li><li><a href="#aioseo-sql-query-greater-than-specific-date-range">SQL Query &#8211; Greater Than Specific Date Range</a><ul><li><a href="#aioseo-command">Command</a></li><li><a href="#aioseo-example">Example</a></li></ul></li><li><a href="#aioseo-sql-query-greater-than-and-less-than-specific-date-range">SQL Query &#8211; Greater Than and Less Than Specific Date Range</a><ul><li><a href="#aioseo-command">Command</a></li><li><a href="#aioseo-example">Example</a></li></ul></li></ul></div>



<p class="wp-block-paragraph"></p>



<h2 class="wp-block-heading" id="aioseo-sql-query-less-than-specific-date-range">SQL Query &#8211; Less Than Specific Date Range</h2>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">If you need to retrieve records where the date is greater than a specific date, please use the below query command.</p>



<p class="wp-block-paragraph"></p>



<h3 class="wp-block-heading" id="aioseo-command">Command </h3>



<p class="wp-block-paragraph"></p>



<pre class="wp-block-preformatted">SELECT *
FROM [Table_Name]
WHERE [Data_Column_Name] &lt;  [Date]

</pre>



<p class="wp-block-paragraph">In this query:</p>



<ul class="wp-block-list">
<li><code>SELECT *</code>: Retrieves all records from the table.</li>



<li><code>FROM Table_Name</code> : Specifies the table from which to pick data.</li>



<li><code>WHERE Data_Column_Name</code> <code>&lt; </code> [Date]:  Defines the date range query to get the records where Data_Column_Name is greater than the specified date.</li>
</ul>



<p class="wp-block-paragraph"></p>



<h3 class="wp-block-heading" id="aioseo-example">Example </h3>



<pre class="wp-block-preformatted">SELECT *
FROM EMPLOYEE
WHERE DATE_JOINED &lt; '2023-11-11'
</pre>



<p class="wp-block-paragraph">The above query returns all the columns of the EMPLOYEE table where DATE_JOINED is greater than the specified date i.e &#8216;2023-11-11&#8217;</p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph"></p>



<h2 class="wp-block-heading" id="aioseo-sql-query-greater-than-specific-date-range">SQL Query &#8211; Greater Than Specific Date Range</h2>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">If you need to retrieve records where the date is greater than a specific date, please use the below query command.</p>



<p class="wp-block-paragraph"></p>



<h3 class="wp-block-heading" id="aioseo-command">Command </h3>



<p class="wp-block-paragraph"></p>



<pre class="wp-block-preformatted">SELECT *
FROM [Table_Name]
WHERE [Data_Column_Name] &gt;  [Date]
</pre>



<h3 class="wp-block-heading" id="aioseo-example">Example </h3>



<pre class="wp-block-preformatted">SELECT *
FROM EMPLOYEE
WHERE DATE_JOINED &gt;  '2023-07-15'


</pre>



<h2 class="wp-block-heading" id="aioseo-sql-query-greater-than-and-less-than-specific-date-range">SQL Query &#8211; Greater Than and Less Than Specific Date Range</h2>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">If you need to retrieve records between two dates where the date is greater than a specific date and less than a specific date, please use the below query command.</p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">We will use <strong>BETWEEN </strong>operator to get records between 2 dates.</p>



<p class="wp-block-paragraph"></p>



<h3 class="wp-block-heading" id="aioseo-command">Command </h3>



<p class="wp-block-paragraph"></p>



<pre class="wp-block-preformatted">SELECT *
FROM [Table_Name]
WHERE [Data_Column_Name] BETWEEN [Date1] AND [Date2];
</pre>



<h3 class="wp-block-heading" id="aioseo-example">Example </h3>



<p class="wp-block-paragraph"></p>



<pre class="wp-block-preformatted">SELECT *
FROM EMPLOYEE
WHERE DATE_JOINED BETWEEN '2022-01-01' AND '2023-01-01';
</pre>



<p class="wp-block-paragraph">In this query:</p>



<ul class="wp-block-list">
<li><code>SELECT *</code>: Get all columns from the specified table.</li>



<li><code>FROM EMPLOYEE</code> : Specifies the table from which to read the data</li>



<li><code>WHERE DATE_JOINED BETWEEN '2022-01-01' AND '2023-01-01'</code>: This query will return all the employees who joined between Jan 1, 2022, and Jan 1, 2023 year.</li>
</ul>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">That&#8217;s all! Happy coding!</p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">Does this help you fix your issue? </p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">Do you have any better solutions or suggestions? Please sound off your comments below.</p>



<hr>



<p class="wp-block-paragraph"></p>



<p class="has-background" style="background-color:#b6d9ac;font-size:18px"><br>Please <strong><em>bookmark </em></strong>this page and <em><strong>share </strong></em>it with your friends.                                                    Please <a href="https://www.thecodebuzz.com/subscription/" target="_blank" rel="noreferrer noopener"><em><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color"><strong>Subscribe</strong> </mark></em></a>to the blog to receive notifications on freshly published (2026) best practices and guidelines for software design and development.</p>




<br>



<hr>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph"></p><p>The post <a href="https://thecodebuzz.com/sql-time-or-date-range-query-with-examples/">SQL – Time or Date range query with examples</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://thecodebuzz.com/sql-time-or-date-range-query-with-examples/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Pagination in SQL &#8211; Guidelines with examples</title>
		<link>https://thecodebuzz.com/pagination-in-sql-guidelines-examples/</link>
					<comments>https://thecodebuzz.com/pagination-in-sql-guidelines-examples/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Mon, 12 Jun 2023 04:28:00 +0000</pubDate>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Pagination in SQL]]></category>
		<guid isPermaLink="false">https://www.thecodebuzz.com/?p=27638</guid>

					<description><![CDATA[<p>Pagination in SQL with guidelines and examples Pagination in SQL is the procedure of retrieving a subset of data results from a query in smaller, manageable chunks or pages. This is often used when dealing with large result sets to improve performance and provide a better user experience. Pagination in SQL is used to retrieve [&#8230;]</p>
<p>The post <a href="https://thecodebuzz.com/pagination-in-sql-guidelines-examples/">Pagination in SQL – Guidelines with examples</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></description>
										<content:encoded><![CDATA[<h1 class="wp-block-heading" id="aioseo-pagination-in-sql-with-guidelines-and-examples"><strong>Pagination in SQL with guidelines and examples</strong></h1>



<figure class="wp-block-image size-full is-resized"><img decoding="async" width="710" height="519" src="https://www.thecodebuzz.com/wp-content/uploads/2023/06/Pagination-in-SQL-Guidelines-with-examples.jpg" alt="Pagination in SQL " class="wp-image-27685" style="width:620px;height:453px" srcset="https://thecodebuzz.com/wp-content/uploads/2023/06/Pagination-in-SQL-Guidelines-with-examples.jpg 710w, https://thecodebuzz.com/wp-content/uploads/2023/06/Pagination-in-SQL-Guidelines-with-examples-300x219.jpg 300w" sizes="(max-width: 710px) 100vw, 710px" /></figure>



<p class="wp-block-paragraph">Pagination in SQL is the procedure of retrieving a subset of data results from a query in smaller, manageable chunks or pages. </p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">This is often used when dealing with large result sets to improve performance and provide a better user experience. </p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">Pagination in SQL is used to retrieve and display a subset of data results at a time, rather than fetching the entire result set in a single query. There are several reasons why pagination is commonly used,</p>



<p class="wp-block-paragraph"></p>



<div class="wp-block-aioseo-table-of-contents"><ul><li><a href="#aioseo-benefits-of-pagination-in-sql">Benefits of Pagination in SQL</a><ul><li><a href="#aioseo-performance-optimization">Pagination SQL &#8211;  Performance Optimization</a></li><li><a href="#aioseo-content-base-loading-in-ux-guidelines">Pagination SQL &#8211; Content-Base Loading in UX guidelines</a></li><li><a href="#aioseo-reduced-network-latency">Reduced Network Latency</a></li></ul></li><li><a href="#aioseo-better-resource-management">Better Resource Management</a></li><li><a href="#aioseo-scalability">Scalability</a><ul><li><a href="#aioseo-data-streaming-and-real-time-updates">Data streaming and Real-Time Updates</a></li></ul></li><li><a href="#aioseo-pagination-in-sql-with-examples">Pagination in SQL using &#8211; LIMIT and OFFSET</a></li><li><a href="#aioseo-pagination-in-sql-with-examples-row_number">Pagination in SQL using &#8211; ROW_NUMBER()</a></li><li><a href="#aioseo-row_number-vs-limit-offset">LIMIT/OFFSET Vs ROW_NUMBER</a></li></ul></div>



<p class="wp-block-paragraph"></p>



<h2 class="wp-block-heading" id="aioseo-benefits-of-pagination-in-sql"><strong>Benefits of Pagination in SQL </strong></h2>



<p class="wp-block-paragraph"></p>



<h3 class="wp-block-heading" id="aioseo-performance-optimization">Pagination SQL &#8211;  Performance Optimization</h3>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">Fetching all the data at once can be inefficient and resource-intensive. It could literally break your system or crash your services or System. </p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">When dealing with large result sets, by implementing pagination, you can retrieve or display a smaller chunk of data, reducing the amount of data transferred over the network and improving query performance.</p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph"><strong><em>Example</em></strong>: Most services failure generally occurs due to a huge set of data request on the server in one GO.</p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph"></p>



<h3 class="wp-block-heading" id="aioseo-content-base-loading-in-ux-guidelines">Pagination SQL &#8211; Content-Base Loading in UX guidelines </h3>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">To improve user experience, pagination allows users to navigate through the data in a more manageable and convenient manner. </p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">It provides a way to break down the results into smaller, organized pages, making it easier for users to locate and access the information they need.</p>



<p class="wp-block-paragraph"></p>



<h3 class="wp-block-heading" id="aioseo-reduced-network-latency">Reduced Network Latency</h3>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">Repeatedly fetching large data in a single query can result in increased network latency on the database, especially in distributed systems or when dealing with remote databases. </p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">However, actual network latency and data transfer time depend on the <em><strong>underlying network infrastructure and the size of the dataset being retrieved</strong></em>.</p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph"></p>



<h2 class="wp-block-heading" id="aioseo-better-resource-management">Better Resource Management</h2>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">Resources like memory GB or CPU cost you money. With pagination, you only load a portion of the result set into memory at a time, reducing the memory footprint and allowing for better resource utilization.</p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">When working with memory-constrained environments, fetching and storing small amounts of data will help the system&#8217;s performance and responsiveness.</p>



<p class="wp-block-paragraph"></p>



<h2 class="wp-block-heading" id="aioseo-scalability">Scalability</h2>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">Pagination enables applications to scale efficiently by fetching and processing data in <a href="https://www.thecodebuzz.com/mongodb-implement-batch-chunks-crud-update-operations/" target="_blank" rel="noopener" title="MongoDB – C#.NET Implement Batch or Chunk for CRUD operations">smaller chunks</a>. </p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">This approach minimizes the impact on system resources, making it easier to handle increasing user loads and larger datasets without overwhelming the database or application servers.</p>



<p class="wp-block-paragraph"></p>



<h3 class="wp-block-heading" id="aioseo-data-streaming-and-real-time-updates">Data streaming and Real-Time Updates</h3>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">Data streaming in chunks helps can in providing real-time updates with delta changes efficiently.</p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">When new <a href="https://www.thecodebuzz.com/insert-data-table-into-sql-server-database/">data is added or modified in the database,</a> pagination allows you to refresh the displayed results by fetching a new page without reloading the entire result set. This can be useful for displaying live or frequently updated data in real time.</p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">Overall, pagination in SQL provides a balance between query performance, resource utilization, and user experience. </p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">It allows for efficient data retrieval, reduces network and memory overhead, and enables users to navigate and access data in a more organized and manageable way.</p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph"></p>



<h2 class="wp-block-heading" id="aioseo-pagination-in-sql-with-examples"><strong>Pagination in SQL using &#8211; LIMIT and OFFSET</strong></h2>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">Let&#8217;s assume you have a table called <code>customers</code> with columns <code>customer_id</code>, <code>first_name</code>, and <code>last_name</code>, and you want to retrieve the customers in pages of 10 results each.</p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">Let&#8217;s assume here our Page size is 10 i.e. it can hold 10 records in each page.</p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph"><strong>Retrieve the first 10 Records</strong></p>



<p class="wp-block-paragraph"></p>



<pre class="wp-block-preformatted has-medium-font-size"><code>SELECT * 
FROM Employee
ORDER BY Employee_id
<strong>LIMIT 10 OFFSET 0;</strong>
</code></pre>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">In the above query, </p>



<p class="wp-block-paragraph"></p>



<ul class="wp-block-list">
<li><code><strong>LIMIT 10</strong></code> specifies that we want to retrieve a maximum of 10 rows, and </li>



<li><code>OFFSET 0</code> indicates that we start from the first row (0-based index). </li>



<li>This retrieves the first page of 10 customers sorted by <code>customer_id</code>.</li>
</ul>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph"><strong>Retrieve</strong> <strong>the next 10 Records</strong></p>



<p class="wp-block-paragraph">To retrieve the second page, you can use an <code>OFFSET</code> value of 10:</p>



<p class="wp-block-paragraph"></p>



<pre class="wp-block-preformatted has-medium-font-size"><code><code>SELECT * 
FROM Employee
ORDER BY Employee_id
<strong>LIMIT 10 OFFSET 10;</strong>
</code></code></pre>



<ul class="wp-block-list">
<li>The above query skips the first 10 rows and fetches the next 10 rows, giving you the second page of customers.</li>
</ul>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">You can continue this pattern to retrieve subsequent pages by incrementing the <code>OFFSET</code> value. </p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph"><strong>Retrieve the third set of 10 Records and so on</strong></p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">For example, to retrieve the third page, you would use an <code>OFFSET</code> value of 20</p>



<p class="wp-block-paragraph"></p>



<pre class="wp-block-preformatted has-medium-font-size"><code>SELECT * 
FROM Employee
ORDER BY Employee_id
<strong>LIMIT 10 OFFSET 20;</strong>

</code></pre>



<p class="wp-block-paragraph">So just by adjusting the <strong><em><code>OFFSET</code> </em></strong>value and keeping the <em><strong><code>LIMIT</code> </strong></em>value <strong>constant</strong>, you can navigate through the entire result data set and retrieve data in pages efficiently.</p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">It&#8217;s important to note that when using pagination with <code>LIMIT</code> and <code>OFFSET</code>, the performance may degrade as you go deeper into the result set. </p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">This is because the database needs to skip a certain number of rows before returning the requested page.</p>



<p class="wp-block-paragraph"></p>



<h2 class="wp-block-heading" id="aioseo-pagination-in-sql-with-examples-row_number"><strong>Pagination in SQL using &#8211; ROW_NUMBER()</strong></h2>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">This is an alternative pagination technique such as using keyset pagination or the <code><strong>ROW_NUMBER</strong>()</code> the function can provide better performance and scalability.</p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">Pagination using the <code>ROW_NUMBER()</code> function is a common technique that assigns a sequential number to each row in the result set, which is used to divide the data into pages. </p>



<pre class="wp-block-preformatted">SELECT *
FROM (SELECT *, ROW_NUMBER() OVER (ORDER BY Employee_id) AS row_num
  FROM Employess) AS numbered_rows
WHERE row_num BETWEEN 11 AND 20;</pre>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">In the above query, </p>



<ul class="wp-block-list">
<li>The inner query uses the <code>ROW_NUMBER()</code> function to assign a row number to each row in the <code>customers</code> table, ordered by the <code>customer_id</code>. </li>



<li>The result of the inner query is then aliased as <code>numbered_rows</code>.</li>



<li>The outer query filters the result to retrieve rows where the row_num falls within the desired page range. For example &#8211; we retrieve rows 11 to 20, effectively representing the second page of data with a page size of 10.</li>



<li>You can adjust the <code>BETWEEN</code> clause to specify different page ranges to retrieve the desired subsets of data. For example, <code>BETWEEN 1 AND 10</code> retrieves the first page, <code>BETWEEN 21 AND 30</code> retrieves the third page, and so on.</li>
</ul>



<p class="wp-block-paragraph"></p>



<h2 class="wp-block-heading" id="aioseo-row_number-vs-limit-offset"><strong>LIMIT/OFFSET Vs ROW_NUMBER</strong></h2>



<p class="wp-block-paragraph"></p>



<ul class="wp-block-list">
<li>Using <code>ROW_NUMBER()</code> for pagination offers more flexibility and control compared to the <code>LIMIT</code> and <code>OFFSET</code> approach. </li>



<li>It avoids the performance degradation often associated with large <code>OFFSET</code> values.</li>



<li>Allows you to efficiently navigate through the result set by directly selecting the desired page range.</li>



<li>R<code>OW_NUMBER()</code> let you combine query with other SQL clauses, such as <code>ORDER BY</code>, <code>WHERE</code>, and <code>JOIN</code>, providing more refinement and filtering the paginated results</li>
</ul>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">Additionally, it&#8217;s recommended to have appropriate <strong><em>indexes </em></strong>on the columns used in the <code>ORDER BY</code> clause to improve the efficiency of pagination queries.</p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph" style="font-size:18px">Do you have any <strong>comments or ideas or any better </strong>suggestions to share?</p>



<p class="has-small-font-size wp-block-paragraph"></p>



<p class="wp-block-paragraph" style="font-size:18px">Please sound off your comments below.</p>



<p class="has-medium-font-size wp-block-paragraph"></p>



<p class="has-medium-font-size wp-block-paragraph"><strong>Happy Coding </strong>!!</p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph"></p>



<hr>



<p class="wp-block-paragraph"></p>



<p class="has-background" style="background-color:#b6d9ac;font-size:18px"><br>Please <strong><em>bookmark </em></strong>this page and <em><strong>share </strong></em>it with your friends.                                                    Please <a href="https://www.thecodebuzz.com/subscription/" target="_blank" rel="noreferrer noopener"><em><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color"><strong>Subscribe</strong> </mark></em></a>to the blog to receive notifications on freshly published (2026) best practices and guidelines for software design and development.</p>




<br>



<hr>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph"></p><p>The post <a href="https://thecodebuzz.com/pagination-in-sql-guidelines-examples/">Pagination in SQL – Guidelines with examples</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://thecodebuzz.com/pagination-in-sql-guidelines-examples/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>T-SQL- Pad left and Pad right functions in SQL</title>
		<link>https://thecodebuzz.com/t-sql-pad-left-pad-right-functions-sql-mysql/</link>
					<comments>https://thecodebuzz.com/t-sql-pad-left-pad-right-functions-sql-mysql/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Sun, 26 Mar 2023 00:57:49 +0000</pubDate>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[lpad in sql server]]></category>
		<category><![CDATA[lpad in sql server example]]></category>
		<category><![CDATA[right padding in sql server]]></category>
		<category><![CDATA[sql pad left 0]]></category>
		<category><![CDATA[sql pad left with spaces]]></category>
		<category><![CDATA[sql padding]]></category>
		<category><![CDATA[sql server left pad zeros]]></category>
		<guid isPermaLink="false">https://www.thecodebuzz.com/?p=26350</guid>

					<description><![CDATA[<p>T-SQL- Pad left and Pad right functions in SQL Today in this article, we will see how to add Pad left and Pad right functions in SQL with examples. Learn how to add Pad left and Pad right functions in SQL with examples, including adding leading zeros to SQL fields. We have below EmployeeID which [&#8230;]</p>
<p>The post <a href="https://thecodebuzz.com/t-sql-pad-left-pad-right-functions-sql-mysql/">T-SQL- Pad left and Pad right functions in SQL</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></description>
										<content:encoded><![CDATA[<h1 class="wp-block-heading"><strong>T-SQL- Pad left and Pad right functions in SQL</strong></h1>



<figure class="wp-block-image size-full"><img decoding="async" width="710" height="373" src="https://www.thecodebuzz.com/wp-content/uploads/2023/03/T-SQL-pad-left-pad-right.jpg" alt="sql pad left with spaces,
" class="wp-image-26369" srcset="https://thecodebuzz.com/wp-content/uploads/2023/03/T-SQL-pad-left-pad-right.jpg 710w, https://thecodebuzz.com/wp-content/uploads/2023/03/T-SQL-pad-left-pad-right-300x158.jpg 300w" sizes="(max-width: 710px) 100vw, 710px" /></figure>



<p class="wp-block-paragraph">Today in this article, we will see how to add Pad left and Pad right functions in SQL with examples.</p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">Learn how to add Pad left and Pad right functions in SQL with examples, including adding leading zeros to SQL fields.</p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">We have below EmployeeID which is defined as a <a href="https://www.thecodebuzz.com/regex-mongodb-array-string-field-value-length-greater-than-less-than/">string type and length</a> of 4 characters.</p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph"><strong>Example </strong></p>



<pre class="wp-block-preformatted has-large-font-size">1

123

1234</pre>



<p class="wp-block-paragraph"></p>



<h2 class="wp-block-heading">SQL- Pad right logic to add trailing zeros to SQL Field</h2>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">The expected output is as below,</p>



<p class="wp-block-paragraph"></p>



<pre class="wp-block-preformatted has-large-font-size">1000

1200

1230

1234</pre>



<p class="wp-block-paragraph"></p>



<h3 class="wp-block-heading">SQL Query pattern</h3>



<p class="wp-block-paragraph"></p>



<pre class="wp-block-preformatted">SELECT LEFT (&lt;SQL FIELD> +'0000', 4) FROM &lt;Table Name>;</pre>



<p class="wp-block-paragraph"></p>



<h3 class="wp-block-heading">Example </h3>



<p class="wp-block-paragraph"></p>



<pre class="wp-block-preformatted">SELECT LEFT ([EmployeeID]+'0000', 4) FROM Table_2;</pre>



<p class="wp-block-paragraph"></p>



<figure class="wp-block-image size-large is-style-default"><a href="https://www.thecodebuzz.com/t-sql-pad-left-and-pad-right-functions-in-sql-mysql/" target="_blank" rel="noreferrer noopener"><img loading="lazy" decoding="async" width="1024" height="328" src="https://www.thecodebuzz.com/wp-content/uploads/2023/03/SQL-Add-trailing-zero-pad-right-mysql-1024x328.jpg" alt="SQL Pad left and right " class="wp-image-26359" srcset="https://thecodebuzz.com/wp-content/uploads/2023/03/SQL-Add-trailing-zero-pad-right-mysql-1024x328.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2023/03/SQL-Add-trailing-zero-pad-right-mysql-300x96.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2023/03/SQL-Add-trailing-zero-pad-right-mysql-768x246.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2023/03/SQL-Add-trailing-zero-pad-right-mysql-785x251.jpg 785w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a><figcaption class="wp-element-caption">#image_title</figcaption></figure>



<p class="wp-block-paragraph"></p>



<h2 class="wp-block-heading">SQL- Pad left zeros i.e to add leading zeros to SQL Field</h2>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">The expected output is as below,</p>



<p class="wp-block-paragraph"></p>



<pre class="wp-block-preformatted has-large-font-size">0001

0012

0123

1234</pre>



<p class="wp-block-paragraph"></p>



<h3 class="wp-block-heading">SQL Query pattern</h3>



<p class="wp-block-paragraph"></p>



<pre class="wp-block-preformatted">SELECT RIGHT('0000' + &lt;SQL FIELD&gt;, 4) FROM &lt;Table Name&gt;</pre>



<p class="wp-block-paragraph"></p>



<h3 class="wp-block-heading">Example </h3>



<p class="wp-block-paragraph"></p>



<pre class="wp-block-preformatted">SELECT RIGHT ('0000'+ [EmployeeID], 4) FROM Table_2;</pre>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph"></p>



<figure class="wp-block-image size-large"><a href="https://www.thecodebuzz.com/t-sql-pad-left-and-pad-right-functions-in-sql-mysql/" target="_blank" rel="noreferrer noopener"><img loading="lazy" decoding="async" width="1024" height="351" src="https://www.thecodebuzz.com/wp-content/uploads/2023/03/SQL-Add-leading-zero-pad-left-pad-right-mysql-1024x351.jpg" alt="Pad left and Pad right functions in SQL,sql pad left with spaces,
<a href=" class="wp-image-26361" srcset="https://thecodebuzz.com/wp-content/uploads/2023/03/SQL-Add-leading-zero-pad-left-pad-right-mysql-1024x351.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2023/03/SQL-Add-leading-zero-pad-left-pad-right-mysql-300x103.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2023/03/SQL-Add-leading-zero-pad-left-pad-right-mysql-768x263.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2023/03/SQL-Add-leading-zero-pad-left-pad-right-mysql-785x269.jpg 785w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a><figcaption class="wp-element-caption">#image_title</figcaption></figure>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">That&#8217;s all! Happy coding!</p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">Does this help you fix your issue? </p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">Do you have any better solutions or suggestions? Please sound off your comments below.</p>



<p class="wp-block-paragraph"></p>



<hr>



<p class="wp-block-paragraph"></p>



<p class="has-background" style="background-color:#b6d9ac;font-size:18px"><br>Please <strong><em>bookmark </em></strong>this page and <em><strong>share </strong></em>it with your friends.                                                    Please <a href="https://www.thecodebuzz.com/subscription/" target="_blank" rel="noreferrer noopener"><em><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color"><strong>Subscribe</strong> </mark></em></a>to the blog to receive notifications on freshly published (2026) best practices and guidelines for software design and development.</p>




<br>



<hr>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph"></p><p>The post <a href="https://thecodebuzz.com/t-sql-pad-left-pad-right-functions-sql-mysql/">T-SQL- Pad left and Pad right functions in SQL</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://thecodebuzz.com/t-sql-pad-left-pad-right-functions-sql-mysql/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Concatenate Texts from Multiple Rows in SQL</title>
		<link>https://thecodebuzz.com/concatenate-texts-from-multiple-rows-in-sql/</link>
					<comments>https://thecodebuzz.com/concatenate-texts-from-multiple-rows-in-sql/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Sun, 19 Mar 2023 01:01:45 +0000</pubDate>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[NoSQL]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Concatenate Texts from Multiple Rows in SQL]]></category>
		<guid isPermaLink="false">https://www.thecodebuzz.com/?p=26403</guid>

					<description><![CDATA[<p>Concatenate texts from multiple rows in SQL Today in this article, we will see how to Concatenate texts from multiple rows in SQL. It&#8217;s pretty simple to combine multiple rows in SQL and create Concatenate texts using the STRING_AGG function. Let&#8217;s take an example below. Here I have a sample table with the Bank code [&#8230;]</p>
<p>The post <a href="https://thecodebuzz.com/concatenate-texts-from-multiple-rows-in-sql/">Concatenate Texts from Multiple Rows in SQL</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></description>
										<content:encoded><![CDATA[<h1 class="wp-block-heading"><strong>Concatenate texts from multiple rows in SQL</strong></h1>



<figure class="wp-block-image size-full"><a href="https://www.thecodebuzz.com/concatenate-texts-from-multiple-rows-in-sql/" target="_blank" rel="noreferrer noopener"><img loading="lazy" decoding="async" width="710" height="373" src="https://www.thecodebuzz.com/wp-content/uploads/2023/03/convert-multiple-rows-into-one-with-comma-as-separator.jpg" alt="" class="wp-image-26410" srcset="https://thecodebuzz.com/wp-content/uploads/2023/03/convert-multiple-rows-into-one-with-comma-as-separator.jpg 710w, https://thecodebuzz.com/wp-content/uploads/2023/03/convert-multiple-rows-into-one-with-comma-as-separator-300x158.jpg 300w" sizes="auto, (max-width: 710px) 100vw, 710px" /></a></figure>



<p class="wp-block-paragraph">Today in this article, we will see how to Concatenate texts from multiple rows in SQL.</p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">It&#8217;s pretty simple to combine multiple rows in SQL and create Concatenate texts using the STRING_AGG function.</p>



<p class="wp-block-paragraph"></p>



<div class="wp-block-aioseo-table-of-contents"><ul><li><a href="#aioseo-example">Example</a></li><li><a href="#aioseo-expected-output">Expected output</a></li><li><a href="#aioseo-command">What is STRING_AGG</a></li><li><a href="#aioseo-command">Command</a></li></ul></div>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">Let&#8217;s take an example below. Here I have a sample table with the Bank code and Name of the employee matching the bank code.</p>



<p class="wp-block-paragraph"></p>



<h2 class="wp-block-heading" id="aioseo-example"><strong>Example </strong></h2>



<p class="wp-block-paragraph"></p>



<figure class="wp-block-image size-full"><a href="https://www.thecodebuzz.com/concatenate-texts-from-multiple-rows-in-sql" target="_blank" rel="noreferrer noopener"><img loading="lazy" decoding="async" width="474" height="400" src="https://www.thecodebuzz.com/wp-content/uploads/2023/03/concatenate-text-from-multiple-rows-into-a-single-text-string-in-sql.jpg" alt="Concatenate texts from multiple rows in SQL" class="wp-image-26404" srcset="https://thecodebuzz.com/wp-content/uploads/2023/03/concatenate-text-from-multiple-rows-into-a-single-text-string-in-sql.jpg 474w, https://thecodebuzz.com/wp-content/uploads/2023/03/concatenate-text-from-multiple-rows-into-a-single-text-string-in-sql-300x253.jpg 300w" sizes="auto, (max-width: 474px) 100vw, 474px" /></a></figure>



<h2 class="wp-block-heading" id="aioseo-expected-output">Expected output </h2>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">Here we will try to get out as below,</p>



<p class="wp-block-paragraph"></p>



<pre class="wp-block-preformatted has-medium-font-size">FC100   X, Y, Z
FC345   A, P, S</pre>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph"></p>



<h2 class="wp-block-heading" id="aioseo-command">What is STRING_AGG</h2>



<p class="wp-block-paragraph"></p>



<ul class="wp-block-list">
<li>The aggregate function STRING AGG concatenates every expression from every row into a single string.</li>
</ul>



<p class="wp-block-paragraph"></p>



<ul class="wp-block-list">
<li>The implicit conversion of expression values to string types is done before concatenation.</li>
</ul>



<p class="wp-block-paragraph"></p>



<ul class="wp-block-list">
<li>The implicit conversion to strings adheres to the standards for converting data types.</li>
</ul>



<p class="wp-block-paragraph"></p>



<h2 class="wp-block-heading" id="aioseo-command">Command </h2>



<p class="wp-block-paragraph"></p>



<pre class="wp-block-preformatted">STRING_AGG ( expression, separator ) [ &lt;order_clause&gt; ] &lt;order_clause&gt; ::= WITHIN GROUP ( ORDER BY &lt;order_by_expression_list&gt; [ ASC | DESC ] )</pre>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">Example:</p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph"></p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="402" src="https://www.thecodebuzz.com/wp-content/uploads/2023/03/concatenate-text-from-multiple-rows-into-single-text-string-in-sql-1024x402.jpg" alt="STRING_AGG " class="wp-image-26405" srcset="https://thecodebuzz.com/wp-content/uploads/2023/03/concatenate-text-from-multiple-rows-into-single-text-string-in-sql-1024x402.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2023/03/concatenate-text-from-multiple-rows-into-single-text-string-in-sql-300x118.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2023/03/concatenate-text-from-multiple-rows-into-single-text-string-in-sql-768x302.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2023/03/concatenate-text-from-multiple-rows-into-single-text-string-in-sql-785x308.jpg 785w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph" style="font-size:18px">Do you have any <strong>comments or ideas or any better </strong>suggestions to share?</p>



<p class="has-small-font-size wp-block-paragraph"></p>



<p class="wp-block-paragraph" style="font-size:18px">Please sound off your comments below.</p>



<p class="has-medium-font-size wp-block-paragraph"></p>



<p class="has-medium-font-size wp-block-paragraph"><strong>Happy Coding </strong>!!</p>



<p class="wp-block-paragraph"></p>



<hr>



<p class="wp-block-paragraph"></p>



<p class="has-background" style="background-color:#b6d9ac;font-size:18px"><br>Please <strong><em>bookmark </em></strong>this page and <em><strong>share </strong></em>it with your friends.                                                    Please <a href="https://www.thecodebuzz.com/subscription/" target="_blank" rel="noreferrer noopener"><em><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color"><strong>Subscribe</strong> </mark></em></a>to the blog to receive notifications on freshly published (2026) best practices and guidelines for software design and development.</p>




<br>



<hr>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph"></p><p>The post <a href="https://thecodebuzz.com/concatenate-texts-from-multiple-rows-in-sql/">Concatenate Texts from Multiple Rows in SQL</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://thecodebuzz.com/concatenate-texts-from-multiple-rows-in-sql/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
