ahmed elbarbary

ahmed elbarbary

  • NA
  • 1.6k
  • 281.4k

How to write query generate unique Id for concate both columns search

Feb 22 2021 4:50 PM

I work on sql server 2012 . I face issue I can't generate unique Id from merge or concave both columns

Search Parts and Company ID and generated Id must be on column Generated ID

to be easier and fast on search

so Please How to generate unique Id from both column Search Parts and Company ID
and if both column repeated value both get same Id
so please How to do that

AS Example

SearchParts CompanyId GeneratedId
A5ghf7598f7GGHYUTYA 3456 901
when concate both columns Search Parts and Company Id generate Id
and take same Id in case of repeated
but main idea generate number unique for both column search parts and company id

every search parts text and CompanyId must be unique and take unique Id on generated Id
 
  1. create table #partswithcompany  
  2.   (  
  3.   SearchParts  nvarchar(200),  
  4.   CompanyId  int,  
  5.   GeneratedId INT  
  6.   )  
  7.   insert into #partswithcompany (SearchParts,CompanyId,GeneratedId)  
  8.   values  
  9.   ('A5ghf7598fdmlcpghjk',1234,NULL),  
  10.   ('AKLJGSA7598fdmlcpghjk',5870,NULL),  
  11.   ('A5ghfJKKJGHHGghjk',9081818,NULL),  
  12.   ('KHJLFFS8fdmlcpghjk',123345,NULL),  
  13.   ('A5ghf7598f7GGHYUTYA',3456,NULL),  
  14.   ('A5ghfJKKJGHHGghjk',9081818,NULL),  
  15.    ('A5ghf7598f7GGHYUTYA',3456,NULL),  
  16.    ('A5ghf7598f7GGHYUTYA',3456,NULL)  
  17.       
  18.  Expected Result  
  19.       
  20.      SearchParts    CompanyId    GeneratedId  
  21.      A5ghf7598fdmlcpghjk    1234    5  
  22.      AKLJGSA7598fdmlcpghjk    5870    9  
  23.      A5ghfJKKJGHHGghjk    9081818    8  
  24.      KHJLFFS8fdmlcpghjk    123345    6  
  25.      A5ghf7598f7GGHYUTYA    3456    7  
  26.      A5ghfJKKJGHHGghjk    9081818    8  
  27.      A5ghf7598f7GGHYUTYA    3456    7  
  28.      A5ghf7598f7GGHYUTYA    3456    7

Answers (4)