Solved

converting varchar to int to extract records

Posted on 2011-09-07
4
349 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now