Question

'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"
Was it helpful?

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

OTHER TIPS

SELECT Names
FROM Data
WHERE Names LIKE 'type12pen%' 
AND CAST(SUBSTRING(Names,10,3) AS INT) BETWEEN 100 AND 110
AND RIGHT(Names,1) = 'A'
declare @Data table(
  name varchar(32)
)

insert into @Data values('type12pen105A')
insert into @Data values('type12pen110A')
insert into @Data values('type12pen121B')
insert into @Data values('book11jil124C')


select name
from @Data
where cast(substring(name, 10, 3) as int) between 100 and 110
      and name like 'type12pen%'
      and right(name, 1) = 'A'

If this is a large table you'd probably be better served by running a process on the data and splitting the different aspects of the product name out into individual fields and querying on those. Using substring and right means you won't get the benefit of indexes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top