SQL Server 2005 - DATE ADD AND GETDATE DOESN'T WORK
GETDATE()
returns the time component of the date. In SQL Server 2005, you can remove the time component by doing:
DATASCADENZA = DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0)
You can add three days as:
DATASCADENZA = DATEADD(day, 3 + DATEDIFF(day, 0, GETDATE()), 0)
Starting in SQL Server 2008, you can just convert to the DATE
data type:
DATASCADENZA = CAST(GETDATE() as DATE)
I much prefer this formulation to adding days to some mysterious "0" date.
SQL add one day to start date
With the DATEADD
function:
CONCAT(FORMAT(DATEADD(dd,1,sc.StartDate),'dd/MM/yyy'), + ' - ' + FORMAT(sc.ENDDate,'dd/MM/yyy')) SIPDate
MSDN reference:
https://msdn.microsoft.com/en-us/library/ms186819.aspx
SQL Server 2005: arithmetic with dates
This will retrieve monday for the current week
Select DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 0)
and then you need to subtract 70 from the above day
SELECT Dateadd(DAY,-70,DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 0))
Edit : Please go through the answer posted in SO
Monday is displayed as Current Week because DATEFIRST which indicates the 1st day of the week is set to monday .In order to set it to Sunday ,you need to change the setting to Sunday
Set DATEFIRST 7
Else as suggested in the above SO link ,you need to change your code
DECLARE @dt DATE = '1905-01-01';
SELECT [start_of_week] = DATEADD(WEEK, DATEDIFF(WEEK, @dt, CURRENT_TIMESTAMP), @dt);
SQL convert DateTime to Date (SQL server 2005)
There is no Date
datatype in SQL Server 2005, it was added in SQL Server 2008.
To go with your second alternative, you can truncate the datetime to midnight on the same day:
SELECT DATEADD(DAY, DATEDIFF(DAY,0, YourDateColumn), 0)
FROM YourTable
Define a parameter for searching on days DATEADD / datediff - SQL server 2005
You still need to perform an operation. If you want to pass in -30 then you need to add, if you want to pass in 30 then you need to subtract.
If @dayparameter
is -30:
dateadd(day, datediff(day, 0 ,getdate()) + @dayparameter, 0) and getdate())
-----------------------------------------^ this operator is important
If @dayparameter
is 30:
dateadd(day, datediff(day, 0 ,getdate()) - @dayparameter, 0) and getdate())
-----------------------------------------^ this operator is still important
Related Topics
How to Count Number of Occurrences for All Different Values in Database Column
Export All Ms Access SQL Queries to Text Files
How to Select the Most Frequently Appearing Values
Use String Contains Function in Oracle SQL Query
How to Tell If I Have Uncommitted Work in an Oracle Transaction
In SQL Is There a Difference Between Count(*) and Count(<Fieldname>)
Difference Between N'String' VS U'String' Literals in Oracle
Django Orm - Get Latest Record for Group
Select Databases Which Only Contain Specific Table
Add Row to Query Result Using Select
SQL Join Table Naming Convention
When Should You Use Full-Text Indexing
Join Two Spreadsheets on a Common Column in Excel or Openoffice
Get the Nearest Longitude and Latitude from Mssql Database Table
Difference Between Decimal and Numeric
SQL Selecting Rows Where One Column's Value Is Common Across Another Criteria Column