List all tables with a certain string

  • Hi,

    I want to list al tables that contains only _ar_ in the name (archive tables)

    I have tried the following script

    Select name from sys.tables where name like '%_ar_%'

    but when i get the result i also see tables like abc_yard_abc and i only want to have tables like abc_ar_abc

     

  • Your problem here is that the underscore character is itself a wildcard in T-SQL, matching any single character. That explains the results you are seeing.

    The solution is to ESCAPE the wildcard. Something (rather inelegant) like this (untested):

    Select name 
    from sys.tables
    where name like '%|_ar|_%' ESCAPE '|';

    • This reply was modified 3 years, 11 months ago by  Phil Parkin.

    The absence of evidence is not evidence of absence
    - Martin Rees
    The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
    - Phil Parkin

  • Thanks Phil it seems to work !

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply