Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Use 'case-when' But error conditions overlap in query. Expand / Collapse
Author
Message
Posted Tuesday, November 27, 2012 7:51 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, April 29, 2013 9:44 AM
Points: 14, Visits: 46
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. :)
Post #1389207
Posted Tuesday, November 27, 2012 8:09 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Tuesday, December 23, 2014 12:10 PM
Points: 13,330, Visits: 12,829
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1389216
Posted Tuesday, November 27, 2012 8:12 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Thursday, December 18, 2014 9:58 AM
Points: 13,872, Visits: 9,600
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
Post #1389219
Posted Tuesday, November 27, 2012 8:28 AM


UDP Broadcaster

UDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP Broadcaster

Group: General Forum Members
Last Login: Thursday, December 11, 2014 12:22 PM
Points: 1,446, Visits: 3,232
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.
Post #1389230
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse