ALL >> Computer-Programming >> View Article
Sqlyoga: Adding Computed Columns In Sql Server
![Profile Picture](https://www.123articleonline.com/images/avatar-default.jpg)
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. How App Development Companies Are Revolutionizing Industries In 2024Author: Backend Brains
2. Latest & Ecommerce Technologies & Trends In 2024
Author: SpryBit Softlab
3. Elevate Your Tech Team: The Impact Of Hiring Top Node.js Programmers
Author: sanjay
4. Which Is The Best Institute For The Java Aadhaar Foundation Course In Bhopal?
Author: Shankar Singh
5. Boost Your Business Efficiency With Apptrop’s Industry-specific Tools
Author: apptrop
6. How Java Language Is Important In Programming?
Author: Java Learning Center
7. What Is The Role Of Artificial Intelligence In Computer Science?
Author: Suresh Gyan Vihar
8. Alvi Software’s Top Magento Development Service
Author: Jeet Hanani
9. Why Your Business Needs A Mobile App: The Role Of A Mobile App Development Company
Author: Backend Brains
10. Exploring Microservices Architecture: The Future Of Scalable Software Solutions
Author: Softylus
11. Essential Computer Maintenance Tips: Keep Your Pc Running Smoothly
Author: Robbin-Micheal
12. Why Every Real Estate Business Needs A Professional Web Development Company Uae
Author: Adele White
13. How The Best Website Development Company In Dubai Improves User Experience
Author: Adele White
14. Why Your Liquor Store Needs The Best Pos System: A Guide From Atlantic Systems Inc
Author: Elena Stewart
15. Alvi Software's Magento Development Services
Author: Jeet Hanani