I want to get the previous month relative to the current date
SELECT Datiff (mm, -1.2-2-2011)
This query gives 67 , which is the wrong value .. where did I go wrong?
You can use DATEADD
eg.
SELECT DATEADD(month, -1, GETDATE())
This is 2-2-2011not a valid date literal - you subtract 2 from 2, and then 2011 from the result - the corresponding date literals '2-2-2011'and #2-2-2011#. You can use GETDATE()to get the current date instead of relying on a literal.
2-2-2011
'2-2-2011'
#2-2-2011#
GETDATE()
And you should not use DATEDIFF- it gives you the difference between dates.
DATEDIFF
DATEADD .
DATEADD
:
SELECT DATEADD(mm,-1, GETDATE())
.
, DATEPART:
DATEPART
SELECT DATEPART(mm, SELECT DATEADD(mm,-1, GETDATE()))
SELECT datepart(mm, dateadd(mm,-1,'2011/1/1') )
, , SELECT MONTH(DATEADD(mm, -1, GETDATE()))
SELECT MONTH(DATEADD(mm, -1, GETDATE()))
, , SELECT DATEADD(mm, -1, GETDATE())
SELECT DATEADD(mm, -1, GETDATE())
BTW, SELECT datediff(mm,-1,2-2-2011) -1 -2011, 67 (2010/30). , .
SELECT datediff(mm,-1,2-2-2011)
DATEADD - not DATEDIFF
DATEDIFF - ....
, : '2-2-2011' 2-2-2011.
And finally: I would highly recommend using the ISO-8601 YYYYMMDD date format (here:) 20110202- it will work regardless of the language and regional settings on your SQL Server - the date format will be BREAK on many servers due to the language settings.
20110202
Source: https://habr.com/ru/post/1791654/More articles:static var in member function - c ++preg_replace regex problem - phpWhat resources are needed to run SMS programming through Java? - javaIf I reuse the QProcess variable, can there be any remaining data in the stdout / stderr channels? - qtgenerating a unique id in php (to shorten the url) - phpGet Unicode character by glyph index in CTFontRef or CGFontRef - objective-cUsing PHP as a template engine - phpProcess.Start vs Process `p = new Process ()` in C #? - c #Is there a Google Closure compiler plugin for Notepad ++ - javascriptMimic batch file with C # - c #All Articles