Total members 11894 |It is currently Wed Nov 27, 2024 2:50 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





In article we talk about the date_add function which is used to add time or date to a specific MySQL table. You can add day, month, year, hour, seconds etc. You can use where clause to select any record for the updating process. The output of this function will be stored in a database record while you can control the format of this output. At following is the syntax of date_add function:

Code:
date_add(Table_Date_Field, Time/ Date to add);

Here is an example for updating a table record using current date plus a four months:
For months:
Code:
update Department set enteryDate=date_add(now(),interval 3 month) where id=3

The query updates the table "Department" and, specifically, the column "enteryDate" to a new value. The new value is generated by using the "date_add()" function, which takes two arguments: the first argument is the current date and time (now()), and the second argument is an interval of 3 months. The function will add three months to the current date and time and return the new date. The "where" clause in this query is used to limit the rows that will be affected by the update statement. In this case, it is specified that the update should only be applied to the row where the "id" column is equal to 3. This query updates the "enteryDate" column in the "Department" table by adding three months to the current date and time for the row where the id is 3.


It can be years:
Code:
update Department set enteryDate=date_add(now(),interval 4 year) where id=6

Or days:
Code:
update Department set enteryDate=date_add(now(),interval 500 day) where id=9


You can also do some testing using date_add function without a table but as follows :
Code:
select date_add(now(),interval 50 day) as newDate


You can also use it in the insert query:
Code:
insert into Transaction(EnteryDate,ExpireDate) values(now(), date_add(now(),interval 54 day) )



In addition to what I've already mentioned, it's worth noting that the query is using the "date_add()" function. This function is a MySQL function that allows you to add a specified time interval to a date or datetime value. This function can be used to perform various date calculations, such as adding or subtracting days, months, or years to date, or calculating the difference between two dates. It's also worth noting that the query is using the "now()" function, which returns the current date and time. This value is used as the first argument for the "date_add()" function, and the interval of 3 months is added to it. It's also important to note that the query is using the "update" statement to modify the data in the table, this statement will change the data in the database, and it's essential to make sure that the update statement is correct and not causing any errors or data loss. It's always a good practice to make a backup of the data before running any update statement and test the statement on a subset of data before running it on the entire table. It's also important to consider the effect of this statement on other parts of the application that may use this table or the data in it. Some applications may have their own logic and rules, and the update statement may cause unexpected results on those applications.



_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : date_add function usage
 php function     -  
 array_key_exists function use     -  
 Using include function     -  
 Function Overloading     -  
 The isset() Function     -  
 Function Recursion     -  
 function key recognition     -  
 finalize() function     -  
 Using the array_slice () Function     -  
 Function return more than one value     -  



Topic Tags

Database, SQL, Mysql SQL, MYSQL
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com