sql case比较text datarow和int datarow

rlcwz9us  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(207)

在我的表中,有datarow注解,它存储为文本数据类型。
我有数据行idpersonalstamm,它存储为int。
我的问题是,有时来自personalstamm的数据会被复制到comment中。
所以我做了一个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>
lvjbypge

lvjbypge1#

你能试试吗:

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

或:case当cast(p.idpersonalstamm as varchar(20))类似“%”+cast(vr.comment as varchar(20))+“%”时,则为null,否则vr.comment将以注解结尾

相关问题