Click here to Skip to main content
16,004,778 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
SQL
DECLARE @PDate date,
@In_Punch datetime
SELECT *From MASTERPROCESSDAILYDATA where PDate>='02-1-13' AND PDate<= '02-28-13'
IF(@In_Punch>='08:00:00' AND @In_Punch <= '09:30:00')
BEGIN
    UPDATE MASTERPROCESSDAILYDATA set Status = 'P'  SELECT  *from MASTERPROCESSDAILYDATA;
END

ELSE

SQL
BEGIN
    UPDATE MASTERPROCESSDAILYDATA set Status = 'HL'  SELECT  *from MASTERPROCESSDAILYDATA;
END</pre>


It is not working only else part is executing & status is updated to all the students.

Can anyone help me how to write sql query where In_punch between '08:00:00 AM' AND '10:30:00AM'

Out_punch between '04:00:00 PM' AND '05:30:00PM'


Consider for a set of 5 students like std1,std2,std3,std4,std5(from table1)

eg:
std1,std2 made their punches beyond the timings i.e., In_punch between '08:00:00 AM' AND '10:30:00AM',
Out_punch between '04:00:00 PM' AND '05:30:00PM'
UPDATE Status ='P'
otherwise
UPDATE Status ='HL'

How can i update the status of students using if condition or loops


Thanks in advance
Posted
Updated 24-Dec-13 0:32am
v4
Comments
OriginalGriff 24-Dec-13 5:12am    
How are you storing the data? What are the datatypes?
Karthik Achari 24-Dec-13 7:40am    
datatype for In_Punch & Out_Punch was datetime
datatype for status was varchar(MAX)

Ashish_Agrawal 24-Dec-13 7:05am    
Where are you assigning @In_Punch?

1 solution

The problem is probably that you need to look at the time parts only: a DateTime has both date and time, so if your timestamps are literally that then they probably include a date element. But that code looks rather odd anyway...
What you probably want to do is along the lines of

C#
UPDATE MASTERPROCESSDAILYDATA SET Status =  
   CASE WHEN CAST(PDate AS TIME) 
             BETWEEN CAST ('08:00' AS TIME) 
             AND CAST('09:30' AS TIME)
   THEN 'P'
   ELSE 'HL'
   END

[edit]Wrong column name[/edit]
 
Share this answer
 
v2

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