Select 字符串范围之间的数据
Select data between string range
我有一个 table Employee
,
_________________________________
Id | name | salary
______________________________
1 | John | [1300 - 2000]
_______________________________
2 | Aby | [600 - 1000]
________________________________
3 | Mike | [1000 - 1500]
工资栏是Nvarchar
我想要 SQL 中的查询/函数/SP,如果我搜索 1400,输出应该如下所示
Id | name | salary
________________________________
1 | John | [1300 - 2000]
_______________________________
3 | Mike | [1000 - 1500]
提前致谢。
您不应将范围存储在一列中。但如果你不能改变它,那就是解决方案:
SELECT [Id] , name,
,[salary]
FROM [Test Database].[dbo].[test] where
1700 >= RTRIM(LTRIM(SUBSTRING(salary,0, CHARINDEX('-',salary))))
and 1700 <= RTRIM(LTRIM(SUBSTRING(salary, CHARINDEX('-', salary) + 1, LEN(salary))))
您需要提取数值,然后转换为数值类型:
declare @mySalary money;
set @mySalary = 1400;
with Employee(ID, Name, Salary ) as
(
select 1,'John','[1300 - 2000]' union all
select 2,'Aby','[600 - 1000]' union all
select 3,'Mike','[1000 - 1500]'
), e2 as
(
select SUBSTRING(Salary,PATINDEX('%[0-9]%', Salary),CHARINDEX('-',Salary)-2) as Salary1,
SUBSTRING(Salary,CHARINDEX('-',Salary)+1,CHARINDEX(']',Salary)-CHARINDEX('-',Salary)-1) as Salary2,
e.*
from Employee e
)
select ID, Name, Salary
from e2
where @mySalary between cast(Salary1 as money) and cast(Salary2 as money);
ID Name Salary
1 John [1300 - 2000]
3 Mike [1000 - 1500]
试试这个查询:
declare @tbl table (id int, name varchar(15), salary varchar(20));
declare @mySalary int = 1400;
insert into @tbl
select 1,'John','[1300 - 2000]' union all
select 2,'Aby','[600 - 1000]' union all
select 3,'Mike','[1000 - 1500]'
select id, name, salary from (
select id, name, salary,
convert(int, substring(salary, openBrcktIdx + 1, hyphenIdx - openBrcktIdx - 2)) lowerBound,
convert(int, substring(salary, hyphenIdx + 2, closeBrcktIdx - hyphenIdx - 2)) upperBound
from (
select *,
charindex('-', salary) hyphenIdx,
charindex('[', salary) openBrcktIdx,
charindex(']', salary) closeBrcktIdx
from @tbl
) t
) t where @mySalary between lowerBound and upperBound
声明@var1 int =1400
select * 来自
(select *,replace(SUBSTRING(salary,0,CHARINDEX('-',salary,0)),'[','') as Splitted1,
替换(SUBSTRING(薪水,CHARINDEX('-',薪水,0)+ 1,len(薪水)-1),']','')作为Splitted2
来自员工
) 作为 t1
其中@var1>=Splitted1 和@var1<=Splitted2
我有一个 table Employee
,
_________________________________
Id | name | salary
______________________________
1 | John | [1300 - 2000]
_______________________________
2 | Aby | [600 - 1000]
________________________________
3 | Mike | [1000 - 1500]
工资栏是Nvarchar
我想要 SQL 中的查询/函数/SP,如果我搜索 1400,输出应该如下所示
Id | name | salary
________________________________
1 | John | [1300 - 2000]
_______________________________
3 | Mike | [1000 - 1500]
提前致谢。
您不应将范围存储在一列中。但如果你不能改变它,那就是解决方案:
SELECT [Id] , name,
,[salary]
FROM [Test Database].[dbo].[test] where
1700 >= RTRIM(LTRIM(SUBSTRING(salary,0, CHARINDEX('-',salary))))
and 1700 <= RTRIM(LTRIM(SUBSTRING(salary, CHARINDEX('-', salary) + 1, LEN(salary))))
您需要提取数值,然后转换为数值类型:
declare @mySalary money;
set @mySalary = 1400;
with Employee(ID, Name, Salary ) as
(
select 1,'John','[1300 - 2000]' union all
select 2,'Aby','[600 - 1000]' union all
select 3,'Mike','[1000 - 1500]'
), e2 as
(
select SUBSTRING(Salary,PATINDEX('%[0-9]%', Salary),CHARINDEX('-',Salary)-2) as Salary1,
SUBSTRING(Salary,CHARINDEX('-',Salary)+1,CHARINDEX(']',Salary)-CHARINDEX('-',Salary)-1) as Salary2,
e.*
from Employee e
)
select ID, Name, Salary
from e2
where @mySalary between cast(Salary1 as money) and cast(Salary2 as money);
ID Name Salary
1 John [1300 - 2000]
3 Mike [1000 - 1500]
试试这个查询:
declare @tbl table (id int, name varchar(15), salary varchar(20));
declare @mySalary int = 1400;
insert into @tbl
select 1,'John','[1300 - 2000]' union all
select 2,'Aby','[600 - 1000]' union all
select 3,'Mike','[1000 - 1500]'
select id, name, salary from (
select id, name, salary,
convert(int, substring(salary, openBrcktIdx + 1, hyphenIdx - openBrcktIdx - 2)) lowerBound,
convert(int, substring(salary, hyphenIdx + 2, closeBrcktIdx - hyphenIdx - 2)) upperBound
from (
select *,
charindex('-', salary) hyphenIdx,
charindex('[', salary) openBrcktIdx,
charindex(']', salary) closeBrcktIdx
from @tbl
) t
) t where @mySalary between lowerBound and upperBound
声明@var1 int =1400
select * 来自 (select *,replace(SUBSTRING(salary,0,CHARINDEX('-',salary,0)),'[','') as Splitted1, 替换(SUBSTRING(薪水,CHARINDEX('-',薪水,0)+ 1,len(薪水)-1),']','')作为Splitted2 来自员工 ) 作为 t1 其中@var1>=Splitted1 和@var1<=Splitted2