Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to cast time and how to compare in sql server
I am using following

I getting this error

Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.


Thanks

What I have tried:

SQL
update WorkingHours set intime='09:45' where cast(intime as time) >= '09:46' and cast(intime as time) <= '09:55'
  PRINT ''
  update WorkingHours set outtime='18:15' where cast(outtime as time) >= '18:16'
  update WorkingHours set intime='09:25' where cast(intime as time) < '09:20'
  update WorkingHours set LateComing='Yes' where cast(intime as time) > '10:45' and InTime is not null
  update WorkingHours set EarlyLeaving='Yes' where cast(OutTime as time) < '05:45' and OutTime is not null
  update WorkingHours set MissPunchDate=date where InTime is not null and OutTime is null
  update WorkingHours set MissPunchDate=date where OutTime is not null and InTime is null
  
  update WorkingHours set ShortPerDay='Yes' where (dbo.[MinutesToDuration](datediff(minute,intime,outtime))) < '8' and InTime is not null and OutTime is not null
Posted
Updated 29-Jun-17 8:12am
v2
Comments
RedDk 29-Jun-17 13:58pm    
See here:
https://technet.microsoft.com/en-us/library/ms186724(v=sql.110).aspx

You need to compare like items that have numerical values.
int, float, time, date, datetime, etc., all have numerical values.
Strings, on the other hand, do not.

If you wish to compare two times then they must both be cast to TIME.
Rephrasing:
You cannot compare cast(outtime as time) and '18:16'
Do this:
cast(outtime as time) > cast('18:16' as time) 



 
Share this answer
 

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