使用LEN的ntext字段的SQL SUM

时间:2023-01-29 16:28:02

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

我想计算ntext列中的字符数,然后获取SUM例如,我有以下查询:

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.

我现在想要得到长度字段的SUM。使用MS SQL 2008。

3 个解决方案

#1


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

#2


0  

try this:

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

由于您使用的是sql server 2008,因此可以使用公用表表达式(CTE):

;with cte as(
     SELECT LEN(field1) AS Length, field1 
     FROM table)
select sum(Length) as sumOfLenght from CTE

#3


0  

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

你可以先找到长度,然后找到那个长度的总和......

SELECT SUM(LEN(field1)) FROM table

#1


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

#2


0  

try this:

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

由于您使用的是sql server 2008,因此可以使用公用表表达式(CTE):

;with cte as(
     SELECT LEN(field1) AS Length, field1 
     FROM table)
select sum(Length) as sumOfLenght from CTE

#3


0  

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

你可以先找到长度,然后找到那个长度的总和......

SELECT SUM(LEN(field1)) FROM table