Search code examples
sqlsql-server-2005-express

SQL scalar variable value between and


'Names' in table 'Data'

"type12pen105A"
"type12pen110A"
"type12pen121B"

Declare @n int;

select Names From Data
where Names ='type12pen'+cast(@n between 100 and 110 as varchar)+'A'

My Required out put is

"type12pen105A"
"type12pen110A"

Solution

  • Based on the info you provided, this is ugly but it should work:

    create table #data
    (
        names varchar(50)
    )
    
    insert into #data values('type12pen105A')
    insert into #data values('type12pen101A')
    insert into #data values('type12pen112A')
    insert into #data values('type12pen120A')
    insert into #data values('type12pen110A')
    insert into #data values('type12pen106A')
    insert into #data values('type12pen110C')
    insert into #data values('type12pen110D')
    insert into #data values('type12pen110E')
    insert into #data values('type12pen121B')
    
    SELECT Names
    FROM #Data
    WHERE Names LIKE 'type12pen%' 
    AND RIGHT(Names,1) = 'A'
    AND replace(replace(names, 'type12pen', ''), 'A', '') BETWEEN 100 AND 110 
    
    drop table #data
    

    results:

    type12pen105A
    type12pen101A
    type12pen110A
    type12pen106A