SQL Dynamic DatePart when using DateDiff

前端 未结 6 2096
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-10 01:07

Is there a way to pass the DatePart parameter of DateDiff as a variable? So that I can write code that is similar to this?

DECLARE @datePart VARCHAR(2)
DECLA         


        
6条回答
  •  再見小時候
    2020-12-10 01:53

    I don't think there are better ways then you describe. Sql Server probably compiles the DATEDIFF query to a set of operations that depend on the datepart parameter. So you'd need a CASE, or dynamic queries.

提交回复
热议问题