Jesh
Jesh

Reputation: 121

Rename a select column in sql

I have a question about SQL. Here is the case: I have a table with 5 columns (C1...C5) I want to do

 select (C1+C2*3-C3*5/C4) from table;

Is there any way of naming the resulting column for referring later in a query ?

Upvotes: 12

Views: 32817

Answers (3)

a'r
a'r

Reputation: 37029

Yes, its called a column alias.

select (C1+C2*3-C3*5/C4) AS result from table;

Upvotes: 5

Andrea
Andrea

Reputation: 1057

select (C1+C2*3-C3*5/C4) as new_name from table;

Upvotes: 4

Jacob
Jacob

Reputation: 43299

SELECT (C1+C2*3-C3*5/C4) AS formula FROM table;

You can give it an alias using AS [alias] after the formula. If you can use it later depends on where you want to use it. If you want to use it in the where clause, you have to wrap it in an outer select, because the where clause is evaluated before your alias.

SELECT * 
FROM (SELECT (C1+C2*3-C3*5/C4) AS formula FROM table) AS t1
WHERE formula > 100

Upvotes: 21

Related Questions