Search code examples
sqljoinsql-like

SQL SELECTING * then select values from columns that end with


I have a following code;

Select * from test left join testtwo on test.testid = testtwo.id

I then need to select values from another column that is called 'code' from 'testtwo' and the values end with '100' ('%100')

I tried the following code but it didn't work:

Select * from test left join testtwo on test.testid = testtwo.id
union
SELECT * FROM testtwo
WHERE code LIKE '100%';

id    testid   code
1     1       0001100
2     2       0002100
3     3       0003100
4     4       0004100

Solution

  • for a string column where the values end with 100 you should use

     WHERE code LIKE '%100'; 
    

    Looking to your sample you could use

    Select * 
    from test 
    INNER  join testtwo on test.testid = testtwo.id
    WHERE code LIKE '%100';
    

    If you want also '%400' you could use an OR condition

    Select * 
    from test 
    INNER  join testtwo on test.testid = testtwo.id
    WHERE code LIKE '%100' 
    OR code LIKE '%400' ;
    

    or use a union

    Select * 
    from test 
    INNER  join testtwo on test.testid = testtwo.id
    WHERE code LIKE '%100' 
    union 
    Select * 
    from test 
    INNER  join testtwo on test.testid = testtwo.id
    WHERE code LIKE '%400'