Click here to Skip to main content
15,921,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to get time part from '2013-11-18 14:15:39.000'

I am using MS SQL SERVER2008R2
I want to update status AS 'P','A','HL'

SPC:
C#
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE  SP_DIFF(@FromDate datetime,@ToDate datetime)
AS
BEGIN	
SET NOCOUNT ON;

DECLARE 
@In_Punch datetime,
@Out_Punch datetime
--@PDate datetime
 
SELECT CASE WHEN  CAST(@In_Punch AS TIME,@Out_Punch AS TIME ) BETWEEN CAST ('08:00' AS TIME) 
             AND CAST('16:00'  AS TIME) THEN 'P'
             WHEN  CAST(@Out_Punch AS TIME ) BETWEEN CAST ('16:00' AS TIME) 
             AND CAST('17:30'  AS TIME) THEN 'P'
--WHEN  CAST(@In_Punch AS TIME ) BETWEEN CAST ('08:00' AS TIME) 
             --AND CAST('09:30'  AS TIME) THEN 'P'
WHEN  CAST(@In_Punch AS TIME,@Out_Punch AS TIME ) BETWEEN CAST ('09:31' AS TIME) 
             AND CAST('15:59'  AS TIME) THEN 'HL'
             ELSE 'A'
--SELECT *FROM MASTERPROCESSDAILYDATA where PDate > '02-1-13' AND PDate <= '02-28-13'
      
END             
END 
GO

Can anyone help me how to write SPC
SQL
OUTPUT:

@In_Punch                           @Out_Punch                Status
08:00 AND 09:30     between         16:00 AND 17:30            'P'
09:30               between         15:59                      'HL'


ELSE 'A'


Thanks in advance
Posted
Updated 29-Dec-13 20:13pm
v5

1 solution

SQL
SELECT CASE WHEN  CAST(@In_Punch AS TIME,@Out_Punch AS TIME ) BETWEEN CAST ('08:00' AS TIME)
             AND CAST('16:00'  AS TIME) THEN 'P'



This is code you invented. It's not SQL. That's not how it works, you need to read up on what the right SQL is, not just make something up. You'd need to do this in several steps. First, is the in time between the two times, and then, is the out time between the two.

Also, if you must take a datetime and them turn it in to a time, do it once. Do it once for the times you use to check as well, not over and over again. This makes your code more efficient and more readable.

SQL
declare @eightAM as time = cast('08:00' as time)
declare @fourPM as time = cast('16:00' as time)
declare @timein as time = cast(@In_punch as time)
declare @timeout as time = cast(@Out_punch as time)

select case when @timein >= @eightAM and @timeOut <= @timeout and @timein < @timeOut then 'P'



and so on. Note I am doing less checks than you. My code checks the same thing, but is more readable.
 
Share this answer
 
Comments
Karthik Achari 31-Dec-13 0:49am    
I am a fresher could you please suggest some Reference books because I don't have any idea about SQL now iam learning

Thanks
Christian Graus 31-Dec-13 0:55am    
Start by doing what I told you. There are a ton of articles on SQL on this site as well, I've started my own series, it starts at http://www.codeproject.com/Articles/700317/SQL-Wizardry-Episode-One-Joins
Christian Graus 31-Dec-13 0:56am    
So if you have no idea about SQL, I assume this is not for a class or a paid job, it's just to learn ? I would suggest you instead find a book or series of articles you can work through, and abandon this project, because you clearly can't do it, now.
Karthik Achari 31-Dec-13 0:56am    
Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900