Is there anyone who could tell me why Microsoft decided that for some functions some of the parameters will allow values (as first parameter for DateAdd for example) as literals only when in similar cases other functions accept strings (Format)?
select getDate(), DateAdd( day, -1, getDate() ), Format( 0.1234, 'P2' )
----------------------- ----------------------- ------2019-08-19 13:08:12.900 2019-08-18 13:08:12.900 12.34%
(1 row affected)
This is just another case of SQL Server's inconsistency.
I, generally dislike constant literals as parameters. Why? Because these are a kind of eternal identities with no shape or form. You may have impression that these also exist in Oracle but these are probably system variables/constants which do have their names, datatypes an values (just like user variable).
I just gave it a second thought and it IS a problem. Let's take a look at the documentation for above mentioned DateAdd function. What does it say in bold? This:
"DATEADD does not accept user-defined variable equivalents for the datepart arguments."That's about it.
Let's imagine we have some kind of configuration table that's gonna be used in some kind of date operations, let's say in calculating next valid date. It could be easily used in such calculations as is but no, you'll be forced to case the whole formula repeating DateAdd multiple times.
No comments:
Post a Comment