{"id":1006,"date":"2010-09-09T20:24:55","date_gmt":"2010-09-09T14:54:55","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=1006"},"modified":"2010-09-09T20:24:55","modified_gmt":"2010-09-09T14:54:55","slug":"case-statement-in-where-clause-sql-server-conditional-where-clause","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/sql\/case-statement-in-where-clause-sql-server-conditional-where-clause\/","title":{"rendered":"Case statement in Where Clause &#8211; SQL Server &#8211; Conditional Where clause"},"content":{"rendered":"<p>In few scenario, we might need to perform conditional where clause. Some time the condition can be written well in OR with Where clause but in some scenario it is not possible.<\/p>\n<p>so this time, i have come up with one more article on writing the conditional where clause.<\/p>\n<p>Example:<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nDECLARE @Student Table (Stream Varchar(20), &#x5B;Name] Varchar(20), isAllow BIT )\n\nINSERT Into @Student\nVALUES\n('Computer Engineering','Minal Zaa', 0)\n\nINSERT Into @Student\nVALUES\n('Civil Engineering','Manoranjan Sahoo', 1)\n\nINSERT Into @Student\nVALUES\n('scientist','Santosh Karemore', 1)\n\nINSERT Into @Student\nVALUES\n('BCom','Rahul jha', 0)\n\nSELECT\n   Stream,\n   &#x5B;Name]\nFROM\n   @Student\nWHERE\nCASE\n    WHEN Stream = 'Computer Engineering' THEN 1\n    WHEN isAllow = 1 THEN 1\nEND = 1\n<\/pre>\n<p>Output:<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/09\/Case-statement-in-Where-Clause.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1011\" title=\"Case statement in Where Clause\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/09\/Case-statement-in-Where-Clause.jpg?resize=259%2C98&#038;ssl=1\" alt=\"Case statement in Where Clause\" width=\"259\" height=\"98\" \/><\/a><\/p>\n<p>In above code, we have checked that if stream is &#8216;Computer Engineering&#8217; then display the record or if IsAllow bit is true then display the record. Same query can be written using OR operator but here i wanted to demonstrate the use of Case statement in Where Clause.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Using Case statement in Where Clause in SQl Server. This script is used to create the conditional where clause<\/p>\n","protected":false},"author":1,"featured_media":1011,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[13,14],"tags":[292],"class_list":["post-1006","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-sql","category-sqlserver","tag-sql-server"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":1161,"url":"https:\/\/www.jitendrazaa.com\/blog\/sql\/sqlserver\/sql-server-pivot-table-example\/","url_meta":{"origin":1006,"position":0},"title":"SQL Server Pivot Table Example","author":"Jitendra","date":"September 29, 2010","format":false,"excerpt":"Example and tutorial of using PIVOT in SQL Server","rel":"","context":"In &quot;SQL Server&quot;","block_context":{"text":"SQL Server","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/sql\/sqlserver\/"},"img":{"alt_text":"SQL Server Pivot Table","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/09\/SQL-Server-Pivot-Table.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1147,"url":"https:\/\/www.jitendrazaa.com\/blog\/sql\/sqlserver\/between-clause-problem-in-date-comparison-sql-server\/","url_meta":{"origin":1006,"position":1},"title":"Between Clause problem in Date comparison &#8211; SQL Server","author":"Jitendra","date":"September 23, 2010","format":false,"excerpt":"SQL Server Date Comparison Tips : Never user Between clause with DateTime in SQL Server","rel":"","context":"In &quot;SQL Server&quot;","block_context":{"text":"SQL Server","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/sql\/sqlserver\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2861,"url":"https:\/\/www.jitendrazaa.com\/blog\/sql\/pagination-and-switch-case-in-where-clause-sql-server\/","url_meta":{"origin":1006,"position":2},"title":"Pagination and Switch Case in Where clause &#8211; SQL Server","author":"Jitendra","date":"May 19, 2012","format":false,"excerpt":"Example of using pagination and switch case in where clause in SQL server","rel":"","context":"In &quot;SQL&quot;","block_context":{"text":"SQL","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/sql\/"},"img":{"alt_text":"SQL Server Employee Table","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/05\/SQL-Server-Employee-Table.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1422,"url":"https:\/\/www.jitendrazaa.com\/blog\/sql\/update-from-statement-in-sql-server\/","url_meta":{"origin":1006,"position":3},"title":"&#8220;Update From&#8221; Statement in SQL Server","author":"Jitendra","date":"December 17, 2010","format":false,"excerpt":"Example to demonstrate the \"Update From\" Statement in SQL Server. This example can be used in scenarios where developer wants to update the Table with the help of Inner Join.","rel":"","context":"In &quot;SQL&quot;","block_context":{"text":"SQL","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/sql\/"},"img":{"alt_text":"SQL Server Table 1","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/12\/SQL-Server-Table-1.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":829,"url":"https:\/\/www.jitendrazaa.com\/blog\/sql\/sqlserver\/convert-rows-into-comma-separated-values-column-sql-server\/","url_meta":{"origin":1006,"position":4},"title":"Convert rows into comma separated values column &#8211; SQL Server","author":"Jitendra","date":"October 4, 2010","format":false,"excerpt":"Convert rows into comma separated values column - SQL Server","rel":"","context":"In &quot;SQL Server&quot;","block_context":{"text":"SQL Server","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/sql\/sqlserver\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":5882,"url":"https:\/\/www.jitendrazaa.com\/blog\/sql\/sqlserver\/sql-server-read-all-files-in-directory-and-store-in-table\/","url_meta":{"origin":1006,"position":5},"title":"SQL Server &#8211; Read all files in directory and store in Table","author":"Jitendra","date":"January 18, 2017","format":false,"excerpt":"T-SQL Script to read all files in directory and store back in table - SQL Server","rel":"","context":"In &quot;SQL Server&quot;","block_context":{"text":"SQL Server","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/sql\/sqlserver\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/1006","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=1006"}],"version-history":[{"count":0,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/1006\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=1006"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=1006"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=1006"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}