{"id":27296,"date":"2023-12-25T06:24:00","date_gmt":"2023-12-25T05:24:00","guid":{"rendered":"https:\/\/expert-only.com\/?p=27296"},"modified":"2024-02-23T10:10:46","modified_gmt":"2024-02-23T09:10:46","slug":"how-to-use-if-then-else-in-sql-server","status":"publish","type":"post","link":"https:\/\/expert-only.com\/en\/t-sql\/how-to-use-if-then-else-in-sql-server\/","title":{"rendered":"How To Use IF THEN ELSE in SQL Server?"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\" id=\"h-what-is-the-syntax-of-the-if-then-else-in-sql-server-code-and-how-to-use-it\"><em>What is the syntax of the IF THEN ELSE in SQL Server code and how to use it ?<\/em><\/h4>\n\n\n\n<p>Transact-SQL comes with rich features allowing dynamic and also robust data manipulation, and among these, the IF THEN ELSE statement serves as a pillar for decision-making within SQL Server code. This traditional coding feature enables conditional execution of T-SQL statements, creating opportunities for more dynamic and responsive queries in data manipulation.<\/p>\n\n\n\n<p>In this SQL guide, let&#8217;s learn a few options of the IF THEN ELSE statement, using a sales table to provide real-world examples. Indeed, conditional statements are at the heart of algorithmic, which name comes from the <strong><a href=\"https:\/\/en.wikipedia.org\/wiki\/Al-Khwarizmi\" target=\"_blank\" rel=\"noreferrer noopener\">famous Persian Mathematician Al-Khwarizmi<\/a>.<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-what-is-an-if-then-else-statement-in-sql\">What is an IF THEN ELSE statement in SQL ?<\/h3>\n\n\n\n<p>The IF THEN ELSE statement in T-SQL is an essential tool for implementing conditional logic and decision making in your SQL Server queries and applications. When it comes to implementing business rules directly in SQL queries or making stored procedures and scripts more adaptable, the IF THEN ELSE statement becomes indispensable. Essentially, using this statement effectively means writing SQL code that is adaptable and more dynamic, as it uses the value of the data itself to perform certain actions.<\/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\/how-to-use-if-then-else-in-sql-server\/#1-prepare-a-sql-table-used-by-the-if-then-else-examples\" >1. Prepare a SQL table used by the IF THEN ELSE 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\/how-to-use-if-then-else-in-sql-server\/#2-basic-syntax-of-a-if-then-else-t-sql-query\" >2. Basic syntax of a IF THEN ELSE T-SQL query<\/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\/how-to-use-if-then-else-in-sql-server\/#3-simple-if-else-sql-server-example-based-on-a-column\" >3. Simple IF ELSE SQL Server example based on a column<\/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\/how-to-use-if-then-else-in-sql-server\/#4-use-else-if-for-multiple-conditions-in-one-query\" >4. Use ELSE IF for multiple conditions in one query<\/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\/how-to-use-if-then-else-in-sql-server\/#5-nested-if-then-else-queries-in-t-sql\" >5. Nested IF THEN ELSE queries in T-SQL<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"h-1-prepare-a-sql-table-used-by-the-if-then-else-examples\"><span class=\"ez-toc-section\" id=\"1-prepare-a-sql-table-used-by-the-if-then-else-examples\"><\/span>1. Prepare a SQL table used by the IF THEN ELSE examples<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>To illustrate the capabilities of the IF THEN ELSE conditional statement, let&#8217;s create and then populate the Sales table that includes a column representing the amount of each sale. Another version of the sales table was used in another tutorial, as an <a href=\"https:\/\/expert-only.com\/en\/t-sql\/xml-query-method-sql-server\/\"><strong>XML sales table<\/strong><\/a>. The second step of the script insert 3 sales with three different amounts.<\/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=\"\">-- Expert-Only.com\nCREATE TABLE Sales (\n  SalesID INT PRIMARY KEY,\n  Amount  FLOAT\n);\n\nINSERT INTO Sales (SalesID, Amount)\nVALUES\n   (1, 200.50),\n   (2, 300.75),\n   (3, 150.20);\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-2-basic-syntax-of-a-if-then-else-t-sql-query\"><span class=\"ez-toc-section\" id=\"2-basic-syntax-of-a-if-then-else-t-sql-query\"><\/span>2. Basic syntax of a IF THEN ELSE T-SQL query<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The basic syntax for the IF THEN ELSE statement in T-SQL appears as follows:<\/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=\"\">IF condition\n    BEGIN\n        -- statements to execute if condition is TRUE\n    END\nELSE\n    BEGIN\n        -- statements to execute if condition is FALSE\n    END\n<\/pre>\n\n\n\n<p>Note: T-SQL doesn&#8217;t actually use the <code>THEN<\/code> keyword; we&#8217;ve included it in the title for readability and to align with other programming languages that use a similar construct.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-3-simple-if-else-sql-server-example-based-on-a-column\"><span class=\"ez-toc-section\" id=\"3-simple-if-else-sql-server-example-based-on-a-column\"><\/span>3. Simple IF ELSE SQL Server example based on a column<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>To begin with a straightforward example, checking if a sale exceeds a certain amount:<\/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=\"\">-- Expert-Only.com\nDECLARE @SalesAmount FLOAT;\nSELECT  @SalesAmount = Amount\n  FROM  Sales\n  WHERE SalesID = 1;\n\nIF @SalesAmount > 200\n    BEGIN\n        PRINT 'This a Large Sale';\n    END\nELSE\n    BEGIN\n        PRINT 'This a Small Sale';\n    END\n<\/pre>\n\n\n\n<p>To expect this example to output &#8216;Large Sale&#8217; because the amount for SalesID = 1 is 200.50, which is greater than 200.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"720\" height=\"640\" src=\"https:\/\/expert-only.com\/wp-content\/uploads\/2024\/02\/sql-server-if-begin-end-else-query.jpg\" alt=\"How To Use IF THEN ELSE in SQL Server?\" class=\"wp-image-29993\" srcset=\"https:\/\/expert-only.com\/wp-content\/uploads\/2024\/02\/sql-server-if-begin-end-else-query.jpg 720w, https:\/\/expert-only.com\/wp-content\/uploads\/2024\/02\/sql-server-if-begin-end-else-query-300x267.jpg 300w\" sizes=\"auto, (max-width: 720px) 100vw, 720px\" \/><figcaption class=\"wp-element-caption\"><em>How To Use IF THEN ELSE in SQL Server?<\/em><\/figcaption><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-4-use-else-if-for-multiple-conditions-in-one-query\"><span class=\"ez-toc-section\" id=\"4-use-else-if-for-multiple-conditions-in-one-query\"><\/span>4. Use ELSE IF for multiple conditions in one query<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>To handle multiple conditions at the same time, for example for the same column with different use cases, use the <strong>ELSE IF<\/strong> keyword.<\/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=\"\">DECLARE @SalesAmount FLOAT;\nSELECT @SalesAmount = Amount FROM Sales WHERE SalesID = 1;\n\nIF @SalesAmount > 300\n    BEGIN\n        PRINT 'Very Large Sale';\n    END\nELSE IF @SalesAmount > 200\n    BEGIN\n        PRINT 'Large Sale';\n    END\nELSE\n    BEGIN\n        PRINT 'Small Sale';\n    END\n<\/pre>\n\n\n\n<p>We anticipate the output to be &#8216;Large Sale&#8217; because the amount falls between 200 and 300.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-5-nested-if-then-else-queries-in-t-sql\"><span class=\"ez-toc-section\" id=\"5-nested-if-then-else-queries-in-t-sql\"><\/span>5. Nested IF THEN ELSE queries in T-SQL<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Nested IF THEN ELSE queries in T-SQL allow developers to handle more complex conditions by embedding additional conditional logic within existing statements. This enhances the flexibility of decision-making processes in SQL scripts, enabling the execution of specific code blocks based on intricate combinations of conditions.<\/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=\"\">DECLARE @SalesAmount FLOAT;\nSELECT @SalesAmount = Amount FROM Sales WHERE SalesID = 1;\n\nIF @SalesAmount >= 300\n    BEGIN\n        IF @SalesAmount >= 500\n            BEGIN\n                PRINT 'Extra Large Sale';\n            END\n        ELSE\n            BEGIN\n                PRINT 'Very Large Sale';\n            END\n    END\nELSE\n    BEGIN\n        PRINT 'Small or Large Sale';\n    END\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-conditional-if-else-statement-allows-developers-to-create-dynamic-t-sql-queries\">Conditional IF ELSE statement allows developers to create dynamic T-SQL queries<\/h3>\n\n\n\n<p>The <em>IF THEN ELSE<\/em> statement in T-SQL serves as an indispensable tool for conditional logic and decision-making within SQL Server queries and applications. To master this feature allows for writing more dynamic, efficient, and robust SQL code. Whether just starting with T-SQL or an experienced developer looking to up your game, understanding the <em>IF THEN ELSE<\/em> statement becomes crucial for writing intelligent, responsive database code.<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>What is the syntax of the IF THEN ELSE in SQL Server code and how to use it ? Transact-SQL comes with rich features allowing dynamic and also robust data manipulation, and among these, the IF THEN ELSE statement <a class=\"mh-excerpt-more\" href=\"https:\/\/expert-only.com\/en\/t-sql\/how-to-use-if-then-else-in-sql-server\/\" title=\"How To Use IF THEN ELSE in SQL Server?\">&#8230;<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":10324,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[454],"tags":[],"class_list":{"0":"post-27296","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 Use IF THEN ELSE in SQL Server? T-SQL<\/title>\n<meta name=\"description\" content=\"Use the T-SQL IF THEN ELSE statement in SQL Server and implement conditional logic to make queries more efficient with T-SQL examples.\" \/>\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\/how-to-use-if-then-else-in-sql-server\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How To Use IF THEN ELSE in SQL Server?\" \/>\n<meta property=\"og:description\" content=\"Use the T-SQL IF THEN ELSE statement in SQL Server and implement conditional logic to make queries more efficient with T-SQL examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/expert-only.com\/en\/t-sql\/how-to-use-if-then-else-in-sql-server\/\" \/>\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-12-25T05:24:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-02-23T09:10:46+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/board-953155_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=\"3 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\/how-to-use-if-then-else-in-sql-server\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/how-to-use-if-then-else-in-sql-server\/\"},\"author\":{\"name\":\"Expert-Only\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef\"},\"headline\":\"How To Use IF THEN ELSE in SQL Server?\",\"datePublished\":\"2023-12-25T05:24:00+00:00\",\"dateModified\":\"2024-02-23T09:10:46+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/how-to-use-if-then-else-in-sql-server\/\"},\"wordCount\":554,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/expert-only.com\/en\/#organization\"},\"image\":{\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/how-to-use-if-then-else-in-sql-server\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/board-953155_1920x1080.jpg\",\"articleSection\":[\"T-SQL\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/expert-only.com\/en\/t-sql\/how-to-use-if-then-else-in-sql-server\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/how-to-use-if-then-else-in-sql-server\/\",\"url\":\"https:\/\/expert-only.com\/en\/t-sql\/how-to-use-if-then-else-in-sql-server\/\",\"name\":\"How To Use IF THEN ELSE in SQL Server? T-SQL\",\"isPartOf\":{\"@id\":\"https:\/\/expert-only.com\/en\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/how-to-use-if-then-else-in-sql-server\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/how-to-use-if-then-else-in-sql-server\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/board-953155_1920x1080.jpg\",\"datePublished\":\"2023-12-25T05:24:00+00:00\",\"dateModified\":\"2024-02-23T09:10:46+00:00\",\"description\":\"Use the T-SQL IF THEN ELSE statement in SQL Server and implement conditional logic to make queries more efficient with T-SQL examples.\",\"breadcrumb\":{\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/how-to-use-if-then-else-in-sql-server\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/expert-only.com\/en\/t-sql\/how-to-use-if-then-else-in-sql-server\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/how-to-use-if-then-else-in-sql-server\/#primaryimage\",\"url\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/board-953155_1920x1080.jpg\",\"contentUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/board-953155_1920x1080.jpg\",\"width\":1920,\"height\":1080},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/how-to-use-if-then-else-in-sql-server\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"en\",\"item\":\"https:\/\/expert-only.com\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How To Use IF THEN ELSE in SQL Server?\"}]},{\"@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 Use IF THEN ELSE in SQL Server? T-SQL","description":"Use the T-SQL IF THEN ELSE statement in SQL Server and implement conditional logic to make queries more efficient with T-SQL examples.","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\/how-to-use-if-then-else-in-sql-server\/","og_locale":"en_US","og_type":"article","og_title":"How To Use IF THEN ELSE in SQL Server?","og_description":"Use the T-SQL IF THEN ELSE statement in SQL Server and implement conditional logic to make queries more efficient with T-SQL examples.","og_url":"https:\/\/expert-only.com\/en\/t-sql\/how-to-use-if-then-else-in-sql-server\/","og_site_name":"SQL and IT Tutorials","article_publisher":"https:\/\/www.facebook.com\/ExpertOnlyCom\/","article_published_time":"2023-12-25T05:24:00+00:00","article_modified_time":"2024-02-23T09:10:46+00:00","og_image":[{"width":1920,"height":1080,"url":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/board-953155_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":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/expert-only.com\/en\/t-sql\/how-to-use-if-then-else-in-sql-server\/#article","isPartOf":{"@id":"https:\/\/expert-only.com\/en\/t-sql\/how-to-use-if-then-else-in-sql-server\/"},"author":{"name":"Expert-Only","@id":"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef"},"headline":"How To Use IF THEN ELSE in SQL Server?","datePublished":"2023-12-25T05:24:00+00:00","dateModified":"2024-02-23T09:10:46+00:00","mainEntityOfPage":{"@id":"https:\/\/expert-only.com\/en\/t-sql\/how-to-use-if-then-else-in-sql-server\/"},"wordCount":554,"commentCount":0,"publisher":{"@id":"https:\/\/expert-only.com\/en\/#organization"},"image":{"@id":"https:\/\/expert-only.com\/en\/t-sql\/how-to-use-if-then-else-in-sql-server\/#primaryimage"},"thumbnailUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/board-953155_1920x1080.jpg","articleSection":["T-SQL"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/expert-only.com\/en\/t-sql\/how-to-use-if-then-else-in-sql-server\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/expert-only.com\/en\/t-sql\/how-to-use-if-then-else-in-sql-server\/","url":"https:\/\/expert-only.com\/en\/t-sql\/how-to-use-if-then-else-in-sql-server\/","name":"How To Use IF THEN ELSE in SQL Server? T-SQL","isPartOf":{"@id":"https:\/\/expert-only.com\/en\/#website"},"primaryImageOfPage":{"@id":"https:\/\/expert-only.com\/en\/t-sql\/how-to-use-if-then-else-in-sql-server\/#primaryimage"},"image":{"@id":"https:\/\/expert-only.com\/en\/t-sql\/how-to-use-if-then-else-in-sql-server\/#primaryimage"},"thumbnailUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/board-953155_1920x1080.jpg","datePublished":"2023-12-25T05:24:00+00:00","dateModified":"2024-02-23T09:10:46+00:00","description":"Use the T-SQL IF THEN ELSE statement in SQL Server and implement conditional logic to make queries more efficient with T-SQL examples.","breadcrumb":{"@id":"https:\/\/expert-only.com\/en\/t-sql\/how-to-use-if-then-else-in-sql-server\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/expert-only.com\/en\/t-sql\/how-to-use-if-then-else-in-sql-server\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/expert-only.com\/en\/t-sql\/how-to-use-if-then-else-in-sql-server\/#primaryimage","url":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/board-953155_1920x1080.jpg","contentUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/board-953155_1920x1080.jpg","width":1920,"height":1080},{"@type":"BreadcrumbList","@id":"https:\/\/expert-only.com\/en\/t-sql\/how-to-use-if-then-else-in-sql-server\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"en","item":"https:\/\/expert-only.com\/en\/"},{"@type":"ListItem","position":2,"name":"How To Use IF THEN ELSE in SQL Server?"}]},{"@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\/27296","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=27296"}],"version-history":[{"count":9,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/27296\/revisions"}],"predecessor-version":[{"id":30001,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/27296\/revisions\/30001"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media\/10324"}],"wp:attachment":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media?parent=27296"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/categories?post=27296"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/tags?post=27296"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}