如何在具有*变异的特定参数的SQL Server中查找数据对

时间:2021-07-03 09:09:57

I show table and what I need from SQL Server table with specific parameter with liberal variation from parameter column if parameter pass from variable.

如果参数从变量传递,我从SQL Server表中显示表和我需要的具有特定参数的参数列的*变异。

I don't know how can I achieve this result.

我不知道如何才能达到这个效果。

Filter Parameter:

过滤参数:

ParameterFilterB    -           
ParameterFilterC    -           
ParameterFilterD    -           
ParameterFilterE    -   B       

Table:

表:

A   B   C   D   E
------------------
1   A   A   A   A
2   A   A   A   B
3   A   B   A   C
4   A   B   A   D

Result :

结果:

A   B   C   D   E
------------------
1   A   A   A   A
2   A   A   A   B
3   A   B   A   C

Please help me and thanks in advance.

请帮助我,并提前感谢。

1 个解决方案

#1


0  

You need to use ASCII and CHAR functions.

您需要使用ASCII和CHAR函数。

You may also want to check for minimum/maximum allowed characters e.g. what happens when someone enters A or Z

您可能还想检查允许的最小/最大字符数,例如当有人进入A或Z时会发生什么

For ParameterFilterB the code would look like this:

对于ParameterFilterB,代码如下所示:

DECLARE @ParameterFilterBMin CHAR( 1 ), @ParameterFilterBMax CHAR( 1 )

-- Set first search letter
SET @ParameterFilterBMin =
    CASE
        -- Handle edge case
        WHEN @ParameterFilterB = 'A' THEN 'A'
        -- Get previous letter
        ELSE CHAR( ASCII( @ParameterFilterB ) - 1 )
    END

-- Get last search letter
SET @ParameterFilterBMax =
    CASE
        -- Handle edge case
        WHEN @ParameterFilterB = 'Z' THEN 'Z'
        -- Get next letter
        ELSE CHAR( ASCII( @ParameterFilterB ) + 1 )
    END

-- Find matching values
SELECT *
FROM Table
WHERE colB BETWEEN @ParameterFilterBMin AND @ParameterFilterBMax
    -- AND/OR colC BETWEEN @ParameterFilterCMin AND @ParameterFilterCMax
    -- etc.

You should be able to replicate this for the other parameters

您应该能够为其他参数复制此内容

#1


0  

You need to use ASCII and CHAR functions.

您需要使用ASCII和CHAR函数。

You may also want to check for minimum/maximum allowed characters e.g. what happens when someone enters A or Z

您可能还想检查允许的最小/最大字符数,例如当有人进入A或Z时会发生什么

For ParameterFilterB the code would look like this:

对于ParameterFilterB,代码如下所示:

DECLARE @ParameterFilterBMin CHAR( 1 ), @ParameterFilterBMax CHAR( 1 )

-- Set first search letter
SET @ParameterFilterBMin =
    CASE
        -- Handle edge case
        WHEN @ParameterFilterB = 'A' THEN 'A'
        -- Get previous letter
        ELSE CHAR( ASCII( @ParameterFilterB ) - 1 )
    END

-- Get last search letter
SET @ParameterFilterBMax =
    CASE
        -- Handle edge case
        WHEN @ParameterFilterB = 'Z' THEN 'Z'
        -- Get next letter
        ELSE CHAR( ASCII( @ParameterFilterB ) + 1 )
    END

-- Find matching values
SELECT *
FROM Table
WHERE colB BETWEEN @ParameterFilterBMin AND @ParameterFilterBMax
    -- AND/OR colC BETWEEN @ParameterFilterCMin AND @ParameterFilterCMax
    -- etc.

You should be able to replicate this for the other parameters

您应该能够为其他参数复制此内容