Mabuhay!
Is there any shortcode for updating date with additional 2 years and end of month?
Sample:
Account Opened: 2017-04-04
Expiry: 2019-04-30
UPDATE dbname SET [Expiry] = DateAdd(year,2,[AccountOpened])
Without adding this UPDATE dbname SET [Expiry] = DateAdd(mm,1,[AccountOpened])
because it will add 1 month instead of last month date.
Thanks
If your are using SQLSERVER following script will give desire result. Before performing UPDATE operation take back of Original data.
UPDATE dbname
SET [Expiry] = DATEADD(d,-1,DATEADD(m,DATEDIFF(m,0, (DATEADD(YY,2,AccountOpened))) + 1,0))
This should work for Sql Server (any version, I think)
UPDATE dbName
SET [Expiry] = DATEADD(MONTH, ((YEAR([AccountOpened]) - 1898) * 12) + MONTH([AccountOpened]), -1)
I made a fiddle
For Sql Server 2012+ you can simply:
UPDATE dbName SET [Expiry] = EOMONTH([AccountOpened], 24)
For MySql (which I guess you are not using because you are using brackets for your fields, but here it is nonetheless):
UPDATE dbName SET Expiry = LAST_DAY(DATE_ADD(AccountOpened, INTERVAL 2 YEAR))
Are you looking for this query?
Where Clause = CAST([CustomDate2] AS DATE) between CAST(GETDATE() AS DATE) and DATEADD(day, +/-number here,CAST(GETDATE() AS DATE))
SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,"your date")+1,0))
LastDay_AnyMonth
ResultSet:
LastDay_AnyMonth
———————–
2007-08-31 23:59:59.000
Related
In SQL Server 2016, in one table, I want to change a date column as:
1936-12-15
1928-11-04
1940-09-18
1933-04-26
1942-08-17
To the one below, changing only months and days to 05-20, keeping only the years!
1936-05-20
1928-05-20
1940-05-20
1933-05-20
1942-05-20
I tried the following:
UPDATE [column]
SET [column] = DATEADD(mm, 5, [column])
But it added 5 months to all dates!
Please help. Thank you.
You appear to be using SQL Server:
UPDATE [column]
SET [column] = datefromparts(year(column), 5, 20);
In earlier versions of SQL Server, you can do:
UPDATE [column]
SET [column] = datename(year, column) + '0520'
This will automatically convert the string to the correct date (although you could add an explicit cast()/convert() as well).
Using FORMATMESSAGE:
UPDATE tab
SET col = FORMATMESSAGE('%i0520',YEAR(col));
DBFiddle Demo
UPDATE [Table]
SET [column] = FORMAT([column],'yyyy-05-20')
Im new to SQL. I have a field (ContractDays) with an integer (number of days from a datediff) and want to create a date field with a value of todays date minus the number in ContractDays field. Can you help please?
If you are using mssql. You can do this:
SELECT DATEADD(day, -ContractDays, GETDATE())
FROM Table1
MS SQL contains datetime as number value. Integer part is number of days since 1/1/1753.
So you can write
select GetDate() - ContractDays
But it is better to use DATEADD
You need to alter the table in Oracle, MySQL or MS SQL to add a new column, let's call it ContractDate.
After you have this column, you need to update it to have the correct value.
Oracle:
update Contracts
set ContractDate = (SYSDATE - ContractDays)
MySQL:
update Contracts
set ContractDate = date_add(now(), INTERVAL -ContractDays DAY)
MS SQL (courtesy to Arion):
update Contracts
set ContractDate = DATEADD(day, -ContractDays, GETDATE())
I'm writing a SQL query on a timesheet report. I need the report to return only the details for the week of the selected date.
E.g., if I pick 02/01/2012 (dd/MM/yyyy), then it should only return results between 02/01/2012 and 08/01/2012.
Thanks
SELECT
*
FROM
yourTable
WHERE
dateField >= #yourDate
AND dateField < #yourDate + 7
Some variations of SQL may have specific ways of adding 7 days to a datevalue. Such as...
- DateAdd(Day, 7, #date)
- DATE_ADD(#date, INTERVAL 7 DAYS)
- etc, etc
This option is both index friendly, and is resilient to database fields that have time parts as well as date parts.
You easiest is the equivalent of WEEK_OF_YEAR function in your SQL engine
But you can also use DATE_ADD
WHERE table.date BETWEEN target_date AND DATE_ADD(target_date,INTERVAL 7 DAY)
That depends on the database system you're using. MySQL has a function calles WEEK(), SQL Server can do something like this with the DATEPART() function:
MySQL:
SELECT
*
FROM table
WHERE WEEK(date_col) = WEEK('02/01/2012');
SQL SERVER:
SELECT
*
FROM table
WHERE DATEPART(WEEK, datecol) = DATEPART(WEEK,'02/01/2012');
SELECT * FROM table_name
WHERE date BETWEEN '1/02/2012' AND '1/08/2012';
you can replace the example date with your date and yourdate + 6.
Look here for example: http://www.roseindia.net/sql/sql-between-datetime.shtml
This should be a softball for you SQL guys. I know I can add to an int field with something like UPDATE tblUser SET Total=(Total+2) but what is the syntax for adding seconds to a datetime field?
I'm using SQLServer 2008
UPDATE tbluser SET DateField = DATEADD(ss,numOfSeconds,DateField)
Note the first parameter "ss". This shows that you are adding seconds to the date.
Check the docs for more info.
You should look into DATEADD.
DATEADD (datepart , number , date)
or the full update syntax
UPDATE tbl SET YourDateField = DATEADD (ss, 2, YourDateField)
I want to add 1 year to a datetime-type column in every single row in a table. Adding using an UPDATE statement is easy for numeric types. ex:
UPDATE TABLE SET NUMBERCOLUMN = NUMBERCOLUMN + 1
I'd like to do the same thing with a DATETIME-type...
UPDATE Procrastination SET DropDeadDueDate = DropDeadDueDate + ?
...but I'm not sure what value to use. Is there a numeric value I could use that means "1 year"? Or is there a DATEADD function or similar in SQL Server?
ADDITIONAL QUESTION
I would like to do this for not one field, but for every field in the database of data type 'datetime'. Is there an easy way to select all fields of type 'datetime' and perform an update of adding x amount of years? I am new to sql so please be gentle...
There is in fact a DATEADD statement in T-SQL, you can find it here
UPDATE Procrastination SET DropDeadDueDate = DATEADD(yyyy,1,DropDeadDueDate)
EDIT: You could use year, yy, or yyyy for the first argument of DATEADD.
It could be done with a DATEADD() function like this:
UPDATE Procrastination SET DropDeadDueDate = DATEADD(yy, 1, DropDeadDueDate)
UPDATE Procrastination SET DropDeadDueDate = DATEADD(year, 1, DropDeadDueDate)
http://msdn.microsoft.com/en-us/library/ms186819.aspx
SQL Server has a DATEADD function.
http://msdn.microsoft.com/en-us/library/aa258267(SQL.80).aspx
The DateAdd function should do what you want.
UPDATE Procrastination SET DropDeadDueDate = DateAdd(yy, 1, DropDeadDueDate)
UPDATE Procrastination SET DropDeadDueDate =
DATEADD(yy, 1, DropDeadDueDate)
ref: http://doc.ddart.net/mssql/sql70/da-db_5.htm