user2119980
user2119980

Reputation: 509

subtracting in SQL Server

I have a table in SQL Server where I have the scores for some competencies, I have one score for the standard and one for the actual score. For instance S25 is the actual score and C25 is the standard for the score. I need to find the difference between the two so I can see who was above and below the standard and cannot figure out how to get the subtract to work. THe way I tried was

Select (S25) - (C25) AS 25_Score

Which did not work

Upvotes: 0

Views: 1364

Answers (3)

Jimmy Smith
Jimmy Smith

Reputation: 2451

If table starts with a number, bracket it, and that might work. What error do you get?

select (S25)-(C25) AS [25_Score]
from table_name

Upvotes: 4

Taryn
Taryn

Reputation: 247870

Your query should work if your columns are a numeric datatype.

The only issue I see is you are starting the alias with a number. You will need to escape the number value with a square bracket:

Select (S25) - (C25) AS [25_Score]
from yt;

See Demo

Upvotes: 2

Prahalad Gaggar
Prahalad Gaggar

Reputation: 11609

It may be that the column is of varchar so you have to convert

select convert(int,[S25])-convert(int,[C25]) AS [25_Score]
from table_name

Upvotes: 1

Related Questions