{"id":26099,"date":"2023-05-22T06:41:00","date_gmt":"2023-05-22T04:41:00","guid":{"rendered":"https:\/\/expert-only.com\/?p=26099"},"modified":"2024-03-18T16:53:50","modified_gmt":"2024-03-18T15:53:50","slug":"comparison-operators-in-t-sql","status":"publish","type":"post","link":"https:\/\/expert-only.com\/en\/t-sql\/comparison-operators-in-t-sql\/","title":{"rendered":"How to Compare Values in T-SQL?"},"content":{"rendered":"\n<p class=\"has-text-align-left\"><strong><em>T-SQL queries with examples using the most common T-SQL logical operators in order to compare 2 values.<\/em> <\/strong><\/p>\n\n\n\n<p>Here is a list with the comparison and logic operators commonly used in T-SQL language with various examples. The examples shown here range from the simplest to the most complex.<\/p>\n\n\n\n<p>In addition, the code for creating the example tables, as well as the result of the queries, are available in each and every section of this article.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 ez-toc-wrap-center counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/expert-only.com\/en\/t-sql\/comparison-operators-in-t-sql\/#1-prepare-tables-for-our-t-sql-operators-examples\" >1. Prepare tables for our T-SQL operators examples<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/expert-only.com\/en\/t-sql\/comparison-operators-in-t-sql\/#2-the-equal-comparison-operator\" >2. The Equal comparison operator<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/expert-only.com\/en\/t-sql\/comparison-operators-in-t-sql\/#3-sql-server-comparison-operator-different-from\" >3. SQL Server comparison operator Different from<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/expert-only.com\/en\/t-sql\/comparison-operators-in-t-sql\/#4-t-sql-comparison-operators-greater-than-or-less-than\" >4. T-SQL comparison operators greater than or less than<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/expert-only.com\/en\/t-sql\/comparison-operators-in-t-sql\/#5-sql-server-greater-or-equal-to-and-less-or-equal-to-operators\" >5. SQL Server greater or equal to and less or equal to operators<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/expert-only.com\/en\/t-sql\/comparison-operators-in-t-sql\/#6-t-sql-logical-operator-and\" >6. T-SQL logical operator AND<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/expert-only.com\/en\/t-sql\/comparison-operators-in-t-sql\/#7-t-sql-or-logical-operator\" >7. T-SQL OR logical operator<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/expert-only.com\/en\/t-sql\/comparison-operators-in-t-sql\/#8-the-like-logical-operator-to-compare-strings\" >8. The LIKE Logical Operator to Compare Strings<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/expert-only.com\/en\/t-sql\/comparison-operators-in-t-sql\/#9-sql-server-contains-function-to-search-text\" >9. SQL Server CONTAINS function to search text<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"1-prepare-tables-for-our-t-sql-operators-examples\"><\/span>1. Prepare tables for our T-SQL operators examples<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The 4 tables used here are as follows, they are deliberately basic to easily illustrate the cases.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Customers<\/li>\n\n\n\n<li>Orders<\/li>\n\n\n\n<li>Products<\/li>\n\n\n\n<li>Employees<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">1.1 Create the 4 tables used in the T-SQL queries <\/h4>\n\n\n\n<p>Run this code from an SSMS window to add the 4 following tables to your database.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mssql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/* T-SQL code by https:\/\/expert-only.com *\/\n\nCREATE TABLE Customers (\n   CustomerID INT,\n   CustomerName VARCHAR(255),\n   Country VARCHAR(255)\n);\n\nCREATE TABLE Orders (\n   OrderID INT,\n   CustomerID INT,\n   OrderDate DATE\n);\n\nCREATE TABLE Products (\n   ProductID INT,\n   ProductName VARCHAR(255),\n   Price DECIMAL(10,2),\n   ProductWeight DECIMAL(10,2)\n);\n\nCREATE TABLE Employees (\n   EmployeeID INT,\n   EmployeeName VARCHAR(255),\n   Age INT,\n   Department VARCHAR(255)\n);\n<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">1.2 Insert a few lines of sample data into each table<\/h4>\n\n\n\n<p>Copy, paste and run these SQL lines to insert a few rows into each of the 4 tables, still using the <a href=\"https:\/\/expert-only.com\/en\/ssms\/check-if-object-exists-with-ssms\/\"><strong>SSMS software<\/strong><\/a>.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mssql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/* T-SQL code by https:\/\/expert-only.com *\/\n\nINSERT INTO Customers (CustomerID, CustomerName, Country) \nVALUES\n   (1, 'John Smith', 'USA'),\n   (2, 'Jane Doe', 'Canada'),\n   (3, 'Bob Johnson', 'USA');\n   \nINSERT INTO Orders (OrderID, CustomerID, OrderDate)\nVALUES (1, 1, '2022-01-01'),\n   (2, 1, '2022-02-01'),\n   (3, 2, '2022-03-01'),\n   (4, 3, '2022-04-01');\n   \nINSERT INTO Products (ProductID, ProductName, Price, ProductWeight)\nVALUES (1, 'Computer', 1000, 20),\n   (2, 'Printer', 500, 10),\n   (3, 'Monitor', 300, 15);\n   \nINSERT INTO Employees (EmployeeID, EmployeeName, Age, Department)\nVALUES (1, 'John Smith', 35, 'IT'),\n   (2, 'Jane Doe', 30, 'Sales'),\n   (3, 'Bob Johnson', 40, 'IT');\n   <\/pre>\n\n\n\n<p>Once the tables are created and populated with some example data, let&#8217;s move on to practice with concrete use cases.<\/p>\n\n\n\n<p>The list of T-SQL <a href=\"https:\/\/www.sciencedirect.com\/topics\/engineering\/logical-operator\" target=\"_blank\" rel=\"noreferrer noopener\">comparison operators<\/a> covered in this tutorial is as follows:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Equal to: =<\/li>\n\n\n\n<li>Greater than: &gt;<\/li>\n\n\n\n<li>Greater than or equal to: &gt;=<\/li>\n\n\n\n<li>Less than: &lt;<\/li>\n\n\n\n<li>Less than or equal to: &lt;=<\/li>\n\n\n\n<li>And: AND<\/li>\n\n\n\n<li>Or: OR<\/li>\n\n\n\n<li>Like: LIKE<\/li>\n\n\n\n<li>Contains: CONTAINS<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"2-the-equal-comparison-operator\"><\/span>2. The Equal comparison operator<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Of course one of the most used operator to compare values in T-SQL is the equal symbol. The comparison operator &#8220;=&#8221;, equal in English, is used to compare if two values are equal. It returns true or false.<\/p>\n\n\n\n<p><strong>Simple example<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mssql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/* T-SQL code by https:\/\/expert-only.com *\/\n\nSELECT *\nFROM   Customers\nWHERE  CustomerID = 1;\n<\/pre>\n\n\n\n<p>The result is a single row with client number 1, John Smith.<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><thead><tr><th>ClientID<\/th><th>Name<\/th><th>Country<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>John Smith<\/td><td>USA<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"600\" height=\"320\" src=\"https:\/\/expert-only.com\/wp-content\/uploads\/2023\/05\/compare-values-in-t-sql-equal-operator.jpg\" alt=\"Use the Equal Symbol to Compare Values in T-SQL\" class=\"wp-image-30777\" srcset=\"https:\/\/expert-only.com\/wp-content\/uploads\/2023\/05\/compare-values-in-t-sql-equal-operator.jpg 600w, https:\/\/expert-only.com\/wp-content\/uploads\/2023\/05\/compare-values-in-t-sql-equal-operator-300x160.jpg 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><figcaption class=\"wp-element-caption\"><em>Use the Equal Symbol to Compare Values in T-SQL<\/em><\/figcaption><\/figure><\/div>\n\n\n<p><strong>Complex example<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mssql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/* T-SQL code by https:\/\/expert-only.com *\/\nSELECT COUNT(*)\nFROM   Orders\nWHERE OrderDate = (\n   SELECT MAX(OrderDate)\n   FROM Orders\n);\n<\/pre>\n\n\n\n<p><em>The query returns 1.<\/em><\/p>\n\n\n\n<p>This query returns the number of orders that have the most recent order date. It uses the &#8220;=&#8221; comparison operator to compare the date of each order with the most recent order date, returned by the subquery:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mssql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/* T-SQL code by https:\/\/expert-only.com *\/\nSELECT MAX(OrderDate)\nFROM Orders;\n<\/pre>\n\n\n\n<p>The query returns the latest date, i.e. <em>2022-04-01<\/em>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"3-sql-server-comparison-operator-different-from\"><\/span>3. SQL Server comparison operator Different from<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The comparison operator &#8220;&lt;&gt;&#8221; or &#8220;!=&#8221;, called different from in English, is used to compare if two values are different. So it returns true if values are evaluated different by the system, and false is values are identical.<\/p>\n\n\n\n<p><strong>Simple example<\/strong><\/p>\n\n\n\n<p>This first simple example shows customers whose country is different from USA.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mssql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/* T-SQL code by https:\/\/expert-only.com *\/\nSELECT *\nFROM   Customers\nWHERE  Country &lt;> 'USA';\n<\/pre>\n\n\n\n<p>The result is therefore the only client with Canada as a country.<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><thead><tr><th>ClientID<\/th><th>Name<\/th><th>Country<\/th><\/tr><\/thead><tbody><tr><td>2<\/td><td>Jane Doe<\/td><td>Canada<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Complex example<\/strong><\/p>\n\n\n\n<p>This query returns customers who are in a country other than the United States and who have placed an order that includes at least one product priced above 500. It uses the &#8220;&lt;&gt;&#8221; comparison operator to filter out customers who are not in the United States.<\/p>\n\n\n\n<p>It also uses the &#8220;AND&#8221; operator to combine this filter with the expensive product filter. In addition, a T-SQL join is needed to link the Clients, Orders, and Products tables.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mssql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/* T-SQL code by https:\/\/expert-only.com *\/\nSELECT *\nFROM Customers c\n   JOIN Orders o\n      ON c.CustomerID = o.CustomerID\n   JOIN Products p\n      ON o.OrderID = p.ProductID\nWHERE  c.Country &lt;> 'USA'\n   AND p.Price > 50;\n   <\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"4-t-sql-comparison-operators-greater-than-or-less-than\"><\/span>4. T-SQL comparison operators greater than or less than<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The comparison operator &gt; or &lt;, that is to say strictly greater than or less than, is used to compare if a value is greater than another.<\/p>\n\n\n\n<p><strong>Simple example<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mssql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/* T-SQL code by https:\/\/expert-only.com *\/\nSELECT *\nFROM Products\nWHERE Price > 500;\n<\/pre>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><thead><tr><th class=\"has-text-align-center\" data-align=\"center\">ProductID<\/th><th class=\"has-text-align-center\" data-align=\"center\"><strong>ProductName<\/strong><\/th><th class=\"has-text-align-center\" data-align=\"center\"><strong>Price<\/strong><\/th><th class=\"has-text-align-center\" data-align=\"center\"><strong>Weight<\/strong><\/th><\/tr><\/thead><tbody><tr><td class=\"has-text-align-center\" data-align=\"center\">1<\/td><td class=\"has-text-align-center\" data-align=\"center\">Computer<\/td><td class=\"has-text-align-center\" data-align=\"center\">1000<\/td><td class=\"has-text-align-center\" data-align=\"center\">20<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The result is the only product with a price above 500, the computer.<\/p>\n\n\n\n<p><strong>Complex example<\/strong><\/p>\n\n\n\n<p>This query returns the names of customers who have placed more than one order. It uses the &gt; comparison operator to filter groups of customers with more than one order, using the HAVING COUNT(*) &gt; 1 clause.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mssql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/* T-SQL code by https:\/\/expert-only.com *\/\nSELECT\n   c.CustomerName,\n   COUNT(*) as 'Number of orders'\nFROM   Customers c\nJOIN   Orders o\n   ON c.CustomerID = o.CustomerID\nGROUP  BY c.CustomerName\nHAVING COUNT(*) > 1;\n<\/pre>\n\n\n\n<p>The result is as follows, with John Smith being the customer who placed 2 orders. To write a T-SQL query and use the less than comparison operator, use the same logic, but in reverse.<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><thead><tr><th class=\"has-text-align-center\" data-align=\"center\"><strong>Name<\/strong><\/th><th class=\"has-text-align-center\" data-align=\"center\">Number of orders<\/th><\/tr><\/thead><tbody><tr><td class=\"has-text-align-center\" data-align=\"center\">John Smith<\/td><td class=\"has-text-align-center\" data-align=\"center\">2<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>To go further on manipulating data using SQL Server code, these examples help you understand <a href=\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-data-types\/\"><strong>the different data types available in SQL<\/strong><\/a>.<\/p>\n\n\n\n<figure class=\"wp-block-embed aligncenter is-type-wp-embed is-provider-sql-and-it-tutorials wp-block-embed-sql-and-it-tutorials\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"AAajL1bpNU\"><a href=\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-data-types\/\">SQL Server data types with code examples to create columns<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;SQL Server data types with code examples to create columns&#8221; &#8212; SQL and IT Tutorials\" src=\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-data-types\/embed\/#?secret=oOuGcozSNY#?secret=AAajL1bpNU\" data-secret=\"AAajL1bpNU\" width=\"500\" height=\"282\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"5-sql-server-greater-or-equal-to-and-less-or-equal-to-operators\"><\/span>5. SQL Server greater or equal to and less or equal to operators<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The greater than or equal and less than or equal comparison operators, &gt;= or &lt;=, allow to compare if a value is greater than or equal to another, or less than or equal to another, respectively.<\/p>\n\n\n\n<p><strong>Simple example using the greater or equal logical operator<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mssql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/* T-SQL code by https:\/\/expert-only.com *\/\nSELECT *\nFROM Employees\nWHERE Age >= 35;\n<\/pre>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><thead><tr><th class=\"has-text-align-center\" data-align=\"center\">EmployeeID<\/th><th class=\"has-text-align-center\" data-align=\"center\">EmployeeName<\/th><th class=\"has-text-align-center\" data-align=\"center\">Age<\/th><th class=\"has-text-align-center\" data-align=\"center\">Department<\/th><\/tr><\/thead><tbody><tr><td class=\"has-text-align-center\" data-align=\"center\">1<\/td><td class=\"has-text-align-center\" data-align=\"center\">John Smith<\/td><td class=\"has-text-align-center\" data-align=\"center\">35<\/td><td class=\"has-text-align-center\" data-align=\"center\">IT<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">3<\/td><td class=\"has-text-align-center\" data-align=\"center\">Bob Johnson<\/td><td class=\"has-text-align-center\" data-align=\"center\">40<\/td><td class=\"has-text-align-center\" data-align=\"center\">IT<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Complex example<\/strong><\/p>\n\n\n\n<p>This query returns the average price of products that have a weight greater than or equal to 15 or a price greater than or equal to 500. It uses the &#8220;&gt;=&#8221; (greater than or equal to) comparison operator to filter products based on their weight and price, using the clause WHERE Poids &gt;= 15 OR Prix &gt;= 500.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mssql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/* T-SQL code by https:\/\/expert-only.com *\/\nSELECT\n   AVG(Price) as 'Average price'\nFROM  Products\nWHERE ProductWeight >= 15\n   OR Price >= 500;\n<\/pre>\n\n\n\n<p>The result is displayed in one unique column showing the average price, as follows:<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><thead><tr><th class=\"has-text-align-center\" data-align=\"center\"><strong>Average price<\/strong><\/th><\/tr><\/thead><tbody><tr><td class=\"has-text-align-center\" data-align=\"center\">800<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>To write a query with the less than or equal to comparison operator, use the same logic.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"6-t-sql-logical-operator-and\"><\/span>6. T-SQL logical operator AND<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The AND logic operator, allows you to combine multiple filtering conditions in the WHERE clause of a query. Rows that do not satisfy all conditions are excluded from the result.<\/p>\n\n\n\n<p><strong>SQL Server query example using the AND operator<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mssql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/* T-SQL code by https:\/\/expert-only.com *\/\nSELECT *\nFROM Employees\nWHERE Age > 35\nAND Department = 'IT';\n<\/pre>\n\n\n\n<p>This query returns the employees who are over 35 years old and who work in the IT department. It uses the &#8220;AND&#8221; logical operator to combine the conditions on age and department, using the clause WHERE Age &gt; 35 AND Department = &#8216;IT&#8217;.<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><thead><tr><th class=\"has-text-align-center\" data-align=\"center\">IDEmployee<\/th><th class=\"has-text-align-center\" data-align=\"center\">EmployeeName<\/th><th class=\"has-text-align-center\" data-align=\"center\">Age<\/th><th class=\"has-text-align-center\" data-align=\"center\">Department<\/th><\/tr><\/thead><tbody><tr><td class=\"has-text-align-center\" data-align=\"center\">3<\/td><td class=\"has-text-align-center\" data-align=\"center\">Bob Johnson<\/td><td class=\"has-text-align-center\" data-align=\"center\">40<\/td><td class=\"has-text-align-center\" data-align=\"center\">IT<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"7-t-sql-or-logical-operator\"><\/span>7. T-SQL OR logical operator<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The &#8220;OR&#8221; logic operator allows you to select rows that satisfy at least one of the filtering conditions in the WHERE clause of a query.<\/p>\n\n\n\n<p><strong>Simple query example<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mssql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/* T-SQL code by https:\/\/expert-only.com *\/\nSELECT *\nFROM  Products\nWHERE Price &lt; 500\n   OR ProductWeight > 15;\n<\/pre>\n\n\n\n<p>In this case, only the products having a price under five hundred dollars and a weight larger than 15 are displayed.<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><thead><tr><th>ProductID<\/th><th>ProductName<\/th><th>Price<\/th><th>Weight<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>Computer<\/td><td>1000<\/td><td>20<\/td><\/tr><tr><td>3<\/td><td>Monitor<\/td><td>300<\/td><td>15<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Complex query example using the SQL OR operator<\/strong><\/p>\n\n\n\n<p>This query returns the names of customers who have placed less than 2 orders or more than 3 orders. It uses the &#8220;OR&#8221; logic operator to combine conditions on the number of orders, using the clause HAVING COUNT(<em>) &lt; 2 OR COUNT(<\/em>) &gt; 3.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mssql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/* T-SQL code by https:\/\/expert-only.com *\/\nSELECT\n   c.CustomerName,\n   COUNT(*) as 'Number of orders'\nFROM Customers c\n   JOIN Orders o\n      ON c.CustomerID = o.CustomerID\nGROUP BY c.CustomerName\n   HAVING COUNT(*) &lt; 2\n      OR COUNT(*) > 3;\n <\/pre>\n\n\n\n<p>In this second query using the OR SQL operator, we can see that only 2 customers have less than 2 or more then 3 orders.<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><thead><tr><th><strong>Name<\/strong><\/th><th>Number of orders<\/th><\/tr><\/thead><tbody><tr><td>Jane Doe<\/td><td>1<\/td><\/tr><tr><td>Bob Johnson<\/td><td>1<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"8-the-like-logical-operator-to-compare-strings\"><\/span>8. The LIKE Logical Operator to Compare Strings <span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In T-SQL, to compare one string against specific values and know if it contains it, we use the LIKE keyword. Indeed, the &#8220;LIKE&#8221; string operator allows you to filter rows that contain a specific string.<\/p>\n\n\n\n<p>You can use wildcard characters such as &#8220;%&#8221; (which matches any number of characters) and &#8220;_&#8221; (which matches a single character) in the string to search for.<\/p>\n\n\n\n<p>It is possible to use <a href=\"https:\/\/regexr.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">regular expressions<\/a> available with SQL Server in the LIKE clause for complex searches.<\/p>\n\n\n\n<p><strong>Simple example using LIKE comparison operator<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mssql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/* T-SQL code by https:\/\/expert-only.com *\/\nSELECT *\nFROM  Customers\nWHERE CustomerName LIKE 'Ja%';\n<\/pre>\n\n\n\n<p>This query returns customers whose name starts with &#8220;J&#8221;. It uses the &#8220;LIKE&#8221; string operator and the &#8220;%&#8221; wildcard character to filter customers based on their name, using the clause WHERE Name LIKE &#8216;J%&#8217;.<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><thead><tr><th>CustomerID<\/th><th>Name<\/th><th>Country<\/th><\/tr><\/thead><tbody><tr><td>2<\/td><td>Jane Doe<\/td><td>Canada<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Complex example using LIKE<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mssql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/* T-SQL code by https:\/\/expert-only.com *\/\nSELECT *\nFROM  Customers\nWHERE CustomerName LIKE 'J%'\n   OR Country LIKE 'C%';\n<\/pre>\n\n\n\n<p>SELECT * FROM Customers WHERE Name LIKE &#8216;J%&#8217; OR Country LIKE &#8216;C%&#8217;; This query returns customers whose name starts with &#8220;J&#8221; or who live in a country whose name starts with &#8220;C&#8221;. It uses the &#8220;LIKE&#8221; string operator and the &#8220;%&#8221; and &#8220;_&#8221; wildcard characters to filter customers based on their name and country, using the clause WHERE Name LIKE &#8216;J%&#8217; OR Country LIKE &#8216;C%&#8217;. The result is as follows:<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><thead><tr><th>CustomerID<\/th><th>Name<\/th><th>Country<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>John Smith<\/td><td>USA<\/td><\/tr><tr><td>2<\/td><td>Jane Doe<\/td><td>Canada<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"9-sql-server-contains-function-to-search-text\"><\/span>9. SQL Server CONTAINS function to search text<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The &#8220;CONTAINS&#8221; function in SQL Server is a powerful tool for text search within a column. It enables you to find specific text fragments in your data.<\/p>\n\n\n\n<p>However, for this function to work properly, <a href=\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-full-text-index-script\/\"><strong>a full-text index must be created on the column you want to search<\/strong>.<\/a><\/p>\n\n\n\n<p>Also if <a href=\"https:\/\/expert-only.com\/en\/errors\/full-text-search-is-not-installed-sql-server\/\"><strong>the Full-Search Feature is not installed on your SQL Server<\/strong><\/a> you may face an error.<\/p>\n\n\n\n<p>In essence, it would be something like this code, with 3 simple steps.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mssql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/* T-SQL code by https:\/\/expert-only.com *\/\n\n-- 1. Create the index\nCREATE UNIQUE INDEX ui_CustomerID \nON Customers(CustomerID);\n\n-- 2. Create the full text catalog\nCREATE FULLTEXT CATALOG ftCatalog AS DEFAULT;\n\n-- 3. Create the full text index\nCREATE FULLTEXT INDEX ON Customers\n(\n    CustomerName LANGUAGE English\n)\nKEY INDEX ui_CustomerID \n   ON ftCatalog \nWITH CHANGE_TRACKING AUTO;\n<\/pre>\n\n\n\n<figure class=\"wp-block-embed aligncenter is-type-wp-embed is-provider-sql-and-it-tutorials wp-block-embed-sql-and-it-tutorials\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"1l08L2Pllg\"><a href=\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-full-text-index-script\/\">Create a SQL Server full-text index column with a script<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;Create a SQL Server full-text index column with a script&#8221; &#8212; SQL and IT Tutorials\" src=\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-full-text-index-script\/embed\/#?secret=57gxvRrBuG#?secret=1l08L2Pllg\" data-secret=\"1l08L2Pllg\" width=\"500\" height=\"282\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<p><strong>First example using the CONTAINS SQL function<\/strong><\/p>\n\n\n\n<p>This query&#8217;s goal is to find all customers whose name contains &#8216;John&#8217; or &#8216;Smith&#8217;:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mssql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">SELECT *\nFROM Customers\nWHERE CONTAINS(CustomerName, 'John OR Smith');\n<\/pre>\n\n\n\n<p><strong>More complex example using the T-SQL predicate<\/strong><\/p>\n\n\n\n<p>A second query, this time to find all orders that have &#8216;John&#8217; and &#8216;USA&#8217; in the customer&#8217;s name and country fields respectively, but not &#8216;Smith&#8217;:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mssql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">SELECT *\nFROM Orders o\n   JOIN Customers c\n      ON o.CustomerID = c.CustomerID\nWHERE CONTAINS(c.CustomerName, 'John AND USA AND NOT Smith');\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Conclusion about SQL comparisons<\/h3>\n\n\n\n<p>Some T-SQL operators are very useful to compare values, whether they are stored in columns or in variables. They can be used with <em><strong>logic operators like AND and OR<\/strong><\/em> to combine multiple filter conditions.<\/p>\n\n\n\n<p>Choose comparison and logic operators carefully to get the desired result, while also ensuring to avoid syntax errors. It is also recommended to test the performance of queries using different operators to make sure the query runs efficiently.<\/p>\n\n\n\n<p>It is also recommended to create indexes on columns that are often used in filter conditions, in order to improve query performance. However, it is important not to overload the database with too many indexes, as this can also slow down performance.<\/p>\n\n\n\n<p>In general, it is important to plan queries well and choose the right operators to achieve the desired result efficiently.<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>T-SQL queries with examples using the most common T-SQL logical operators in order to compare 2 values. Here is a list with the comparison and logic operators commonly used in T-SQL language with various examples. The examples shown here <a class=\"mh-excerpt-more\" href=\"https:\/\/expert-only.com\/en\/t-sql\/comparison-operators-in-t-sql\/\" title=\"How to Compare Values in T-SQL?\">&#8230;<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":10834,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[454],"tags":[],"class_list":{"0":"post-26099","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-t-sql"},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.7 (Yoast SEO v26.2) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to Compare Values in T-SQL? T-SQL SQL Server<\/title>\n<meta name=\"description\" content=\"List of commonly used comparison and logical T-SQL operators to compare values with simple and complex example queries.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/expert-only.com\/en\/t-sql\/comparison-operators-in-t-sql\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Compare Values in T-SQL?\" \/>\n<meta property=\"og:description\" content=\"List of commonly used comparison and logical T-SQL operators to compare values with simple and complex example queries.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/expert-only.com\/en\/t-sql\/comparison-operators-in-t-sql\/\" \/>\n<meta property=\"og:site_name\" content=\"SQL and IT Tutorials\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/ExpertOnlyCom\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-05-22T04:41:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-18T15:53:50+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/problem-1951987_1920x1080.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1920\" \/>\n\t<meta property=\"og:image:height\" content=\"1080\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Expert-Only\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@expert_only\" \/>\n<meta name=\"twitter:site\" content=\"@expert_only\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Expert-Only\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/comparison-operators-in-t-sql\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/comparison-operators-in-t-sql\/\"},\"author\":{\"name\":\"Expert-Only\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef\"},\"headline\":\"How to Compare Values in T-SQL?\",\"datePublished\":\"2023-05-22T04:41:00+00:00\",\"dateModified\":\"2024-03-18T15:53:50+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/comparison-operators-in-t-sql\/\"},\"wordCount\":1529,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/expert-only.com\/en\/#organization\"},\"image\":{\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/comparison-operators-in-t-sql\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/problem-1951987_1920x1080.jpg\",\"articleSection\":[\"T-SQL\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/expert-only.com\/en\/t-sql\/comparison-operators-in-t-sql\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/comparison-operators-in-t-sql\/\",\"url\":\"https:\/\/expert-only.com\/en\/t-sql\/comparison-operators-in-t-sql\/\",\"name\":\"How to Compare Values in T-SQL? T-SQL SQL Server\",\"isPartOf\":{\"@id\":\"https:\/\/expert-only.com\/en\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/comparison-operators-in-t-sql\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/comparison-operators-in-t-sql\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/problem-1951987_1920x1080.jpg\",\"datePublished\":\"2023-05-22T04:41:00+00:00\",\"dateModified\":\"2024-03-18T15:53:50+00:00\",\"description\":\"List of commonly used comparison and logical T-SQL operators to compare values with simple and complex example queries.\",\"breadcrumb\":{\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/comparison-operators-in-t-sql\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/expert-only.com\/en\/t-sql\/comparison-operators-in-t-sql\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/comparison-operators-in-t-sql\/#primaryimage\",\"url\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/problem-1951987_1920x1080.jpg\",\"contentUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/problem-1951987_1920x1080.jpg\",\"width\":1920,\"height\":1080},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/comparison-operators-in-t-sql\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"en\",\"item\":\"https:\/\/expert-only.com\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Compare Values in T-SQL?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/expert-only.com\/en\/#website\",\"url\":\"https:\/\/expert-only.com\/en\/\",\"name\":\"SQL and IT Tutorials\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/expert-only.com\/en\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/expert-only.com\/en\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/expert-only.com\/en\/#organization\",\"name\":\"Expert-Only\",\"url\":\"https:\/\/expert-only.com\/en\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/09\/cropped-logo_Expert-Only.jpg\",\"contentUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/09\/cropped-logo_Expert-Only.jpg\",\"width\":381,\"height\":174,\"caption\":\"Expert-Only\"},\"image\":{\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/ExpertOnlyCom\/\",\"https:\/\/x.com\/expert_only\",\"https:\/\/www.youtube.com\/channel\/UCMS5sR_FwAetB0FmciNvUaA\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef\",\"name\":\"Expert-Only\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/084b15660763ff5b13bb60b2f52f97bb?s=96&d=identicon&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/084b15660763ff5b13bb60b2f52f97bb?s=96&d=identicon&r=g\",\"caption\":\"Expert-Only\"}}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"How to Compare Values in T-SQL? T-SQL SQL Server","description":"List of commonly used comparison and logical T-SQL operators to compare values with simple and complex example queries.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/expert-only.com\/en\/t-sql\/comparison-operators-in-t-sql\/","og_locale":"en_US","og_type":"article","og_title":"How to Compare Values in T-SQL?","og_description":"List of commonly used comparison and logical T-SQL operators to compare values with simple and complex example queries.","og_url":"https:\/\/expert-only.com\/en\/t-sql\/comparison-operators-in-t-sql\/","og_site_name":"SQL and IT Tutorials","article_publisher":"https:\/\/www.facebook.com\/ExpertOnlyCom\/","article_published_time":"2023-05-22T04:41:00+00:00","article_modified_time":"2024-03-18T15:53:50+00:00","og_image":[{"width":1920,"height":1080,"url":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/problem-1951987_1920x1080.jpg","type":"image\/jpeg"}],"author":"Expert-Only","twitter_card":"summary_large_image","twitter_creator":"@expert_only","twitter_site":"@expert_only","twitter_misc":{"Written by":"Expert-Only","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/expert-only.com\/en\/t-sql\/comparison-operators-in-t-sql\/#article","isPartOf":{"@id":"https:\/\/expert-only.com\/en\/t-sql\/comparison-operators-in-t-sql\/"},"author":{"name":"Expert-Only","@id":"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef"},"headline":"How to Compare Values in T-SQL?","datePublished":"2023-05-22T04:41:00+00:00","dateModified":"2024-03-18T15:53:50+00:00","mainEntityOfPage":{"@id":"https:\/\/expert-only.com\/en\/t-sql\/comparison-operators-in-t-sql\/"},"wordCount":1529,"commentCount":0,"publisher":{"@id":"https:\/\/expert-only.com\/en\/#organization"},"image":{"@id":"https:\/\/expert-only.com\/en\/t-sql\/comparison-operators-in-t-sql\/#primaryimage"},"thumbnailUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/problem-1951987_1920x1080.jpg","articleSection":["T-SQL"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/expert-only.com\/en\/t-sql\/comparison-operators-in-t-sql\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/expert-only.com\/en\/t-sql\/comparison-operators-in-t-sql\/","url":"https:\/\/expert-only.com\/en\/t-sql\/comparison-operators-in-t-sql\/","name":"How to Compare Values in T-SQL? T-SQL SQL Server","isPartOf":{"@id":"https:\/\/expert-only.com\/en\/#website"},"primaryImageOfPage":{"@id":"https:\/\/expert-only.com\/en\/t-sql\/comparison-operators-in-t-sql\/#primaryimage"},"image":{"@id":"https:\/\/expert-only.com\/en\/t-sql\/comparison-operators-in-t-sql\/#primaryimage"},"thumbnailUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/problem-1951987_1920x1080.jpg","datePublished":"2023-05-22T04:41:00+00:00","dateModified":"2024-03-18T15:53:50+00:00","description":"List of commonly used comparison and logical T-SQL operators to compare values with simple and complex example queries.","breadcrumb":{"@id":"https:\/\/expert-only.com\/en\/t-sql\/comparison-operators-in-t-sql\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/expert-only.com\/en\/t-sql\/comparison-operators-in-t-sql\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/expert-only.com\/en\/t-sql\/comparison-operators-in-t-sql\/#primaryimage","url":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/problem-1951987_1920x1080.jpg","contentUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/problem-1951987_1920x1080.jpg","width":1920,"height":1080},{"@type":"BreadcrumbList","@id":"https:\/\/expert-only.com\/en\/t-sql\/comparison-operators-in-t-sql\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"en","item":"https:\/\/expert-only.com\/en\/"},{"@type":"ListItem","position":2,"name":"How to Compare Values in T-SQL?"}]},{"@type":"WebSite","@id":"https:\/\/expert-only.com\/en\/#website","url":"https:\/\/expert-only.com\/en\/","name":"SQL and IT Tutorials","description":"","publisher":{"@id":"https:\/\/expert-only.com\/en\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/expert-only.com\/en\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/expert-only.com\/en\/#organization","name":"Expert-Only","url":"https:\/\/expert-only.com\/en\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/expert-only.com\/en\/#\/schema\/logo\/image\/","url":"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/09\/cropped-logo_Expert-Only.jpg","contentUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/09\/cropped-logo_Expert-Only.jpg","width":381,"height":174,"caption":"Expert-Only"},"image":{"@id":"https:\/\/expert-only.com\/en\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/ExpertOnlyCom\/","https:\/\/x.com\/expert_only","https:\/\/www.youtube.com\/channel\/UCMS5sR_FwAetB0FmciNvUaA"]},{"@type":"Person","@id":"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef","name":"Expert-Only","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/expert-only.com\/en\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/084b15660763ff5b13bb60b2f52f97bb?s=96&d=identicon&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/084b15660763ff5b13bb60b2f52f97bb?s=96&d=identicon&r=g","caption":"Expert-Only"}}]}},"_links":{"self":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/26099","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/comments?post=26099"}],"version-history":[{"count":4,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/26099\/revisions"}],"predecessor-version":[{"id":30790,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/26099\/revisions\/30790"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media\/10834"}],"wp:attachment":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media?parent=26099"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/categories?post=26099"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/tags?post=26099"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}