A community member has associated this post with a similar question:
Azure Data Studio: Querying information based on the imported flat file.

Only moderators can edit this content.

SQL Query that looks for ID number

Sherlan Emmanuel Burgonia 86 Reputation points
2022-09-20T23:42:57.64+00:00

I was given a list of ID in string format and extract information from the database based on the ID number. However, these IDs in the database start with either '001' or '002'. Initially, I'm using WHERE ID IN ('0016085','0026085') to find it but I think there's a more efficient way instead of hardcoding both scenarios.

243207-image.png

TABLE
243223-image.png

Azure SQL Database
Developer technologies Transact-SQL
SQL Server Other
{count} votes

3 answers

Sort by: Most helpful
  1. LiHongMSFT-4306 31,566 Reputation points
    2022-09-21T02:39:41.097+00:00

    Hi @Sherlan Emmanuel Burgonia
    You could use RIGHT or SUBSTRING functions to ignore first three digits. Check the sample below:

    CREATE TABLE #TABLE(ID VARCHAR(20))  
    INSERT INTO #TABLE VALUES  
    ('00160885'),('00260885'),('00133263'),('00233263')  
      
    SELECT ID  
    FROM #TABLE  
    WHERE RIGHT(ID,LEN(ID)-3) = 60885  
      
    SELECT ID  
    FROM #TABLE  
    WHERE SUBSTRING(ID,4,LEN(ID)) = 60885  
    

    Best regards,
    LiHong


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments
  2. Jingyang Li 5,896 Reputation points Volunteer Moderator
    2022-09-21T03:23:41.677+00:00

    The right thing is to learn the basic about data types and choose proper data type for your table columns. Don't start with manipulate your data and extract numbers from string type of data.

    0 comments No comments
  3. ikslabuk 21 Reputation points
    2022-09-21T10:34:11.777+00:00
    WHERE ID LIKE '00[12]6085'  
    
    0 comments No comments