July 5, 2013 at 12:02 pm
Hi
I declared the primary key as identity int.
Now everytime a value(code) is selected from the grid , I want a corresponding character to be concatenated to the primary key( integer) and stored in another column.
Example : if code = student then add "S" to "1" and store S1 to another column.
else if code = Teacher then add "T" to 2 and store T2.
How should I implement this logic?
July 5, 2013 at 12:14 pm
sharonsql2013 (7/5/2013)
HiI declared the primary key as identity int.
Now everytime a value(code) is selected from the grid , I want a corresponding character to be concatenated to the primary key( integer) and stored in another column.
Example : if code = student then add "S" to "1" and store S1 to another column.
else if code = Teacher then add "T" to 2 and store T2.
How should I implement this logic?
i would create a view, which joins your two tables together, and creates a calculated value with the logic you describe;
there's no need to store the data, just return the desired data when queries;
plus you get the added benefit of the view always returning the correct data, and when some student gets promoted to a teacher, you don't have to update his data because of this...it's always right as long as you changed your other table to now say he is a teacher, or teachers assistant, or whether is appropriate.
CASE WHEN T2.SomeColumn = 'SomethingAboutStudent'
THEN 'S' + CONVERT(Varchar,T1.PK)
WHEN T2.SomeColumn = 'SomethingAboutTeacher'
THEN 'T' + CONVERT(Varchar,T1.PK)
ELSE 'U' + CONVERT(Varchar,T1.PK) END As MyIndicator
Lowell
July 5, 2013 at 12:53 pm
Lowell does it the way I'd do it if the data is static.
If it's not completely static, or may change often, then you could use another table.
create table status
( code varchar(20)
, value varchar(20)
);
go
insert Status select 'somethingaboutstudent', 'S';
insert Status select 'somethingaboutteacher', 'T';
go
select s.value + cast(a.somecol)
from mytable a
inner join status s
on a.someothercol = s.code
July 5, 2013 at 4:52 pm
sharonsql2013 (7/5/2013)
HiI declared the primary key as identity int.
Now everytime a value(code) is selected from the grid , I want a corresponding character to be concatenated to the primary key( integer) and stored in another column.
Example : if code = student then add "S" to "1" and store S1 to another column.
else if code = Teacher then add "T" to 2 and store T2.
How should I implement this logic?
You probably shouldn't. Storing such data is a violation of even first normal form. At best, creating a view, as already suggested, might be the better way to go but make sure you don't use the concatenated column in a lookup or it's likely the whole view will need to materialize before you can get an answer from it.
Why do you want/need to do this? What business process requires the concatenation?
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply