Click here to Skip to main content
15,908,842 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
Hi,

I want to get the average of nvarchar.

Here is an example:

Date           Dir<br />
2011/04/19     ESE<br />
2011/04/19     WNE<br />
2011/04/19     ESE


Is possible to get the average of the column Dir in a SQL Proc?
Posted
Updated 19-Apr-11 18:17pm
v2
Comments
walterhevedeich 19-Apr-11 22:48pm    
Average of non-numeric characters? Im not aware of such thing. Do you mind explaining further what the scenario is?
jalmonte 20-Apr-11 0:16am    
I have data every 10 minutes. I want to get the average (once a month) to store in another table. But the data contain numeric and non-numerics values.
Pong D. Panda 20-Apr-11 0:24am    
what do you mean by average? Average count of same Dir?
Sandeep Mewara 20-Apr-11 1:00am    
Average of what? Here we have date and alphanumeric text.

1 solution

You need to break the column data into two columns and then use the average function by converting the date into float.

Following is the example:
SQL
DECLARE @test_date TABLE (dt datetime)

INSERT INTO @test_date VALUES (GETDATE() + 1)
INSERT INTO @test_date VALUES (GETDATE() + 2)
INSERT INTO @test_date VALUES (GETDATE() + 3)

SELECT CONVERT(char(8),CAST(AVG(CAST(dt AS FLOAT) - FLOOR(CAST(dt AS FLOAT))) AS datetime),108) FROM @test_date
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900