Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Add Variable to integer


Add Variable to integer

Author
Message
sharonsql2013
sharonsql2013
SSC-Addicted
SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)

Group: General Forum Members
Points: 406 Visits: 1220
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?
Lowell
Lowell
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14973 Visits: 38992
sharonsql2013 (7/5/2013)
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?


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

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

Steve Jones
Steve Jones
SSC-Dedicated
SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)

Group: Administrators
Points: 36282 Visits: 18752
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



Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45273 Visits: 39934
sharonsql2013 (7/5/2013)
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?


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

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search