Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My query as follows
SQL
select a.stud_name,
 BatchID = (select eid from BATCHID where bid = 'B10458' and b_activ = 'A'),
 Cid = (select top 1 certificate_no from certificate_detail where stud_id = a.stud_id      and active = 'A' and crsname = 'RPSCRB')
from student a, course_registration b, batch_course_registration c where
a.stud_id=b.stud_id and b.cr_bill_no=c.cr_bill_no 
and c.bcr_batch_id= 'B10458'
and b.cr_active='A' and a.stud_active<>'D'

When i run the above query output as follows
stud_name  Batchid      Cid
  Ram       RPSCRB/B01   1

i want to concatenate the Batchid and Cid and get the output as follows
stud_name  Batchid      Cid    Result
   Ram     RPSCRB/B01    1    RPSCRB/B01/1

from using above query how can i concatenate the Batchid and Cid.
Posted
Updated 18-Nov-15 23:51pm
v2

Use Query
SQL
With CTE(stud_name,BatchID,Cid) as
(
select a.stud_name,
 BatchID = (select eid from BATCHID where bid = 'B10458' and b_activ = 'A'),
 Cid = (select top 1 certificate_no from certificate_detail where stud_id = a.stud_id      and active = 'A' and crsname = 'RPSCRB')
from student a, course_registration b, batch_course_registration c where
a.stud_id=b.stud_id and b.cr_bill_no=c.cr_bill_no 
and c.bcr_batch_id= 'B10458'
and b.cr_active='A' and a.stud_active<>'D')
select stud_name,BatchID,Cid, BatchID+'/'+ Cid as result from CTE

If your column BatchID and Cid are not of nvarchar type the cast it to nvarchar like
SQL
Cast(BatchID as nvarchar)+'/'+Cast(Cid as nvarchar) as result
 
Share this answer
 
Try this
SQL
;WITH TEMPCTE(StudentName,BatchID,CID)
AS
(
    select a.stud_name,
            BatchID = (select eid from BATCHID where bid = 'B10458' and b_activ = 'A'),
            Cid = (select top 1 certificate_no from certificate_detail where stud_id =            a.stud_id      and active = 'A' and crsname = 'RPSCRB')
    from   student a, course_registration b, batch_course_registration c 
    where  a.stud_id=b.stud_id and b.cr_bill_no=c.cr_bill_no 
           and c.bcr_batch_id= 'B10458'
           and b.cr_active='A' and a.stud_active<>'D'
)

SELECT StudentName,BatchID,CID, Convert(varchar(20),BatchID)+'/'+Convert(varchar(10),CID)
FROM TEMPCTE
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900