Questions tagged [dateadd]

dateadd is a T-SQL function that returns a datetime with the specified number interval added to a specified datepart of that datetime.

dateadd(datepart,number_of_units,date_time_value) is a T-SQL function available in MS SQL Server and the SAP/Sybase family of database products that can add and subtract from datetime values. Subtraction is accomplished by using a negative number_of_units value.

datepart is usually a two letter code which specifies which date or time segment is being added number of units is a signed int value that specifies how much to add to the datepart date_time_value is the datetime that is being manipulated.

SQL Server Syntax

SAP/Sybase Syntax

498 questions
3
votes
1 answer

Need to add constraint: date plus 10 days

I'm trying to add a constraint to a table so that it displays one of the columns as the current date plus 10 days. Here's what I've tried so far (I'm very new to SQL): ALTER TABLE orders ADD CONSTRAINT default_date DEFAULT DATEADD…
isolatedhowl
  • 171
  • 2
  • 6
  • 17
3
votes
5 answers

Adding years to a date resets to 1970-01-01

$somedate = "1980-02-15"; $otherdate = strtotime('+1 year', strtotime($somedate)); echo date('Y-m-d', $otherdate); outputs 1981-02-15 and $somedate = "1980-02-15"; $otherdate = strtotime('+2 year', strtotime($somedate)); echo date('Y-m-d',…
Zurechtweiser
  • 1,165
  • 2
  • 16
  • 29
3
votes
12 answers

SQL Query Help: Transforming Dates In A Non-Trivial Way

I have a table with a "Date" column, and I would like to do a query that does the following: If the date is a Monday, Tuesday, Wednesday, or Thursday, the displayed date should be shifted up by 1 day, as in DATEADD(day, 1, [Date]) On the other…
Jake
  • 15,007
  • 22
  • 70
  • 86
3
votes
2 answers

Converting timestamps to human readable in MS Access

I have dozens of MySQL tables linked in a MS Access program. There are also a dozen or so Queries which pull these tables togather and provide data in a human fashion, especially converting timestamps to mm/dd/yyyy format. I have tested the…
Alex.Barylski
  • 2,843
  • 4
  • 45
  • 68
2
votes
3 answers

Could somebody explaine difference between two queries?

The first query is: declare @myDate datetime = DATEADD(D,-2000,getdate()) SELECT * FROM [myTable] where CreatedDate >= @myDate The second query is: SELECT * FROM [myTable] where CreatedDate >= DATEADD(D,-2000,getdate()) I expect that the first…
Yara
  • 4,441
  • 6
  • 42
  • 62
2
votes
3 answers

MySQL DATE_ADD INTERVAL

mysql_query("UPDATE tablename SET date = DATE_ADD(DATE(NOW() + INTERVAL 1 WEEK), INTERVAL 17 HOUR), INTERVAL 30 MINUTE) WHERE idevent = '2'") or die(mysql_error()); I want to add 7 days to the date, but have the time aspect of datetime…
Dee1983
  • 93
  • 2
  • 8
2
votes
2 answers

Relative Date Calculation Algorithm in SQL

I have a requirement to calculate the next monthly run time of a job which can be specified using two parameters which can take values from Parameter 1: 1 for Sunday, 2 for Monday, 3 for Tuesday, 4 for Wednesday, 5 for Thursday, 6 for Friday, 7 for…
2
votes
2 answers

DATEADD using a decimal - workarounds?

I'm trying to do something like below (I've simplified the problem, to try and solve this individual part). PRINT DATEADD(week, 0.2, GETDATE()) Which I realise will not work due to the number parameter of dateadd is truncated to an int. I'm trying…
Alex KeySmith
  • 16,657
  • 11
  • 74
  • 152
2
votes
1 answer

Creating a column that'll show previous rates

I'm creating a column that will show an old address. Whenever an address is updated the old address is expired the day before the new one is supposed to take effect and a new row with the same account number will be added with the new address (ex:…
supeercod
  • 27
  • 4
2
votes
2 answers

DateAdd side effects?

I am experiencing a very strange behavior here with Microsoft SQL Server 2016 (SP2-CU15): select convert(datetime, max(TS) + 1.0/24) as A from table; yields 2021-01-16 11:59:00.000 while select convert(datetime, max(TS) + 1.0/24) as A ,…
Ewe
  • 33
  • 4
2
votes
4 answers

Sql -How to fetch the last 5 minutes of data

Hello I am attempting to pull the last 5 minutes of data from the database. The query I have written below is not pulling the data I need. Select e.* from Event e where e.whenoccurred >= datefunc('10/01/2019 00:00 -05:00', '-5 minutes') and…
Xfactor
  • 79
  • 1
  • 4
  • 8
2
votes
1 answer

Is there a way to write and DateAdd and DatePart SSIS Expression to always generate ThisWeekMonday's date using Getdate()?

I have a package that needs to be run every Monday, because the file dates all have Monday's dates for each week that it is sent. If the package fails for some reason, I want to I to write it in such a way that anybody else can rerun it on any…
Sast77
  • 97
  • 1
  • 9
2
votes
2 answers

Display employee anniversary dates within the next month or year of current date

basically trying to create a query that will display employee anniversary dates for upcoming month or year of current date, also would like to display a column that shows the years of service SELECT Employee, Hire_Date CASE WHEN…
Teddy
  • 33
  • 7
2
votes
2 answers

T-SQL beginning of week returns incorrect date

I am trying to group a query by the beginning of each week which should be a Monday. I have various references from the internet to get the first day of the week by using the following code: DATEADD(WEEK, DATEDIFF(WEEK, 0, @Date), 0) However, this…
JSams
  • 55
  • 5
2
votes
3 answers

DATEADD MINUTES Between Range MSSQL

I am having some difficulty in trying to figure out something, lets say I have a date and time; And I want to add 180 minutes to it so; SELECT DATEADD(MINUTE,180,'2018-05-24 15:00') This would give me answer of "2018-05-24 18:00" but I want to do…
Kiel
  • 203
  • 2
  • 12
1 2
3
33 34