Active Directory: Convert canonicalName node value from string to integer

后端 未结 1 2081
粉色の甜心
粉色の甜心 2021-01-20 20:42

Are there any methods available to convert the string text value contained within the AD canonicalName attribute to an incremented integer value? Or, does this need to be pe

相关标签:
1条回答
  • 2021-01-20 20:55

    This is where my thinking takes me

    I gave you 9 levels, but the pattern is easy to see and expand

    Without a proper sequence I defaulted to alphabetical by node.

    It also supports multiple root nodes as well

    Example

    Select A.*
          ,Nodes = concat('/',dense_rank() over (Order By N1),'/'
                  ,left(nullif(dense_rank() over (Partition By N1                      Order By N2)-1,0),5)+'/'
                  ,left(nullif(dense_rank() over (Partition By N1,N2                   Order By N3)-1,0),5)+'/'
                  ,left(nullif(dense_rank() over (Partition By N1,N2,N3                Order By N4)-1,0),5)+'/'
                  ,left(nullif(dense_rank() over (Partition By N1,N2,N3,N4             Order By N5)-1,0),5)+'/'
                  ,left(nullif(dense_rank() over (Partition By N1,N2,N3,N4,N5          Order By N6)-1,0),5)+'/'
                  ,left(nullif(dense_rank() over (Partition By N1,N2,N3,N4,N5,N6       Order By N7)-1,0),5)+'/'
                  ,left(nullif(dense_rank() over (Partition By N1,N2,N3,N4,N5,N6,N7    Order By N8)-1,0),5)+'/'
                  ,left(nullif(dense_rank() over (Partition By N1,N2,N3,N4,N5,N6,N7,N8 Order By N9)-1,0),5)+'/'
                  )
     From  YourTable A
     Cross Apply (
                    Select N1 = ltrim(rtrim(xDim.value('/x[1]','varchar(max)')))
                          ,N2 = ltrim(rtrim(xDim.value('/x[2]','varchar(max)')))
                          ,N3 = ltrim(rtrim(xDim.value('/x[3]','varchar(max)')))
                          ,N4 = ltrim(rtrim(xDim.value('/x[4]','varchar(max)')))
                          ,N5 = ltrim(rtrim(xDim.value('/x[5]','varchar(max)')))
                          ,N6 = ltrim(rtrim(xDim.value('/x[6]','varchar(max)')))
                          ,N7 = ltrim(rtrim(xDim.value('/x[7]','varchar(max)')))
                          ,N8 = ltrim(rtrim(xDim.value('/x[8]','varchar(max)')))
                          ,N9 = ltrim(rtrim(xDim.value('/x[9]','varchar(max)')))
                    From  (Select Cast('<x>' + replace((Select replace(stuff([canonicalName],1,1,''),'\','§§Split§§') as [*] For XML Path('')),'§§Split§§','</x><x>')+'</x>' as xml) as xDim) as A 
                 ) B
     Order By 1
    

    Returns

    canonicalName                       Nodes
    \domain.com\                        /1/
    \domain.com\Corporate               /1/1/
    \domain.com\Corporate\Accounting    /1/1/1/
    \domain.com\Corporate\Hr            /1/1/2/
    \domain.com\Security\               /1/2/
    \domain.com\Security\Administrative /1/2/1/
    \domain.com\Security\Executive      /1/2/2/
    \domain.com\Security\Servers        /1/2/3/
    \domain.com\Users\                  /1/3/
    \domain.com\Users\Sales             /1/3/1/
    \domain.com\Users\Whatever          /1/3/2/
    
    0 讨论(0)
提交回复
热议问题