T-SQL: date modification, only days and months - sql

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')

Related

How to add year and end of month in sql query

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

SQL Server - Add seconds to time field

I need to add 8 seconds to a time (e.g. 15:49:12.8080000), but its stored as varchar.
How do I go about it? Any help is appreciated.
Thank you.
here is one way of doing that if you have 2008+ -
update yourtable set timecolumn = dateadd(s, 8, cast(timecolumn as time))
from tbl
see sqlfiddle - http://www.sqlfiddle.com/#!6/efdad/2
If you need to do it for few rows, than you can do by just manually updating, column is varchar:
assuming original value was : 15:49:12.8080000
update table set time_column = "15:49:20.8080000" .....

Replace a certain part of the date

We are creating a demo database. So they need me to change a column having all the dates to a particular date.
Replace just the month and day and keep the year as is.
Ex: 03/12/2012, 06/19/1990
Solution: 01/01/2012, 01/01/1990
They want to make the month and date to: 01/01
My Query is as mentione below:
update Tablename set column = REPLACE(column, select substring(column,1,5) from Tablename ,'01/01') ;
but i get an error as mentione below:
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'select'.
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '01/01'.
Any help/advice would be appreciated.
Don't change dates with string manipulation. It is guaranteed to break things down the road and it's slower.
UPDATE TableName
SET [column] = DATEADD(year,DATEDIFF(year,0,[column]),0)
as of SQL Server 2012 there is a DATEFROMPARTS function which is perfect for this ...
eg ... with #originalDate DATE, #newMonth INT, #newDay INT you could form your new date like this:
DECLARE #replacedDate Date;
SET #replacedDate = DATEFROMPARTS(
DATEPART(year, #originalDate),
#newMonth,
#newDay
)
your sql is thus:
update Tablename
set column = DATEFROMPARTS(DATEPART(year, column),1,1)
here are the sql server docs:
DATEFROMPARTS
DATEPART
Just remove the erroneous select:
update Tablename
set column = REPLACE(column, substring(column,1,5) from Tablename ,'01/01');
However, it seems like a more appropriate approach would be using the DATEPART function:
UPDATE Tablename
SET column = CAST('01/01/' + CAST(DATEPART(year, column) AS VARCHAR) AS DATETIME)
First of all you need to convert DateTime to VARCHAR if you want to perform String operation on it.
Secondly, you don't need to look for SUBSTRING. Just update DateTime column using DateTime format.
UPDATE TABLE SET ColumnName = CAST('Whatever Date You Want' AS DATETIME)

SQL Server: Add seconds to a datetime field?

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)

How can I use a SQL UPDATE statement to add 1 year to a DATETIME column?

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