Select a range name – 'DisplayName' from a given table if it exist in the range, defined in table for a given number

huangapple go评论65阅读模式
英文:

Select a range name - 'DisplayName' from a given table if it exist in the range, defined in table for a given number

问题

以下是翻译好的部分:

给定的临时表结构如下所示:

CREATE TABLE #TempTable (
    DisplayName VARCHAR(50),
    Value1 VARCHAR(50),
    Value2 VARCHAR(50)
)

INSERT INTO #TempTable
VALUES
('569-580','569','580')
('581-592','581','592')
('593-607','593','607')
('608-620','608','620')

SELECT * FROM #TempTable

如果用户提供一个数字,我需要获取DisplayName,然后查看该数字是否存在于这个范围内,如下所示:

如果给定的数字是606,则输出应为:
'593-607'

如果给定的数字是608,则输出应为:
'608-620'

英文:

For a given temp table structure is as given below:

CREATE TABLE #TempTable (
    DisplayName VARCHAR(50),
    Value1 VARCHAR(50),
    Value2 VARCHAR(50)
)

INSERT INTO #TempTable
VALUES
('569-580','569','580')
('581-592','581','592')
('593-607','593','607')
('608-620','608','620')

SELECT * FROM #TempTable

I need get DisplayName if user provide a number then that number exists in that range like<br>
if given number is 606 then output should be:
'593-607'<br>
if given number is 608 then output should be:
'608-620'

答案1

得分: 0

DECLARE @GivenNumber INT = 606
SELECT DisplayName
FROM #TempTable1
WHERE @GivenNumber BETWEEN CAST(Value1 AS INT) AND CAST(Value2 AS INT)
英文:
DECLARE @GivenNumber INT = 606
SELECT DisplayName
FROM #TempTable1
WHERE @GivenNumber BETWEEN CAST(Value1 AS INT) AND CAST(Value2 AS INT)

huangapple
  • 本文由 发表于 2023年6月19日 11:25:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76503439.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定