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. Reputation Management In The Digital Age: Protecting And Enhancing Your Law Firm’s ImageAuthor: jamewilliams
2. What Features Should I Look For In Wordpress Ecommerce Plugins?
Author: Rocket Press
3. Staying Updated With The Latest Gaming News
Author: Next Tech Plus
4. Game Development: Evolving Technologies And New Horizons
Author: Rick John
5. Why Svelte Is The Most In-demand Framework For Web Development?
Author: Why Svelte Is The Most In-Demand Framework For Web
6. Maximizing Ebay Sales With Maropost/neto And Pursuit Info Solutions
Author: rachelvander
7. The Importance Of Software Testing: Ensuring Quality In Development
Author: John Mathew
8. Sadhgurusilveroaks - The Best School In Nellore
Author: Sadhgurusilveroak
9. Website Development Using Kentico – Cloud First Headless Cms
Author: Website Development Using Kentico – Cloud First He
10. Shopify Experts In Melbourne: Elevate Your E-commerce With The Merchant Buddy
Author: themerchantbuddy
11. Web Development 3.0: Shaping The Future Of The Internet
Author: Backend Brains
12. Top College Erp In India Helps Colleges Operate More Efficiently
Author: CONTENT EDITOR FOR SAMPHIRE IT SOLUTIONS PVT LTD
13. "lcc Computer Education: Expert Java Coaching Center"
Author: Khushi Gill
14. Which Institute Is Best For Full Stack Developers In Bhopal?
Author: Shankar Singh
15. Micheal John
Author: micheal