tsql - Concate Primary Keys in SQL -


i want concate primary keys of multiple tables in sql directly. used below query concate 3 primary keys hyphen between them sql skipped hyphen , sum primary keys , result in single value.

  select cid + '-' + rid + '-'+  cgid [idcombination] ...     

where cid , rid , cgid primary keys of 3 sql tables.

how skipped string part in query ?

any highly appreciated.

updated

for example : values of cid , rid , cgid 3 , 4, 3 respectively. should 3-4-3 result 10.

what happening? remember + means both addition , string concatenation. happens - can interpreted number (like -0), sql server prefers interpret + addition.

normally, when type of operation, separation character cannot interpreted number, , error. amused don't error in case.

one method explicitly cast values strings:

select cast(cid varchar(255)) + '-' + cast(rid +  varchar(255)) '-'+  cast(cgid  varchar(255)) [idcombination]  

in sql server 2012+, can more using concat():

select concat(cid, '-', rid, '-', 'cgid) [idcombination]  

concat() knows should string.


Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -