SQL CASE比较文本数据行和整数数据行

时间:2018-10-17 09:19:38

标签: mysql sql

在我的表中有一个数据行注释,它存储为文本数据类型。

我有Datarow idPersonalstamm,它存储为一个整数。

我的问题是,有时会发生来自Personalstamm的数据被复制到注释中的情况。

所以我做了一个case语句,该语句应将两者都强制转换为varchar,然后进行比较,如果comment等于personalstamm,则应将comment设置为Null,否则应提供该评论。

但是它总是转到else语句。我的错是什么?如何以其他方式实现目标?

整个过程是Powershell脚本的一部分,该脚本每周一次从访问者终端提取此查询。

> My output looks like this  
> Nr. id. Comment.  
>1---2----2  
> 2---3---some text  
> 3---4---

> it should look like this   
>Nr. id. Comment.   
>1---2---  
>2---3---some text  
>3---4---



<pre><code>SELECT ROW_NUMBER() OVER(ORDER BY vb.Arrival ASC) AS Nr,
       p.idPersonalstamm,
       CASE vr.rating 
          WHEN 3 THEN 'Sehr Gut' 
          WHEN 2 THEN 'Gut' 
          WHEN 1 THEN 'Nicht so Gut' 
          ELSE 'Sonstiges' 
       END AS Bewertung, 
       <b>Case 
          WHEN CAST(vr.Comment as varchar (20)) = CAST(p.idPersonalstamm as varchar (20)) Then null 
          ELSE vr.Comment 
        END as Comment,</b>
       vb.Arrival,
       vb.Departure,
       p.Name,
       p.FirstName,
       p.Company,
       p.Stadt
From Personalstamm p 
  left join (
    select vr.*,
           row_number() over (partition by idPersonalstamm order by idPersonalstamm) as seqnum 
    from VisitRating vr
  ) vr on p.idPersonalstamm =  vr.idPersonalstamm 
  left join (
    select vb.*,
           row_number() over (partition by idVisitor order by idVisitor) as seqnum       
    from VisitorBooking vb
  ) vb on p.idPersonalstamm = vb.idVisitor and vb.seqnum = vr.seqnum
where p.idPersonalstamm is not null</code></pre>

1 个答案:

答案 0 :(得分:0)

您可以尝试:

Case 
      WHEN LTRIM(CAST(vr.Comment as varchar (20))) = CAST(p.idPersonalstamm as varchar (20)) Then null 
      ELSE vr.Comment 
    END as Comment

或:     案例WHEN CAST(p.idPersonalstamm为varchar(20))如'%'+ CAST(vr.Comment为varchar(20))+'%'然后为null           ELSE vr.Comment         结束作为评论