?
Solved

converting varchar to int to extract records

Posted on 2011-09-07
4
Medium Priority
?
357 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
[X]
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
4 Comments
 
LVL 3

Assisted Solution

by:Crower
Crower earned 200 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 1200 total points
ID: 36494018
Or try this:

where Absent > '04:00'
0
 
LVL 39

Assisted Solution

by:appari
appari earned 600 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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Your data is at risk. Probably more today that at any other time in history. There are simply more people with more access to the Web with bad intentions.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

752 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