1

How to calculate the sum of values in a column using SQL query, wherein the column name has comma in it? Below is a sample table:

Saint Petersburg, Russia
7.34
29.35
4.4

Output required== SUM(7.34+29.35+4.4) Thanks

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
Kirti
  • 31
  • 1
  • 10
  • 1
    Ugh. Why do you have a column with that name in the first place? That has less than ideal normalization choices written all over it. – Sean Lange Feb 17 '20 at 15:08

1 Answers1

1

Is this what you want?

select sum([Saint Petersburg, Russia])
from t;

You need to escape the column name and the traditional escape method in SQL Server uses square braces. Double quotes can also be used.

If the issue is that 'Saint Petersburg, Russia' is a potential value in a character column you want to sum, then use try_convert():

select sum(try_convert(numeric(20, 4), col)
from t;
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786