Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8485357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:37:15+00:00 2026-06-10T20:37:15+00:00

Is possible to have an and statement in a SQL CASE clause? If so

  • 0

Is possible to have an and statement in a SQL CASE clause? If so how do you write it?

i.e something like this?:

CASE
    When ((Column < 1) AND (Column2 > 0)) THEN column4 + 1
END as Column

my code that executes but doesn’t accomplish what i want (have no negative numbers):

CASE
WHEN (((Clean_Total_Time - ActivityTimeTotalMin) < 0) AND (ActivityTimeTotalMin > 0))     
THEN (ABS(DATEDIFF(hh,Total_Time,ActivityTotalTime))-1)
END as hours

my code that does work and does accomplish what it should but I don’t want it because it allows for negative numbers:

CASE
WHEN (Clean_Total_Time - ActivityTimeTotalMin) < 0
THEN (ABS(DATEDIFF(hh,Total_Time,ActivityTotalTime))-1)
END as hours

EDIT: The purpose of this calculation is as follows:

It subtracts two columns, and if the result is negative, is subtracts the number one from one of those two columns. Further, my AND section was to say that if that column had a value of zero, then it would not subtract 1 from the column. I don’t want that column to have numbers below zero.

The only thing that is not happening, is that it is not catching if the column is equal to zero. So I am getting negative 1 whenever there was originally a 0.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T20:37:17+00:00Added an answer on June 10, 2026 at 8:37 pm

    According to this MSDN article it is:

    SELECT ProductNumber, Name, "Price Range" = 
          CASE 
             ...
             WHEN ListPrice >= 50 and ListPrice < 250 THEN 'Under $250'
             WHEN ListPrice >= 250 and ListPrice < 1000 THEN 'Under $1000'
             ELSE 'Over $1000'
          END
    FROM Production.Product
    

    I just don’t think you need the parentheses.

    Update 1

    You can try using CASE inside CASE like so:

    CASE
        WHEN Column < 1
            CASE
                WHEN Column2 > 0 THEN column4 + 1
            END
    END AS Column
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to have a SELECT statement in mySQL with an IF/ELSE clause
I would love to have a t-sql statement like the following... SELECT [field] FROM
I have a query like this: SELECT A, B, C, CASE WHEN 1 =
I would like to have an UPDATE statement like this: SELECT * FROM Employee
I don't know if this is possible in SQL or if I have to
Possible Duplicate: Subtraction in sql statement I have a table with Presidents names and
This works: SELECT (CASE WHEN x = 'value' THEN a.col1 ELSE nvl(a.col1, a.col2) END)
I have some sql statements that calculates some numbers. It's possible (with bonus points/penalty
Is it possible for a receive statement to have multiple timeout clauses, and if
Possible Duplicate: Using a bitwise & inside an if statement I have the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.