Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

converting varchar to int to extract records

Posted on 2011-09-07
4
352 Views
Last Modified: 2012-05-12
Hi everyone

I have attached a piece of code which I use to calculate students start_time, end_time, time late, time absent and so on for each day.  My aim is to find students who have been absent for longer than 4 hours per day (which is why I write all the records into the temp table #students then select from here which students have been absent 4 hours).  My problem is because absent is a varchar it wont let me compare to see if absent > 4 as I get the following error

Msg 8114, Level 16, State 5, Line 80
Error converting data type varchar to numeric.

Can anyone help me fix this?

Thanks
if object_id('tempdb..#students','U') is not null drop table #students

SELECT student_id, date, week_no, [start time], [end time], CONVERT(VARCHAR(5), [Late], 108) AS 'Late',
	CONVERT(VARCHAR(5), [Absent], 108) AS 'Absent'

into #students

FROM
(
	SELECT  student_id, Date,week_no, 
		MIN(CONVERT(datetime, start_time)) AS 'Start Time', 
		MAX(CONVERT(datetime, End_Time)) AS 'End Time',
		DateAdd(n, SUM(CASE WHEN attendance_type = 'LA' THEN late_minutes ELSE 0 END), 0) AS 'Late',
		DateAdd(n, SUM(CASE WHEN attendance_type IN ('AA', 'AB') THEN DATEDIFF(minute, start_time,  end_time) ELSE 0 END) 
			+ SUM(CASE WHEN attendance_type = 'LA' THEN late_minutes ELSE 0 END), 0) AS 'Absent'
		FROM allstudents

	GROUP BY student_id, Date, week_no
) X	 


select *

from #students

where Absent > 4

Open in new window

0
Comment
Question by:lisa_mc
4 Comments
 
LVL 3

Assisted Solution

by:Crower
Crower earned 50 total points
ID: 36493996

The following link may help. It explains in 2 functions to convert formats and some examples.

I hope it helps


http://msdn.microsoft.com/en-us/library/ms187928.aspx
0
 
LVL 19

Accepted Solution

by:
Rimvis earned 300 total points
ID: 36494018
Or try this:

where Absent > '04:00'
0
 
LVL 39

Assisted Solution

by:appari
appari earned 150 total points
ID: 36494019
try this,
if object_id('tempdb..#students','U') is not null drop table #students

SELECT student_id, date, week_no, [start time], [end time], CONVERT(VARCHAR(5), [Late], 108) AS 'Late',
	CONVERT(VARCHAR(5), [Absent], 108) AS 'Absent'

into #students

FROM
(
	SELECT  student_id, Date,week_no, 
		MIN(CONVERT(datetime, start_time)) AS 'Start Time', 
		MAX(CONVERT(datetime, End_Time)) AS 'End Time',
		DateAdd(n, SUM(CASE WHEN attendance_type = 'LA' THEN late_minutes ELSE 0 END), 0) AS 'Late',
		DateAdd(n, SUM(CASE WHEN attendance_type IN ('AA', 'AB') THEN DATEDIFF(minute, start_time,  end_time) ELSE 0 END) 
			+ SUM(CASE WHEN attendance_type = 'LA' THEN late_minutes ELSE 0 END), 0) AS 'Absent'
		FROM allstudents

	GROUP BY student_id, Date, week_no
) X	 


select *

from #students

where datepart(hh,convert(datetime,Absent)) > 4

Open in new window

0
 
LVL 3

Author Closing Comment

by:lisa_mc
ID: 36494178
Hi rimvis - worked perfect thanks so I picked this as my main answer

Hi appari - I also accepted this as it shows another way of doing things
but one correction instead of
where datepart(hh,convert(datetime,Absent)) > 4
I needed
where datepart(hh,convert(datetime,Absent)) >= 4
because the line >4 only selects times 5 hours or more but I needed 4 hours or more therefore I needed >=4

Hi Crower - thanks for the reference

Thanks very much guys for the prompt answers really appreciate it
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question