【问题标题】:How to compare 2 columns values in Sql Server table [duplicate]如何比较Sql Server表中的2列值[重复]
【发布时间】:2023-04-05 16:43:01
【问题描述】:

我在 sql server 中有 3 列的下表。我想将 [yearmonth] 与 [year] 和 [month] 列的串联进行比较。比如... select count(*) from table where [yearmonth]=concat([year],[month])。

在这里,当我们有一个单位数的 [month] 值时,我们必须在其中添加 0 作为前缀。就像第一条记录一样,在比较时它应该变成 05 而不是 5。所以 select count(*) from table where [yearmonth]=concat([year],[month]) 的最终计数应该是 3

[年月]的数据类型是nvarchar

[年份]的数据类型是nvarchar

[month] 的数据类型是 int

【问题讨论】:

  • 您的实际问题是什么?如何从int20215 中获取'202105'
  • 嗨@Larnu..请检查线程中的以下解决方案..这就是我想要的..感谢您的调查。
  • 列有哪些类型?如果他们是int,那么你可以做year * 100 + month

标签: sql sql-server


【解决方案1】:

使用以下代码:

SELECT COUNT(*) FROM table 
WHERE [yearmonth]= CONCAT([year], RIGHT('00' + CAST([month] AS VARCHAR(2)), 2))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-19
    • 1970-01-01
    • 2011-12-17
    • 1970-01-01
    • 2010-11-27
    • 2019-09-14
    • 1970-01-01
    • 2013-10-11
    相关资源
    最近更新 更多