sql - Shuffling numbers based on the numbers from the row -


let's have 12-digit numbers in given row.

accountnumber ============= 136854775807 293910210121 763781239182 

is possible shuffle numbers of single row solely based on numbers of row? e.g. 136854775807 become 573145887067

i have created user-defined function shuffle numbers.

what have done is, taken out each character , stored table variable along random number. @ last concatenated each character in ascending order of random number.

it not possible use rand function inside user-defined function. created view taking random number.

view : random_num

create view dbo.[random_num] select floor(rand()* 12) [rnd]; 

it's not necessary random number should between 0 , 12. can give larger number instead of 12.

user-defined function : fn_shuffle

create function dbo.[fn_shuffle](     @acc varchar(12) ) returns varchar(12) begin      declare @tbl table([a] varchar(1), [b] int);     declare @i int = 1;     declare @l int;     set @l = (select len(@acc));      while(@i <= @l)     begin          insert @tbl([a], [b])         select substring(@acc, @i, 1), [rnd] [random_num]         set @i += 1;      end      declare @res varchar(12);     select @res = stuff((             select '' + [a]               @tbl             order [b], [a]             xml path('')         )         , 1, 0, ''     );      return @res;  end 

then, able use function below.

select [acc_no],  dbo.[fn_shuffle]([acc_no]) [shuffled] dbo.[your_table_name]; 

find demo here


Comments

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

php - How to remove letter in front of the word laravel -

minify - Minimizing css files -