Use 'case-when' But error conditions overlap in query.

  • I try 'case when' with 'null' and 'not null' set to value 'ErrorCode' field.

    Ex.

    EmpNo|ChkDate |ChkIn |ChkOut |ErrorCode

    00001|2012-10-01 00:00:00.000|2012-10-01 07:21:00.000|2012-10-01 17:05:00.000|0

    00002|2012-10-01 00:00:00.000|2012-10-01 22:17:00.000|2012-10-01 00:00:00.000|6

    00003|2012-10-01 00:00:00.000|2012-10-01 00:00:00.000|2012-10-01 19:30:00.000|6

    00004|2012-10-01 00:00:00.000|NULL |NULL |7

    00005|2012-10-01 00:00:00.000|2012-10-01 07:10:00.000|2012-10-01 12:00:00.000|0

    00006|2012-10-01 00:00:00.000|2012-10-01 13:50:00.000|2012-10-01 19:20:00.000|0

    But i need Output (ErrorCode)

    EmpNo|ChkDate |ChkIn |ChkOut |ErrorCode

    00001|2012-10-01 00:00:00.000|2012-10-01 07:21:00.000|2012-10-01 17:05:00.000|0

    00002|2012-10-01 00:00:00.000|2012-10-01 22:17:00.000|2012-10-01 00:00:00.000|6

    00003|2012-10-01 00:00:00.000|2012-10-01 00:00:00.000|2012-10-01 19:30:00.000|6

    00004|2012-10-01 00:00:00.000|NULL |NULL |7

    00005|2012-10-01 00:00:00.000|2012-10-01 07:10:00.000|2012-10-01 12:00:00.000|8

    00006|2012-10-01 00:00:00.000|2012-10-01 13:50:00.000|2012-10-01 19:20:00.000|8

    valued in ChkIn and ChkOut is Half Day. I need set value = 8. but i try = 0.

    This Code:

    SELECT tf.EmpNo, tf.ChkDate, tf.ChkIn, tf.ChkOut,

    CASE WHEN ChkIn is not null and Convert(nvarchar(10), ChkOut,108) != '00:00:00' THEN 0

    WHEN ChkIn is not null and Convert(nvarchar(10) ,ChkOut,108) = '00:00:00' THEN 6

    WHEN Convert(nvarchar(10),ChkIn,108) = '00:00:00' and ChkOut is not null THEN 6

    WHEN Convert(nvarchar(10),ChkOut,108) <= '12:00:00' OR Convert(nvarchar(10),ChkOut,108) >= '12:00:01' Then 8

    WHEN ChkIn is null and ChkOut is null THEN 7 END as 'ErrorCode'

    FROM filesTA tf

    WHERE tf.ChkDate = '2012-10-01'

    Thanks you for your time. 🙂

  • The reason that so many people have viewed your issue and not responded is because it is not clear what problems you are facing from what you posted. In order to help we need to have ddl (create table statements), sample data (insert statements) and desired output along with a clear explanation. You can find examples of how to gather this information by reading the first link in my signature.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • You need to change the sequence of WHEN statements in your Case. Whatever you want to override something else, you need to have the override value first.

    So, move your check for code 8 first. See if that helps.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • As other have stated it is not clear exactly what sort of logic you are trying to achieve with just a select statement.

    Having said that though, your case statement is not correctly formed to produce consistent results, and you have an OR in their with no parenthesis to insure proper precedence.

    What it looks like you are trying to do is to compare check in or check out times against a threshold and then set a status code. Instead of an overly complicated case statement it might be cleaner to join against a table that contains the time values your are checking against. This time map table can be a CTE and would result in more readable SQL.

    Converting datetime to nvarchar's then comparing the result to other char strings is not a good way to check time periods across sets of data.

    The probability of survival is inversely proportional to the angle of arrival.

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply