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
92
votes
13 answers

Add 'x' number of hours to date

I currently have php returning the current date/time like so: $now = date("Y-m-d H:m:s"); What I'd like to do is have a new variable $new_time equal $now + $hours, where $hours is a number of hours ranging from 24 to 800. Any suggestions?
Jonah Katz
  • 5,230
  • 16
  • 67
  • 90
66
votes
6 answers

SQL Server 2005 Using DateAdd to add a day to a date

How do I in SQL Server 2005 use the DateAdd function to add a day to a date
test
  • 2,087
  • 5
  • 23
  • 22
55
votes
10 answers

How to add days to the current date?

I am trying to add days to the current date and it's working fine but when I add 360 days to the current date it gives me wrong value. eg: Current Date is 11/04/2014 And I am adding 360 Days to it, it should give me 11/04/2015, but it is showing the…
Fazil Mir
  • 783
  • 2
  • 9
  • 23
48
votes
5 answers

Subtract minute from DateTime in SQL Server 2005

Suppose I have a datetime field whose value is 2000-01-01 08:30:00 and a duration field whose value is say 00:15 (meaning 15 minutes) If I subtract these two, I should get 2000-01-01 08:15:00 Also if I want to subtract 1:15 (means 1 hour 15…
priyanka.bangalore
  • 1,471
  • 7
  • 20
  • 32
46
votes
2 answers

Adding one year to a date field in postgresql

I have a table in postgresql with a field_date using the syntax 'YYYY-MM-DD', I want to add a year to the field with the the sentence: UPDATE table SET date_field = DATEADD(YEAR, 1, date_field); but postgres return: ERROR: column "year" does not…
Emilio Galarraga
  • 659
  • 1
  • 8
  • 14
41
votes
3 answers

How to add minutes to the time part of datetime

How to add minutes(INT) to the time part of datetime ? For example : If I have datetime variable like this : @shift_start_time = 2015-11-01 08:00:00.000 @increase = 30 How to get this result? 2015-11-01 08:30:00.000
Anyname Donotcare
  • 11,113
  • 66
  • 219
  • 392
39
votes
4 answers

How to select last one week data from today's date

How to select week data (more precisely, last 7 days data) from the current date in the fastest way as I have millions or rows in the table. I have a time stamp of created_date in sql table. I have tried this SELECT Created_Date FROM…
Zerotoinfinity
  • 6,290
  • 32
  • 130
  • 206
35
votes
5 answers

MySQL - DATE_ADD month interval

I face a problem with the function DATE_ADD in MySQL. My request looks like this : SELECT * FROM mydb WHERE creationdate BETWEEN "2011-01-01" AND DATE_ADD("2011-01-01", INTERVAL 6 MONTH) GROUP BY MONTH(creationdate) The problem is that, in the…
BMN
  • 8,253
  • 14
  • 48
  • 80
25
votes
1 answer

DATEADD equivalent in PostgreSQL

Is there an equivalent to this T-SQL command in PostgreSQL? select dateadd(hh,duration_in_hours,start_date) as end_date I have found only interval keyword with subsequent string, but this terrible construction returns syntax error: select…
Hink
  • 1,054
  • 1
  • 15
  • 31
16
votes
2 answers

MySQL: DATE_ADD

Is there a difference between: SELECT DATE_ADD('2005-01-01', INTERVAL 3 MONTH); and SELECT '2005-01-01' + INTERVAL 3 MONTH;
John Kurlak
  • 6,594
  • 7
  • 43
  • 59
12
votes
2 answers

Using DATE_ADD with a Column Name as the Interval Value

I have a table which contains products, a start date and an interval value : product_name start_date expiry_period Domain Registration (1 Year) 2013-12-08 00:00:00 1 Year Domain Registration (1 Year) …
madebyhippo
  • 681
  • 1
  • 5
  • 13
9
votes
3 answers

MySQL: DATE_SUB/DATE_ADD that accounts for DST?

This returns 1 (aka TRUE) SELECT DATE_SUB(NOW(), INTERVAL 24*100 HOUR) = DATE_SUB(NOW(), INTERVAL 100 DAY); 100 days ago, the hour of day does not change. But due to Daylight Savings Time (US), 100 twenty-four hour periods ago is actually one hour…
700 Software
  • 85,281
  • 83
  • 234
  • 341
8
votes
2 answers

Add Number of days column to Date Column in same dataframe for Spark Scala App

I have a dataframe df of columns ("id", "current_date", "days") and I am trying to add the the "days" to "current_date" and create a new dataframe with new column called "new_date" using spark scala function date_add() val newDF =…
qubiter
  • 235
  • 1
  • 5
  • 19
7
votes
1 answer

MySql: Date_add returns BLOB

i have the next query: select avHours, date_add('2010-01-20', Interval 2 DAY) from tbl_available order by avHours; but it returns a blob field and not a date field. when i see the value in the blob field, it's the right date. how can i fix…
Nick
  • 575
  • 10
  • 29
6
votes
1 answer

The best way to convert time zone efficiently in MYSQL query

My table 'my_logs' have about 20,000,000 records, and I want to find out how many logs I have in each date within a few days. I want to have a result like +------------+---------+ | date | count | +------------+---------+ | 2016-07-01 | …
林鼎棋
  • 1,995
  • 2
  • 16
  • 25
1
2 3
33 34