2

I want to count the number of characters in a ntext column and then get the SUM For example I have the following query:

SELECT LEN(field1) AS Length, field1 
FROM table

It will return results like:

|Length|field1|
-------------------
  4     abcd
  6     abcdef
  4     abcd

I now want to get the SUM of the Length field. Using MS SQL 2008.

Himanshu
  • 31,810
  • 31
  • 111
  • 133
luke2012
  • 1,724
  • 1
  • 13
  • 20

3 Answers3

3

The simpliest solution would be, (without using a subquery or any other that could decrease the performance)

SELECT SUM(LEN(field1)) AS totalLength
FROM table
John Woo
  • 258,903
  • 69
  • 498
  • 492
0

try this:

Since you are using sql server 2008 , you could use common table expression(CTE):

;with cte as(
     SELECT LEN(field1) AS Length, field1 
     FROM table)
select sum(Length) as sumOfLenght from CTE
Joe G Joseph
  • 23,518
  • 5
  • 56
  • 58
0

You can first find Length and then sum of that length...

SELECT SUM(LEN(field1)) FROM table
Brijesh Patel
  • 2,901
  • 15
  • 50
  • 73