Click here to Skip to main content
15,901,853 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have one table checkinout

column name:

EMPID
CHECKTIME
cost
company pass
total


now i have to write a query to search the time from checktime if it is >7:00:00 and <12:00:00 then insert 10 into column COST and 20 Column COMPANY PASS and total of COST+COMPANY PASS into Column Total ie=30,

else if the checktime >12:00:00 the COST=20 and COMPANY PASS=30 and total =COST+COMPANY PASS.
Posted
Comments
Nelek 1-Aug-13 5:36am    
Nawab Ahmad 1-Aug-13 5:37am    
i don't know to use if condition with sql query......

If u know plz help..
[no name] 1-Aug-13 5:52am    
Then read the documentation.

Hi,

Here you have not shown datatype of fields.
See following query, if it works.

SQL
UPDATE checkinout
SET    cost=10,companypass=20,total=30
WHERE  CAST(CHECKTIME AS DATETIME) BETWEEN CAST('7:00:00' AS DATETIME) AND CAST('12:00:00' AS DATETIME)

UPDATE checkinout
SET    cost=20,companypass=30,total=50
WHERE  CAST(CHECKTIME AS DATETIME) > CAST('12:00:00' AS DATETIME)



If you are considering EMPID as where conditions then check following query.

SQL
UPDATE checkinout
SET    cost=10,companypass=20,total=30
WHERE  CAST(CHECKTIME AS DATETIME) BETWEEN CAST('7:00:00' AS DATETIME) AND CAST('12:00:00' AS DATETIME) AND EMPID=@EMPID

UPDATE checkinout
SET    cost=20,companypass=30,total=50
WHERE  CAST(CHECKTIME AS DATETIME) &gt; CAST('12:00:00' AS DATETIME)
   AND EMPID=@EMPID


This both query will execute and one of them will update value for EMPID.


Another way is using if and else conditions.
 
Share this answer
 
v3
Comments
Nawab Ahmad 1-Aug-13 6:00am    
Thanks ..
its working...
but one problem is there....
checktime = DATETIME formate
i have value in checktime= 6/02/2013 7:24

so, if i want to execute this code i have to mention the date also...but i have to do it by checking time only.....
Harshil_Raval 1-Aug-13 6:13am    
hi, then change CAST(CHECKTIME AS DATETIME) to CAST(CONVERT(VARCHAR,CHECKTIME,108) AS DATETIME) and whole query will remain same.
Nawab Ahmad 1-Aug-13 6:20am    
Thanx....
working perfectly....
Nawab Ahmad 1-Aug-13 6:22am    
INSERT INTO Canteen(EMPID,CHECKTIME,CHECKTYPE,VERIFYCODE,SENSORID,LOGID) SELECT * from checkinout where sensorid='4'

i am using this code to fetch data from table where sensorid=4
so what i have to modify if i want to fetch data from two sensorid 3 and 4...
Harshil_Raval 1-Aug-13 6:26am    
check this, INSERT INTO Canteen(EMPID,CHECKTIME,CHECKTYPE,VERIFYCODE,SENSORID,LOGID) SELECT * from checkinout where sensorid in ('4','3')
Hi,

if you want to insert then use..

SQL
insert into tablename
select [EMPID],[CHECKTIME]
,case when datepart(HOUR,[CHECKTIME])>7 and datepart(HOUR,[CHECKTIME]) < 12 then 10 when datepart(HOUR,[CHECKTIME])>12 then 20 else [cost] end as [cost]
,case when datepart(HOUR,[CHECKTIME])>7 and datepart(HOUR,[CHECKTIME])< 12 then 20 when datepart(HOUR,[CHECKTIME])>12 then 30 else [company pass] end as [company pass]
,case when datepart(HOUR,[CHECKTIME])>7 and datepart(HOUR,[CHECKTIME]) < 12 then 30 when datepart(HOUR,[CHECKTIME])>12 then 50 else [Total ] end as [Total ]
from checkinout



and if you need to update

SQL
update checkinout
set cost= case when datepart(HOUR,[CHECKTIME])>7 and datepart(HOUR,[CHECKTIME]) < 12 then 10 when datepart(HOUR,[CHECKTIME])>12 then 20 else [cost] end
,[companypass]=case when datepart(HOUR,[CHECKTIME])>7 and datepart(HOUR,[CHECKTIME])< 12 then 20 when datepart(HOUR,[CHECKTIME])>12 then 30 else [company pass]  end
,[Total]=case when datepart(HOUR,[CHECKTIME])>7 and datepart(HOUR,[CHECKTIME]) < 12 then 30 when datepart(HOUR,[CHECKTIME])>12 then 50 [Total ] end
 
Share this answer
 
v3
Comments
Nawab Ahmad 1-Aug-13 6:08am    
My CHECKTIME value look like this 3/02/2013 7:54
but i want to take only time and insert the value....
Adarsh chauhan 1-Aug-13 6:15am    
i have updated my query now check

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