ALL >> Computer-Programming >> View Article
Sqlyoga: Adding Computed Columns In Sql Server

A scenario that required me to add one calculated column came up today. When a column is computed, it can be used in queries just like any other column and allows us to change one or more columns from the same table.
Lets try it:
Create One Table: tblTestComputed
CREATE TABLE tblTestComputed(
FirstName VARCHAR(50),
LastName VARCHAR(50)
)
Lets insert some data into it:
INSERT INTO tblTestComputed(FirstName, LastName)
SELECT 'Tejas', 'Shah'
UNION
SELECT 'Hiral', 'Shah'
So now I run:
SELECT * FROM tblTestComputed
I will get output like:
Output
I now require the display name to be something like “Shah Tejas” or “Shah Hiral”. I therefore created a new column and named it:
ALTER TABLE tblTestComputed
ADD FullName AS (ISNULL(LastName,'') + ' ' + ISNULL(FirstName,''))
So, now if I run:
SELECT * FROM tblTestComputed
So, Output like:
Output
Add a computed column called FullName to your table to combine first and last names, updating automatically.
However, it’s essential to note that ...
... computed columns cannot be updated directly since they are derived from other columns.
For a deeper dive into computed columns and their applications, click here to learn more. Join SQLYoga to boost your SQL and database skills with tutorials, articles, and expert tips. Be part of a growing community of learners.
Add Comment
Computer Programming Articles
1. Agentic Ai Development Services: Unlocking The Future Of Smarter AutomationAuthor: Albert
2. Why The Best Data Science Institute In Bhopal Is The Gateway To Top It Jobs
Author: Rohan Rajput
3. Complete Php Tutorial: Master Core Php Concepts With Examples
Author: Tech Point
4. Framework7 Tutorial For Beginners – Create Powerful Hybrid Apps
Author: Tech Point
5. Why Spadegaming Is Dominating The Asian Igaming Market
Author: Alex
6. Why Express.js And Mongodb Are A Dynamic Duo For Node.js Development
Author: Andy
7. Discover The Best Data Science Institute In Bhopal For A Future-ready Career
Author: Rohan Rajput
8. Best Data Science Institute In Bhopal: Learn Ai, Ml & Analytics With Experts
Author: Rohan Rajput
9. Jstl Tutorial – Simplifying Jsp Development
Author: Tech Point
10. Easy Java I/o Tutorial For Beginners To Learn File Operations
Author: Tech Point
11. Ai Chatbot Development Vs. Traditional Chatbot Development
Author: Albert
12. Good Schools In Bhopal Offering Academics With All-round Growth
Author: Ronit Sharma
13. Top Data Science Academy In Bhopal
Author: Rohan Rajput
14. Premier Data Science Courses In Bhopal
Author: Rohan Rajput
15. Jsf Tutorial: Everything You Need To Know About Javaserver Faces
Author: Tech Point