[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

converting varchar to int to extract records

Posted on 2011-09-07
4
Medium Priority
?
361 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 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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
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…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

810 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