Select query to generate 8 digit alphanumeric key in SQL Server

SajidHussa
 
on Aug 13, 2021 06:05 AM
345 Views

i create a table tbl_channelpartner 

where cpcode is 8 digit 

my aim is when i generate guid 8 digit first it check that it not exists in cpcode if exist then create new one

purpose is uniqnes between cpcode it is primary key in my case 

i write this query but it not work 

SELECT SUBSTRING(CONVERT(VARCHAR(255), NEWID()),0,9) where NOT IN ( select cpcode from tbl_channelpartner)

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Aug 13, 2021 06:49 AM

Use below query.

SELECT SUBSTRING(CONVERT(VARCHAR(255), NEWID()),0,9) WHERE SUBSTRING(CONVERT(VARCHAR(255), NEWID()),0,9) NOT IN (select cpcode from tbl_channelpartner)