SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


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


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

Author
Message
ppc493
ppc493
Grasshopper
Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)

Group: General Forum Members
Points: 14 Visits: 47
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. Smile
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25940 Visits: 17519
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.

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)
GSquared
GSquared
SSC-Insane
SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)

Group: General Forum Members
Points: 23347 Visits: 9730
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
sturner
sturner
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2218 Visits: 3259
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.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search