【问题标题】:Equivalent? No, but why?相等的?不,但为什么?
【发布时间】:2010-10-01 07:26:36
【问题描述】:

T-SQL:

1(ANSI):convert(varchar(10),@DueDate, 102) < convert(varchar(10),getdate(), 102)

2(美国):convert(varchar(10),@DueDate, 101) < convert(varchar(10),getdate(), 101)

请注意,当考虑年份时,这些将返回不同的结果。

为什么?

有什么区别?为什么运营商在使用#2时不考虑年份?

【问题讨论】:

    标签: sql tsql date


    【解决方案1】:

    你想做什么?你在那里比较varchars。查看这些语句的输出:

    print convert(varchar(10), getdate(), 101)
    print convert(varchar(10), getdate(), 102)
    

    打印这个:

    01/14/2009
    2009.01.14
    

    比较第一个表格实际上只是检查一个日期的月份是否小于当前日期的月份。

    是否有理由需要将日期转换为 varchar?为什么不直接比较呢?

    @DueDate < getdate()
    

    【讨论】:

      猜你喜欢
      • 2023-04-05
      • 2016-03-22
      • 1970-01-01
      • 1970-01-01
      • 2013-08-18
      • 2013-10-25
      • 1970-01-01
      • 2013-02-10
      • 1970-01-01
      相关资源
      最近更新 更多