问题描述
我有一张存储产品的表格。下面是带有数据的架构。
我想要实现的是我将ProductId,Tenure和Range作为参数传递,它将选择匹配的记录。
因此,例如,如果我有参数 ProductId = 21
,Tenure=Duration=1
和Range =14678
,那么它应该给我Id=4
作为预期输出。因为范围是10001-15000。
解决方法
您可以将Updating filepond: bar
Updating filepond: bar
Updating filepond: foo
Updating filepond: bar
与parsename()
配合使用
示例
try_convert()
返回
Declare @YourTable Table ([ID] varchar(50),[ProductID] int,[Price_Range] varchar(50),[Duration] int)
Insert Into @YourTable Values
(1,21,'0 - 5000',1),(2,'5001 - 10000',(4,'10001 - 15000',(5,'15001 - 20000',1)
Select *
From @YourTable
Where ProductID = 21
and Duration = 1
and 14678 between try_convert(int,parsename(replace(Price_Range,'-','.'),2))
and try_convert(int,1))
注意:可能不需要ID ProductID Price_Range Duration
4 21 10001 - 15000 1
。不知道数据的全部范围,最好避免误报。
我认为将parsename()
用于此类目的是很棘手的-聪明但很棘手。 SQL Server确实应该提供适当的功能。
因此,我将使用:
14678 >= try_convert(int,left(price_range,charindex(' ',price_range))) and
14678 <= try_convert(int,stuff(price_range,1,charindex('-',price_range) + 1,''))