---
title: "How can I create and use a calculated column in a SQL Server query?"  
description: "How can I create and use a calculated column in a SQL Server query?"  
author: "ICSM Computer"  
published: 2024-07-11  
updated: 2024-07-12  
canonical: https://www.mindstick.com/forum/160898/how-can-i-create-and-use-a-calculated-column-in-a-sql-server-query  
category: "SQL Server"  
tags: ["database", "sql server", "sql server 2008", "sql server 2012", "sql server 2022"]  
reading_time: 3 minutes  

---

# How can I create and use a calculated column in a SQL Server query?

How can I create and use a [calculated](https://answers.mindstick.com/qa/31699/what-is-sensex-and-how-it-is-calculated) [column](https://www.mindstick.com/forum/33860/how-to-calculate-column-summary-in-sql-server) in a SQL [Server query](https://www.mindstick.com/forum/160192/how-does-the-sql-server-query-optimizer-work)?

## Replies

### Reply by Ravi Vishwakarma

Creating and using calculated columns in an [**SQL Server**](https://www.mindstick.com/articles/269427/learn-the-best-way-to-learn-sql-server) [query](https://www.mindstick.com/blog/202/sub-query-in-sqlserver) allows you to generate values based on existing data in your table. These calculated columns can be used in `SELECT` statements or defined directly in the table schema.

Here's how to create and use calculated columns in both ways:

#### 1. Using Calculated Columns in a `SELECT` Statement

You can create calculated columns directly in your `SELECT` statement by using expressions or functions.

```plaintext
CREATE TABLE CollageStudents (
    StudentID INT PRIMARY KEY, -- Create StudentID with primary key
    FirstName VARCHAR(50),
    LastName VARCHAR(50),
    DOB DATE,
    [Address] VARCHAR(100),
    Age AS DATEDIFF(YEAR, DOB, GETDATE())  -- Calculated column for Age
);

INSERT INTO CollageStudents (StudentID, FirstName, LastName, DOB, [Address])
VALUES
(1, 'John', 'Doe', '2000-05-15', '123 Main St, City'),
(2, 'Jane', 'Smith', '2001-10-20', '456 Oak Ave, Town'),
(3, 'Michael', 'Johnson', '1999-03-08', '789 Elm Rd, Village');
```

#### Querying the Table

Now, let's query the `CollageStudents` table to see how the calculated column `Age` works:

```plaintext
SELECT StudentID, FirstName, LastName, DOB, Age, Address
FROM CollageStudents;
```

![How can I create and use a calculated column in a SQL Server query?](https://www.mindstick.com/mindstickforums/f7b2a69f-3bc6-4175-a818-baa12199d852/images/ba65fc52-198a-4d83-89f6-8bf3809ad273.png)

#### Explanation

**Table Definition**:

- `CollageStudents` table has columns `StudentID`, `FirstName`, `LastName`, `DOB`, `Address`.
- `Age` is a calculated column using the `AS` keyword and the `DATEDIFF` function to calculate the difference in years between `DOB` and the current date (`GETDATE()`).

**Querying the Table**:

- The `SELECT` statement retrieves all columns including `Age`, which is computed dynamically based on the `DOB` of each student.

#### Additional Notes

1. **Updating Calculated Columns**: Calculated columns are automatically updated whenever a row is inserted or updated. They provide a convenient way to derive data based on existing columns.
2. **Performance Considerations**: While calculated columns simplify querying by dynamically computing values, they should be used judiciously in scenarios where performance implications are minimal.

#### Modify the Table and Create a new Computed Column

```plaintext
ALTER TABLE CollageStudents
ALTER COLUMN DOB DATETIME -- Change the column datatype
ADD FullName AS (FirstName + ' ' + LastName); -- Add New Computed or Calculated column
```

Let's see in table

![How can I create and use a calculated column in a SQL Server query?](https://www.mindstick.com/mindstickforums/f7b2a69f-3bc6-4175-a818-baa12199d852/images/d0fbd994-da9c-49b5-9154-96df6289d444.png)

#### Querying the Table

Now, let's query the `CollageStudents` table to see how the calculated column `Age` works:

```plaintext
SELECT * FROM CollageStudents;
```

![How can I create and use a calculated column in a SQL Server query?](https://www.mindstick.com/mindstickforums/f7b2a69f-3bc6-4175-a818-baa12199d852/images/16685f12-64bd-486d-84cb-bbe339d33100.png)

#### You can create a computed column with conditions.

Create `ParmanantAddress` when `IsSameAddress` value is true.

```plaintext
ALTER TABLE CollageStudents
ADD IsSameAddress BIT NOT NULL DEFAULT 0
ADD ParmanantAddress AS (
    CASE
        WHEN IsSameAddress = 1 THEN [Address]
        ELSE NULL
    END
);

-- Update some values
update CollageStudents
set IsSameAddress = 1
where StudentID = 3
-- Run it
SELECT * FROM CollageStudents;
```

## Output

![How can I create and use a calculated column in a SQL Server query?](https://www.mindstick.com/mindstickforums/f7b2a69f-3bc6-4175-a818-baa12199d852/images/66c00727-9245-4498-9bda-3bd556fcbe3e.png)

## Read more

[**Explain the SQL Server backups and their types**](https://www.mindstick.com/articles/336326/explain-the-sql-server-backups-and-their-types)

[**Explain the SQL triggers and their uses**](https://www.mindstick.com/articles/336344/explain-the-sql-triggers-and-their-uses)

[**Define the PIVOT Table with examples in SQL server.**](https://www.mindstick.com/articles/336334/define-the-pivot-table-with-examples-in-sql-server)

[Differences between stored procedures and functions in SQL](https://www.mindstick.com/articles/336349/differences-between-stored-procedures-and-functions-in-sql-server)


---

Original Source: https://www.mindstick.com/forum/160898/how-can-i-create-and-use-a-calculated-column-in-a-sql-server-query

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
