Solved

how to use aggregate function or nvarchar( or whats the workaround?)

Posted on 2014-09-09
6
508 Views
Last Modified: 2014-09-15
I have a column with score of test. The datatype  is set to nvarchar. Now I have to create a report from the table which includes avg of specific range of records. I cant use the avg function on the column. Please help. Thanks
0
Comment
Question by:SQL .NET
[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
  • 3
  • 2
6 Comments
 
LVL 26

Assisted Solution

by:Shaun Kline
Shaun Kline earned 200 total points
ID: 40313115
You should be able to cast your column to the data type you need (int, decimal) and then use the AVG function. However, you will also need to eliminate any non-numeric values by adding the ISNUMERIC function to your WHERE clause.
0
 
LVL 49

Accepted Solution

by:
PortletPaul earned 300 total points
ID: 40313264
You really do need to consider the data quality in such a situation, in particular for the range of records you want to average over: are there any where that score isn't numeric?

e.g.
select * from test_results where test_id = 1 and isnumeric(score) = 0 -- score is NOT numeric

if that is unacceptable you have to fix the data. Otherwise you can proceed as suggested by Shaun Kline above

e.g.
select AVG(CAST(score as int)) from test_results where test_id = 1 and isnumeric(score) = 1 -- score is numeric
0
 

Author Comment

by:SQL .NET
ID: 40314929
No the data has only numeric value and null value but the data type defined is nvarchar(50). Does Null value make the difference. Thanks Shaun AND Paul for the answers
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

Author Comment

by:SQL .NET
ID: 40317580
Can you please help with the use of convert function.
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 40318366
>>"Can you please help with the use of convert function."

what help do you need?

AVG(CAST(score as int))

AVG(CAST(score as decimal(6,2)))
0
 

Author Comment

by:SQL .NET
ID: 40324078
I have tried the cast but it gives error because it has decimal values too. And gives this error:Conversion failed when converting the nvarchar value '61.60' to data type int.
Warning: Null value is eliminated by an aggregate or other SET operation.
0

Featured Post

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

729 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