mardi 21 août 2018

Pattern or regular expression in Access

In MS Access I have the following query to remove all the 0's of the left of the column [IDENTIFIER_1], but I have to define each case (If have 10 zeros on the left then make a substring from the 11 character to the end, If have 9 zeros on the left then make a substring from the 10 character to the end and so on....)

The query I have is:

SELECT YEAR(a.[First Date]) AS [N Year], Month(a.[First Date]) AS [N Month],

  IIF(    inStr(a.[IDENTIFIER_1],  '0000000000')=1, MID(a.[IDENTIFIER_1],11,LEN(a.[IDENTIFIER_1])) ,        

  IIF(    inStr(a.[IDENTIFIER_1],  '000000000')=1, MID(a.[IDENTIFIER_1],10,LEN(a.[IDENTIFIER_1])) , 

  IIF(    inStr(a.[IDENTIFIER_1],  '00000000')=1, MID(a.[IDENTIFIER_1],9,LEN(a.[IDENTIFIER_1])) , 

  IIF(    inStr(a.[IDENTIFIER_1],  '0000000')=1, MID(a.[IDENTIFIER_1],8,LEN(a.[IDENTIFIER_1])) , 

  IIF(    inStr(a.[IDENTIFIER_1],  '000000')=1, MID(a.[IDENTIFIER_1],7,LEN(a.[IDENTIFIER_1])) ,     


 IIF(    inStr(a.[IDENTIFIER_1],  '00000')=1, MID(a.[IDENTIFIER_1],6,LEN(a.[IDENTIFIER_1])) , 


 IIF(    inStr(a.[IDENTIFIER_1],  '0000')=1, MID(a.[IDENTIFIER_1],5,LEN(a.[IDENTIFIER_1])) , 

 IIF(    inStr(a.[IDENTIFIER_1],  '000')=1, MID(a.[IDENTIFIER_1],4,LEN(a.[IDENTIFIER_1])) ,   


 IIF(    inStr(a.[IDENTIFIER_1],  '00')=1, MID(a.[IDENTIFIER_1],3,LEN(a.[IDENTIFIER_1])) ,


 IIF(    inStr(a.[IDENTIFIER_1],  '0')=1, MID(a.[IDENTIFIER_1],2,LEN(a.[IDENTIFIER_1])) , a.[IDENTIFIER_1]


    )    )    )    )    )    )    )    )    )    ) 

    FROM Table1 as a;

but I wonder if there is a form like in java that you can make some pattern or regular expression, avoiding all that code. Something like a Pattern [0]+ or something that replace that code. I also try to do something like this: like '[0]+[1-9]' but I couldn't find a way to make it work.

Aucun commentaire:

Enregistrer un commentaire