4

I want to add a SQL condition in my existing query, to return NULL if the below condition is reached.

I have 2 columns SHIFTA_START and SHIFTA_END.

  • [SHIFTA_START] = employee's clock in time
  • [SHIFTA_END] = employee's clock out time

Example:

  • SHIFTA_START: 3/10/2016 12:15:00 PM
  • SHIFTA_END: 3/10/2016 12:30:00 AM

Hence I want to condition

IF DATEPART SHIFTA_START = DATEPART SHIFTA_END   
   AND SHIFTA_START > SHIFTA_END  
THEN return NULL

How do I make code this in SQL? Help is appreciated, thank You so much! ^_^

1
  • hi op, you've got some good answers below. Feel free to check that they work and upvote any that are relevant. Also please select what you consider the best answer as accepted. Commented Jun 9, 2016 at 7:46

5 Answers 5

2

Use CASE EXPRESSION :

SELECT t.*,
       CASE WHEN CAST(SHIFTA_START as DATE) = CAST(SHIFTA_END as DATE)
                 AND SHIFTA_START > SHIFTA_END 
            THEN NULL
            ELSE SomeOtherValue
       END as Your_Ind_Column
FROM YourTable 

This will result with the entire data set, and an indication column. I didn't see how only this column would help you , but if you want only that, remove t.*, from the query.

Sign up to request clarification or add additional context in comments.

4 Comments

That's never going to return a result, look at the WHEN statement
Its CASE EXPRESSION, not WHEN STATEMENT and of course it does!
So those fields both need to equal each other AND one is greater than the other. I was talking specifically about the WHEN part of your CASE EXPRESSION
@RichBenner Yes, you are correct, didn't look at the names. Fixed.
2

You're after a case expression, you want to convert the dates to date rather than datetime too (I think that's what you're trying to do).

SELECT
CASE
    WHEN CONVERT(date, SHIFTA_START) = CONVERT(date, SHIFTA_END)
        AND SHIFTA_START > SHIFTA_END
        THEN NULL
    ELSE CASE 
            WHEN CONVERT(date, SHIFTA_START) = CONVERT(date, SHIFTA_END)                         
                THEN DATEDIFF(mi, SHIFTA_START, SHIFTA_END)
            ELSE 'Different Days' 
         END
   END AS TimeWorked
FROM TableName

The calculation above will give you the following values;

  • start and end on same day BUT start after end = NULL
  • start and end on same day AND start before end (correct) = minutes between start and end
  • start and end on different days = 'Different Days'

3 Comments

Read the OP's question, he's checking that shifta_start and shifta_end are on the same date, he's then looking for anything where the shift end date comes before the start date.
@sagi I'm assuming the OP is probably calculating the hours worked for employees based upon their clock in and out times. but doesn't want negative values because of incorrect data and also wants to restrict the results to each day (e.g. if somebody doesn't clock out). We've all had to deal with bad data at one point in our careers.
I know, I already commented on this in my answer and fixed my answer
1

Below query will give the required result :-

declare @test table
(SHIFTA_START datetime NOT NULL,
 SHIFTA_END datetime NOT NULL
)

Insert into @test
values('2016-03-10 12:15:00 PM','2016-03-10 12:30:00 AM')

select 
CASE WHEN ((datepart(dd,SHIFTA_START) = datepart(dd,SHIFTA_END)) AND (cast(SHIFTA_START as time) > cast(SHIFTA_END as time)))
THEN NULL 
ELSE '' END AS RESULT from @test

Result

NULL

1 Comment

You need to be aware of the issues that you may have with your datepart calculation here. What if the two fields are on the 1st of June and the 1st of July? You'd be better to convert the datetime to date to avoid potential issues with this.
1

In simple cases (like simple condition leading to TRUE or FALSE) you can use inline version of conditional values supported by IIF() function...

Comments

0

Use CASE WHEN condition. Here is an example:

SELECT 
    CASE WHEN 
        DATEPART SHIFTA_START = DATEPART SHIFTA_END
        AND SHIFTA_START > SHIFTA_END 
    THEN
        NULL 
    ELSE
        YOUR_COLUMN
    END
FROM YOUR_TABLE

1 Comment

DATEPART SHIFTA_START won't work in SQL I'm afraid, you need to do a calculation here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.