Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
SQL
prefix          srno   
TS-SIV-VS-	1
TS-SIV-VS-	4
TS-SIV-VS-	2


i want to combine both column into one column like in below format

SQL
prefix          srno   invoice
TS-SIV-VS-	1       TS-SIV-VS-1   
TS-SIV-VS-	4       TS-SIV-VS-4
TS-SIV-VS-	2       TS-SIV-VS-2
Posted
Updated 2-Apr-18 21:09pm
v2
Comments
_Asif_ 13-May-15 8:47am    
Are you using MS SQL?
King Fisher 13-May-15 8:58am    
Re-design your Table?

1. Add column using ALTER TABLE [table] ADD COLUMN [column]
2. Update new column with combined values
 
Share this answer
 
Comments
Member 11337367 13-May-15 8:48am    
update sal set invoice=prefix+srno
but this is giving error...

Msg 8114, Level 16, State 5, Line 1
Error converting data type nvarchar to float.
Kornfeld Eliyahu Peter 13-May-15 8:52am    
When you are combining different types of columns you have to convert them to something common, otherwise SQL will do it - like in your case...
In your case srno is the last element of the expression so SQL will try to convert all the other elements to the type of rsno - float...It is impossible to convert the value of prefix to float!!!
Try: update sal set invoice = prefix + cast(srno as nvarchar)
Another approach could be like

CREATE TABLE TEST
(
    prefix VARCHAR(10),
    SRNO   INT ,
    INVOICE AS prefix + CAST(SRNO AS VARCHAR(10))
);

INSERT INTO TEST(prefix, SRNO)
SELECT 'TS-SIV-VS-',	1
UNION ALL
SELECT 'TS-SIV-VS-',	2
UNION ALL
SELECT 'TS-SIV-VS-',	3

SELECT * FROM TEST
 
Share this answer
 
SQL
select prefix,srno,prefix+ CAST(srnoAS VARCHAR(10)) as invoice into TableName from
(
SELECT 'TS-SIV-VS-' as  a,    1 as b
UNION ALL
SELECT 'TS-SIV-VS-',    2
UNION ALL
SELECT 'TS-SIV-VS-',    3
)  t
 
Share this answer
 
v2
Are you asking for creating third column in the table ?
or If you want only select query to view the result, you can use this following query.

SQL
WITH CTE as
(
select 'TS-SIV-VS-' as Prefix, 1 as srno
union all
select 'TS-SIV-VS-' , 4
union all
select 'TS-SIV-VS-' , 1
)

select Prefix,Srno,prefix+cast(srno as nvarchar(2)) Invoice from cte
 
Share this answer
 
v2

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