Click here to Skip to main content
15,112,252 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi 'Teachers'

I have a simple question, in my table(MSSQL DB) have fours columns 'working_date' as DateTime type ,'TIME_IN' as char, 'ACTUAL_END_DATE ' as datetime and 'TIME_OUT' as char.

Here is the question: I want to combine them( working_date +TIME_INand ACTUAL_END_DATE +TIME_OUT) and use 'DATEDIFF(minute,A,B)' to calculate working hours, I tried many times ,but every time error come out, my script like below:
SQL
SELECT	
	ISNULL(DATEDIFF(minute,working_date + TIME_IN,ACTUAL_END_DATE + TIME_OUT)*1.0/60,0.0) as 'HOURS'
 FROM ATTENDANCE

error message like this:
Conversion failed when converting date and/or time from character string.


I want the correct way, please help ,'Teachers'.
Posted

Hi
If you use from SQL 2008 you must use from bellow code:

SQL
DECLARE	@Day	As	Int, 
	@Month	As	Int, 
	@Year	As	Int, 
	@Hour	As	Int,
	@Minute	As	Int,
	@Second	As	Int,
	@Result	As	DateTime

Select	@Day	= Your Day, 
	@Month	= Your Month,
	@Year	= Your Year,
	@Hour	= Your Hour,
	@Minute	= Your Minute,
	@Second	= Your Second,

Select	@Result = DATEADD(SECOND, @Second , DATEADD(MINUTE, @Minute , DATEADD(HOUR, @Hour , DATEADD(MONTH, (@Year - 1900) * 12 + @Month - 1 , @Day - 1))))


On the other hand you must use:

DATEADD(SECOND, @Second , DATEADD(MINUTE, @Minute , DATEADD(HOUR, @Hour , DATEADD(MONTH, (@Year - 1900) * 12 + @Month - 1 , @Day - 1))))


Instead of:
DATETIMEFROMPARTS(Year(@StartDate), Month(@StartDate), Day(@StartDate), DATEPART(HOUR,@StartTimeTime), DATEPART(MINUTE,@StartTimeTime) , DATEPART(SECOND,@StartTimeTime) , 0)


I Hope it's helpful for you.
If you have question, Please tell me.
   
Comments
chenghuichao 14-Jul-13 4:48am
   
Hi boss, you are my boss! U save me. :)
Reza Alipour Fard 14-Jul-13 4:52am
   
Welcome, I hope you will be success in your life. Best Regrads
The"correct" way to do it is: don't store the time in or time out as char.
Store them as DateTime instead, and you can do math on them directly

Alternatively, store them as an integer number of minutes, and use the SQL DATEADD function to offset the working_date by that number of minutes.

Never store numeric info as string - it always makes life harder later on.
   
Hi
In clear way, You can use from bellow query instead of first solution:

SQL
Declare	@StartDate	As	Date,
	@StartTimeChar	As	nVarChar(10),
	@StartTimeTime	As	Time(0),
	@ResultDateTime	As	SmallDateTime
 

Set	@StartDate	= '2012-01-01'
Set	@StartTimeChar	= '20:30:00'
Set	@StartTimeTime	= Cast(@StartTimeChar as Time)
Set	@ResultDateTime = DATETIMEFROMPARTS(Year(@StartDate), Month(@StartDate), Day(@StartDate), DATEPART(HOUR,@StartTimeTime), DATEPART(MINUTE,@StartTimeTime) , DATEPART(SECOND,@StartTimeTime) , 0)
Set	@ResultDateTime = DATEADD(SECOND,  DATEPART(SECOND,@StartTimeTime) , DATEADD(MINUTE, DATEPART(MINUTE,@StartTimeTime) , DATEADD(HOUR, DATEPART(HOUR,@StartTimeTime) , DATEADD(MONTH, (Year(@StartDate) - 1900) * 12 + Month(@StartDate) - 1 , Day(@StartDate) - 1))))
 
Select	@StartDate, @StartTimeTime, @ResultDateTime


I hope it's helpful for you.
   
Hi
You can use from bellow query:

SQL
Declare	@StartDate	As	Date,
	@StartTimeChar	As	nVarChar(10),
	@StartTimeTime	As	Time(0),
	@ResultDateTime	As	SmallDateTime


Set	@StartDate	= '2012-01-01'
Set	@StartTimeChar	= '20:30:00'
Set	@StartTimeTime	= Cast(@StartTimeChar as Time)
Set	@ResultDateTime = DATETIMEFROMPARTS(Year(@StartDate), Month(@StartDate), Day(@StartDate), DATEPART(HOUR,@StartTimeTime), DATEPART(MINUTE,@StartTimeTime) , DATEPART(SECOND,@StartTimeTime) , 0)

Select	@StartDate, @StartTimeTime, @ResultDateTime


You must use from DATETIMEFROMPARTS() to create datetime and use Year(), Month(), Day() to get date elements and use DATEPART(HOURS/MINUTE/SECOND,@YourTime) to get hours, minute, second

I think it's helpful for you.
   
Comments
chenghuichao 14-Jul-13 3:39am
   
hi sir, I got the message:'DATEFROMPARTS' is not a recognized built-in function name.
Reza Alipour Fard 14-Jul-13 3:53am
   
Please wait a minute....
What is your SQL server version??? tell me your version until I give solution compatible with your version.
DATEFROMPARTS is a function in SQL server 2012.
Rate 1 out of 5!!! :-(
chenghuichao 14-Jul-13 4:02am
   
SQL server 2008 R2
chenghuichao 14-Jul-13 4:08am
   
I wrote as below also cant:
SELECT
convert(date,
convert(varchar(4),YEAR(WORKING_DATE))+
convert(varchar(2),MONTH(WORKING_DATE))+
convert(varchar(2),DAY(WORKING_DATE))+
convert(varchar(5),isnull(TIME_IN,'00:00'))
),
convert(date,
convert(varchar(4),YEAR(ACTUAL_END_DATE))+
convert(varchar(2),MONTH(ACTUAL_END_DATE))+
convert(varchar(2),DAY(ACTUAL_END_DATE))+
convert(varchar(5),isnull(TIME_OUT,'00:00'))
)
FROM ATTENDANCE

I might break!!
Reza Alipour Fard 14-Jul-13 4:26am
   
Use Solution 1

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