Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL-CASE WHEN THEN ELSE nothing_Sql_Case - Fatal编程技术网

SQL-CASE WHEN THEN ELSE nothing

SQL-CASE WHEN THEN ELSE nothing,sql,case,Sql,Case,我已经写了以下内容,但是在下面的****16之后,我想通过允许所有其他结果返回原来的状态来结束案例陈述,不做任何事情,而不是返回null EG原始数据 1 2 3 4 7 10 16 EXAMPLE1 EXAMPLE2 EXAMPLE3 使用下面的代码我将得到的结果 User Security ID User Name Full Name State Windows Security ID License Type Application ID NULL NULL NULL 我想要的结果 U

我已经写了以下内容,但是在下面的****16之后,我想通过允许所有其他结果返回原来的状态来结束案例陈述,不做任何事情,而不是返回null

EG原始数据

1
2
3
4
7
10
16
EXAMPLE1
EXAMPLE2
EXAMPLE3
使用下面的代码我将得到的结果

User Security ID
User Name
Full Name
State
Windows Security ID
License Type
Application ID
NULL
NULL
NULL
我想要的结果

User Security ID
User Name
Full Name
State
Windows Security ID
License Type
Application ID
EXAMPLE1
EXAMPLE2
EXAMPLE3
查询:

SELECT 
 
[Date and Time] as [Date],
[User ID] as [User Performing Change],
[Type of Change],
CASE [Field No_]

WHEN 1 THEN 'User Security ID'
WHEN 2 THEN 'User Name'
WHEN 3 THEN 'Full Name'
WHEN 4 THEN 'State'
WHEN 7 THEN 'Windows Security ID'
WHEN 10 THEN 'License Type'
**WHEN 16 THEN 'Application ID'

END as [Data],**
[Old Value],
[New Value],
[Record ID]
FROM [DB].[dbo].[TABLE$Change Log Entry]
where [Table No_] = 20000
order by [Field No_]
使用
else

CASE [Field No_]
    WHEN '1'  THEN 'User Security ID'
    WHEN '2'  THEN 'User Name'
    WHEN '3'  THEN 'Full Name'
    WHEN '4'  THEN 'State'
    WHEN '7'  THEN 'Windows Security ID'
    WHEN '10' THEN 'License Type'
    WHEN '16' THEN 'Application ID'
    ELSE [Field No_]
END as [Data]
请注意,我将要比较的值从文字数字更改为文字字符串,因为您的列似乎是字符串数据类型

编辑

如果列是数字数据类型,则:

CASE [Field No_]
    WHEN  1 THEN 'User Security ID'
    WHEN  2 THEN 'User Name'
    WHEN  3 THEN 'Full Name'
    WHEN  4 THEN 'State'
    WHEN  7 THEN 'Windows Security ID'
    WHEN 10 THEN 'License Type'
    WHEN 16 THEN 'Application ID'
    ELSE CONVERT(VARCHAR(MAX), [Field No_])
END as [Data]

我现在似乎面临以下错误:将varchar值“User Security ID”转换为数据类型int时,转换失败。@ChristopherBrogan:好的,因此您有一个整数列。请参阅我的编辑。