Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 create a new column with values from another colum

Author  Topic 

mavericky
Posting Yak Master

117 Posts

Posted - 2011-09-22 : 15:52:52
Hi,
I have a table 'Student' which has column StuID.
StuID has 12000 values in this pattern:
1284810111382
1284810111383
1284810111384
1284810111385
1284810111386
1284810111387
1284810111388
1284810111389
1284810111390

I want to get the last 8 characters from all values.
I want to create another column called as ID which should values:
10111382
10111383
10111384
10111385
10111386
10111387
10111388
10111389
10111390

Can anyone help me how this can be done?
Thanks in anticipation,
Mavericky

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-09-22 : 16:18:15
If it's always 12848, then you can use subtraction:

update t1
set ID = stuID - 1284800000000

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

Ehan
Starting Member

19 Posts

Posted - 2011-09-22 : 16:19:58
alter table Student add ID as (RIGHT(StuID,8))

if you want to physically store computed column add persisted

alter table Student add ID as (RIGHT(StuID,8)) PERSISTED
Go to Top of Page

mavericky
Posting Yak Master

117 Posts

Posted - 2011-09-22 : 16:20:31
Thanks for the reply but it's not always 12848, there are other 5 digit characters too int the column. Is it still possible?
Go to Top of Page

mavericky
Posting Yak Master

117 Posts

Posted - 2011-09-22 : 16:35:32
Thanks a lot!!! This solved my problem.
Go to Top of Page

mavericky
Posting Yak Master

117 Posts

Posted - 2011-09-22 : 16:56:00
Actually, this code solves the issue but i want the datatype of the column to be int. Right now the datatype shows blank and it is uneditable. Can this be done?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-09-22 : 22:40:23
[code]
UPDATE Student
SET ID=StuID%100000000
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -