Author |
Topic |
nagarjuna_a2006
Starting Member
16 Posts |
Posted - 2014-03-11 : 02:57:30
|
We have a Student table with the below columns and data in databaseSId SName SCourse123 Student1 Java124 Student2 DotNet125 Student3 C126 Student4 SQLi would like to add a character 'S' default for the SId column whenever i inserted a record in the tableoutput format as follows SId SName SCourseS123 Student1 JavaS124 Student2 DotNetS125 Student3 CS126 Student4 SQLPlease post the answersRegards,Nagarjuna |
|
stepson
Aged Yak Warrior
545 Posts |
Posted - 2014-03-11 : 03:08:55
|
[code];with aCTEAS (select 123 as SId,'Student1' as SName, 'Java' as SCourse union all select 124,'Student2','DotNet' union all select 125,'Student3','C' union all select 126,'Student4','SQL')SELECT 'S' + CAST(Sid as VARCHAR(30)) as [Sid] ,Sname ,ScourseFROM aCTE[/code]all the magic is here:[code]'S' + CAST(Sid as VARCHAR(30)) as [Sid] [/code]sabinWeb MCP |
|
|
stepson
Aged Yak Warrior
545 Posts |
Posted - 2014-03-11 : 03:09:33
|
outputSid Sname ScourseS123 Student1 JavaS124 Student2 DotNetS125 Student3 CS126 Student4 SQL sabinWeb MCP |
|
|
nagarjuna_a2006
Starting Member
16 Posts |
Posted - 2014-03-11 : 03:20:48
|
Thanks a lot for your posting.but i want to store the same thing in the database instead of showing in the result set. i will just pass only student id as 123 as shown in the below statementInsert Student values(123,'Student1','Java')in the table it should store as SId SName SCourseS123 Student1 Javado i need to change anything in the table definitionThanks in advanceRegards,Nagarjuna |
|
|
stepson
Aged Yak Warrior
545 Posts |
Posted - 2014-03-11 : 03:22:58
|
first of all, what is your table definition? the important field is Sid; what type is Insert Student values('S' + cast(123 as varchar(30)) ,'Student1','Java') sabinWeb MCP |
|
|
nagarjuna_a2006
Starting Member
16 Posts |
Posted - 2014-03-11 : 03:31:24
|
Please have the table definitionCreate Table Student(SId int Primary Key,SName Varchar(10),SCourse Varchar(15))Regards,Nagarjuna |
|
|
stepson
Aged Yak Warrior
545 Posts |
Posted - 2014-03-11 : 03:36:56
|
there are some discutions to do about this;-why to do need the 'S' character to be store (To store this char, you need to change the tbl definition to something like this [SId varchar(30) Primary Key],butit's not recomanded . You will have to also change the fields for the related table, that have FK to Sid (used in join)from my point , i will not change , the tbl definition, to store the character 'S'.sabinWeb MCP |
|
|
nagarjuna_a2006
Starting Member
16 Posts |
Posted - 2014-03-11 : 04:59:48
|
yeah, i don't have any problem for changing the table definition but i would like to know how this will be solved for this kind of requirements.. because this is the interview question which i have faced recently in one of the mnc company in top 10 companies.Thanks in advance.Regards,Nagarjuna |
|
|
remyo
Starting Member
6 Posts |
Posted - 2014-03-11 : 11:01:52
|
Maybe the correct answer is: You can do it but best is if you dont. You could create a extra field S + the primarykey.1 reason is that all your relationships in the DB are still viable2nd reason is that joining on integers is faster the joining on non integersIF it is absulutly necessaryCheck all the FKK's and note themCheck the datamodel if FKK have not been defined (ie relationsships to the current PK)Drop the FK constraintsCreate new table with varchar PKCOPY Data in new table including the S -> Or use select intoChange all fields found with FK constraintsChange all other fieldsRecreate FK constraints |
|
|
sqlsaga
Yak Posting Veteran
93 Posts |
Posted - 2014-03-11 : 11:10:36
|
if you change the datatype from INT to VARCHAR you will able to accomodate that change. It's because 123 is an integer but when you append 'S' to it its no longer an integer but a STRING. So you have to change the data type to VARCHAR. Also, after you change the datatype, you need to write the update statement as below.UPDATE tabelASET SID = 'S'+CONVERT(VARCHAR(10), SID) Visit www.sqlsaga.com for more t-sql snippets and BI related how to's. |
|
|
nagarjuna_a2006
Starting Member
16 Posts |
Posted - 2014-03-11 : 12:07:47
|
Thank you for giving reply.. thanks a lot..Regards,Nagarjuna |
|
|
|