change just the date portion of a sql recordHow can I prevent SQL injection in PHP?How do I perform an IF…THEN in an SQL SELECT?Add a column with a default value to an existing table in SQL ServerHow to return only the Date from a SQL Server DateTime datatypeHow to check if a column exists in a SQL Server table?How to concatenate text from multiple rows into a single text string in SQL server?Inserting multiple rows in a single SQL query?How do I UPDATE from a SELECT in SQL Server?Finding duplicate values in a SQL tableReset identity seed after deleting records in SQL Server
Is there any control character or hack to prevent simple command line tools from showing subsequent data?
The meaning of "offing" in "an agreement in the offing"
I multiply the source, you (probably) multiply the output!
If every star in the universe except the Sun were destroyed, would we die?
When calculating averages, why can we treat exploding die as if they're independent?
How did vāti-s become vātēs?
How do we create our own symbolisms?
Contractor cut joist hangers to make them fit
Is a MySQL database a viable alternative to LDAP?
Short story: Interstellar inspector senses "off" nature of planet hiding aggressive culture
How to say "In Japan, I want to ..."?
Can you mark a new target with the Hunter's Mark spell if the original target shifts to a different plane?
A question regarding Buddhist world view and sense organs and their objects
How can I return only the number of paired values in array?
The pirate treasure of Leatherback Atoll
How invisible hand adjusts stock prices if company is listed on multiple exchanges, under multiple currencies, and one of the currencies plunges?
Word for something that used to be popular but not anymore
Methods and Feasibility of Antimatter Mining?
Supervisor wants me to support a diploma-thesis SW tool after I graduated
Owner keeps cutting corners and poaching workers for his other company
Why would an AC motor heavily shake when driven with certain frequencies?
A PEMDAS issue request for explanation
Is there a way to deal with desistance in a off-chain game?
What is the purpose of the rotating plate in front of the lock?
change just the date portion of a sql record
How can I prevent SQL injection in PHP?How do I perform an IF…THEN in an SQL SELECT?Add a column with a default value to an existing table in SQL ServerHow to return only the Date from a SQL Server DateTime datatypeHow to check if a column exists in a SQL Server table?How to concatenate text from multiple rows into a single text string in SQL server?Inserting multiple rows in a single SQL query?How do I UPDATE from a SELECT in SQL Server?Finding duplicate values in a SQL tableReset identity seed after deleting records in SQL Server
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
Using SQL (Azure Sql)
I have a number of records that contain datetime values such as:
3/16/19 9:25 AM
3/16/19 10:15 AM
I need these to all be todays date but retain their time portion.
I am a C# programmer so my SQL is a bit rusty.
So pseudo code would be something like (asuming requested_delivery_date is the field I need updated.
update set requested_delivery_date = requested_delivery_date.dateadd(7 days)
so anything after this that had a date of 3/20/19 9:30 am would now be 3/27/19 9:30 am
sql
add a comment |
Using SQL (Azure Sql)
I have a number of records that contain datetime values such as:
3/16/19 9:25 AM
3/16/19 10:15 AM
I need these to all be todays date but retain their time portion.
I am a C# programmer so my SQL is a bit rusty.
So pseudo code would be something like (asuming requested_delivery_date is the field I need updated.
update set requested_delivery_date = requested_delivery_date.dateadd(7 days)
so anything after this that had a date of 3/20/19 9:30 am would now be 3/27/19 9:30 am
sql
add a comment |
Using SQL (Azure Sql)
I have a number of records that contain datetime values such as:
3/16/19 9:25 AM
3/16/19 10:15 AM
I need these to all be todays date but retain their time portion.
I am a C# programmer so my SQL is a bit rusty.
So pseudo code would be something like (asuming requested_delivery_date is the field I need updated.
update set requested_delivery_date = requested_delivery_date.dateadd(7 days)
so anything after this that had a date of 3/20/19 9:30 am would now be 3/27/19 9:30 am
sql
Using SQL (Azure Sql)
I have a number of records that contain datetime values such as:
3/16/19 9:25 AM
3/16/19 10:15 AM
I need these to all be todays date but retain their time portion.
I am a C# programmer so my SQL is a bit rusty.
So pseudo code would be something like (asuming requested_delivery_date is the field I need updated.
update set requested_delivery_date = requested_delivery_date.dateadd(7 days)
so anything after this that had a date of 3/20/19 9:30 am would now be 3/27/19 9:30 am
sql
sql
edited Mar 28 at 7:42
Joe Ruder
asked Mar 28 at 7:16
Joe RuderJoe Ruder
1,0011 gold badge11 silver badges34 bronze badges
1,0011 gold badge11 silver badges34 bronze badges
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
It looks like a simple UPDATE statement that updates all rows of YourTable.
UPDATE YourTable
SET requested_delivery_date =
DATEADD(DAY, DATEDIFF(DAY, requested_delivery_date, GETDATE()), requested_delivery_date)
;
If you want to add a constant number of days, then the query is even simpler:
UPDATE YourTable
SET requested_delivery_date =
DATEADD(DAY, 7, requested_delivery_date)
;
add a comment |
Use a combination of DateAdd and DateDiff:
DECLARE @Date As datetime = '2019-03-01T15:32:44'
SELECT @Date As Source,
GETDATE() As Today,
DATEADD(DAY, DATEDIFF(DAY, @Date, GETDATE()), @Date) As Result
Result:
Source Today Result
2019-03-01 15:32:44 2019-03-28 08:27:29 2019-03-28 15:32:44
To update the column in the table you simply need to write an update statement:
update TableName
set requested_delivery_date = DATEADD(DAY, DATEDIFF(DAY, requested_delivery_date , GETDATE()), requested_delivery_date)
-- add a where clause to only update some of the records
thank you....my question is how do I update the current records? I can hard code the date even, they all have the same date (3/16) just different times.
– Joe Ruder
Mar 28 at 7:32
Something like: update set requested_delivery_date = requested_delivery_date.adddays(7) But with SQL code.
– Joe Ruder
Mar 28 at 7:35
The manipulation on the date stays the same, you only change theselectto anupdate- I've edited my answer to reflect that.
– Zohar Peled
Mar 28 at 8:21
Thank you Zohar -- I had already accepted a answer, but I do appreciate your time.
– Joe Ruder
Mar 28 at 9:22
Glad to help :-)
– Zohar Peled
Mar 28 at 10:23
add a comment |
I tried this and successfully. This is my table.
startTime endTime
2019-03-28 15:55:10.690 2019-01-01 09:00:00.000
My SQL statement:
UPDATE [MyDatabase].[dbo].[deom1] SET [endTime]=DATEADD(DAY, DATEDIFF(DAY, [endTime], GETDATE()), [endTime])
And this the result:
startTime endTime
2019-03-28 15:55:10.690 2019-03-28 09:00:00.000
For more details, please see DATEADD (Transact-SQL).
Hope this helps.
thank you Leon -- Vladimir's answer happened to be exactly what I was looking for. I appreciate your time and answer.
– Joe Ruder
Mar 28 at 8:22
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55392030%2fchange-just-the-date-portion-of-a-sql-record%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
It looks like a simple UPDATE statement that updates all rows of YourTable.
UPDATE YourTable
SET requested_delivery_date =
DATEADD(DAY, DATEDIFF(DAY, requested_delivery_date, GETDATE()), requested_delivery_date)
;
If you want to add a constant number of days, then the query is even simpler:
UPDATE YourTable
SET requested_delivery_date =
DATEADD(DAY, 7, requested_delivery_date)
;
add a comment |
It looks like a simple UPDATE statement that updates all rows of YourTable.
UPDATE YourTable
SET requested_delivery_date =
DATEADD(DAY, DATEDIFF(DAY, requested_delivery_date, GETDATE()), requested_delivery_date)
;
If you want to add a constant number of days, then the query is even simpler:
UPDATE YourTable
SET requested_delivery_date =
DATEADD(DAY, 7, requested_delivery_date)
;
add a comment |
It looks like a simple UPDATE statement that updates all rows of YourTable.
UPDATE YourTable
SET requested_delivery_date =
DATEADD(DAY, DATEDIFF(DAY, requested_delivery_date, GETDATE()), requested_delivery_date)
;
If you want to add a constant number of days, then the query is even simpler:
UPDATE YourTable
SET requested_delivery_date =
DATEADD(DAY, 7, requested_delivery_date)
;
It looks like a simple UPDATE statement that updates all rows of YourTable.
UPDATE YourTable
SET requested_delivery_date =
DATEADD(DAY, DATEDIFF(DAY, requested_delivery_date, GETDATE()), requested_delivery_date)
;
If you want to add a constant number of days, then the query is even simpler:
UPDATE YourTable
SET requested_delivery_date =
DATEADD(DAY, 7, requested_delivery_date)
;
answered Mar 28 at 8:05
Vladimir BaranovVladimir Baranov
24.1k5 gold badges31 silver badges67 bronze badges
24.1k5 gold badges31 silver badges67 bronze badges
add a comment |
add a comment |
Use a combination of DateAdd and DateDiff:
DECLARE @Date As datetime = '2019-03-01T15:32:44'
SELECT @Date As Source,
GETDATE() As Today,
DATEADD(DAY, DATEDIFF(DAY, @Date, GETDATE()), @Date) As Result
Result:
Source Today Result
2019-03-01 15:32:44 2019-03-28 08:27:29 2019-03-28 15:32:44
To update the column in the table you simply need to write an update statement:
update TableName
set requested_delivery_date = DATEADD(DAY, DATEDIFF(DAY, requested_delivery_date , GETDATE()), requested_delivery_date)
-- add a where clause to only update some of the records
thank you....my question is how do I update the current records? I can hard code the date even, they all have the same date (3/16) just different times.
– Joe Ruder
Mar 28 at 7:32
Something like: update set requested_delivery_date = requested_delivery_date.adddays(7) But with SQL code.
– Joe Ruder
Mar 28 at 7:35
The manipulation on the date stays the same, you only change theselectto anupdate- I've edited my answer to reflect that.
– Zohar Peled
Mar 28 at 8:21
Thank you Zohar -- I had already accepted a answer, but I do appreciate your time.
– Joe Ruder
Mar 28 at 9:22
Glad to help :-)
– Zohar Peled
Mar 28 at 10:23
add a comment |
Use a combination of DateAdd and DateDiff:
DECLARE @Date As datetime = '2019-03-01T15:32:44'
SELECT @Date As Source,
GETDATE() As Today,
DATEADD(DAY, DATEDIFF(DAY, @Date, GETDATE()), @Date) As Result
Result:
Source Today Result
2019-03-01 15:32:44 2019-03-28 08:27:29 2019-03-28 15:32:44
To update the column in the table you simply need to write an update statement:
update TableName
set requested_delivery_date = DATEADD(DAY, DATEDIFF(DAY, requested_delivery_date , GETDATE()), requested_delivery_date)
-- add a where clause to only update some of the records
thank you....my question is how do I update the current records? I can hard code the date even, they all have the same date (3/16) just different times.
– Joe Ruder
Mar 28 at 7:32
Something like: update set requested_delivery_date = requested_delivery_date.adddays(7) But with SQL code.
– Joe Ruder
Mar 28 at 7:35
The manipulation on the date stays the same, you only change theselectto anupdate- I've edited my answer to reflect that.
– Zohar Peled
Mar 28 at 8:21
Thank you Zohar -- I had already accepted a answer, but I do appreciate your time.
– Joe Ruder
Mar 28 at 9:22
Glad to help :-)
– Zohar Peled
Mar 28 at 10:23
add a comment |
Use a combination of DateAdd and DateDiff:
DECLARE @Date As datetime = '2019-03-01T15:32:44'
SELECT @Date As Source,
GETDATE() As Today,
DATEADD(DAY, DATEDIFF(DAY, @Date, GETDATE()), @Date) As Result
Result:
Source Today Result
2019-03-01 15:32:44 2019-03-28 08:27:29 2019-03-28 15:32:44
To update the column in the table you simply need to write an update statement:
update TableName
set requested_delivery_date = DATEADD(DAY, DATEDIFF(DAY, requested_delivery_date , GETDATE()), requested_delivery_date)
-- add a where clause to only update some of the records
Use a combination of DateAdd and DateDiff:
DECLARE @Date As datetime = '2019-03-01T15:32:44'
SELECT @Date As Source,
GETDATE() As Today,
DATEADD(DAY, DATEDIFF(DAY, @Date, GETDATE()), @Date) As Result
Result:
Source Today Result
2019-03-01 15:32:44 2019-03-28 08:27:29 2019-03-28 15:32:44
To update the column in the table you simply need to write an update statement:
update TableName
set requested_delivery_date = DATEADD(DAY, DATEDIFF(DAY, requested_delivery_date , GETDATE()), requested_delivery_date)
-- add a where clause to only update some of the records
edited Mar 28 at 8:20
answered Mar 28 at 7:28
Zohar PeledZohar Peled
62.1k7 gold badges39 silver badges80 bronze badges
62.1k7 gold badges39 silver badges80 bronze badges
thank you....my question is how do I update the current records? I can hard code the date even, they all have the same date (3/16) just different times.
– Joe Ruder
Mar 28 at 7:32
Something like: update set requested_delivery_date = requested_delivery_date.adddays(7) But with SQL code.
– Joe Ruder
Mar 28 at 7:35
The manipulation on the date stays the same, you only change theselectto anupdate- I've edited my answer to reflect that.
– Zohar Peled
Mar 28 at 8:21
Thank you Zohar -- I had already accepted a answer, but I do appreciate your time.
– Joe Ruder
Mar 28 at 9:22
Glad to help :-)
– Zohar Peled
Mar 28 at 10:23
add a comment |
thank you....my question is how do I update the current records? I can hard code the date even, they all have the same date (3/16) just different times.
– Joe Ruder
Mar 28 at 7:32
Something like: update set requested_delivery_date = requested_delivery_date.adddays(7) But with SQL code.
– Joe Ruder
Mar 28 at 7:35
The manipulation on the date stays the same, you only change theselectto anupdate- I've edited my answer to reflect that.
– Zohar Peled
Mar 28 at 8:21
Thank you Zohar -- I had already accepted a answer, but I do appreciate your time.
– Joe Ruder
Mar 28 at 9:22
Glad to help :-)
– Zohar Peled
Mar 28 at 10:23
thank you....my question is how do I update the current records? I can hard code the date even, they all have the same date (3/16) just different times.
– Joe Ruder
Mar 28 at 7:32
thank you....my question is how do I update the current records? I can hard code the date even, they all have the same date (3/16) just different times.
– Joe Ruder
Mar 28 at 7:32
Something like: update set requested_delivery_date = requested_delivery_date.adddays(7) But with SQL code.
– Joe Ruder
Mar 28 at 7:35
Something like: update set requested_delivery_date = requested_delivery_date.adddays(7) But with SQL code.
– Joe Ruder
Mar 28 at 7:35
The manipulation on the date stays the same, you only change the
select to an update - I've edited my answer to reflect that.– Zohar Peled
Mar 28 at 8:21
The manipulation on the date stays the same, you only change the
select to an update - I've edited my answer to reflect that.– Zohar Peled
Mar 28 at 8:21
Thank you Zohar -- I had already accepted a answer, but I do appreciate your time.
– Joe Ruder
Mar 28 at 9:22
Thank you Zohar -- I had already accepted a answer, but I do appreciate your time.
– Joe Ruder
Mar 28 at 9:22
Glad to help :-)
– Zohar Peled
Mar 28 at 10:23
Glad to help :-)
– Zohar Peled
Mar 28 at 10:23
add a comment |
I tried this and successfully. This is my table.
startTime endTime
2019-03-28 15:55:10.690 2019-01-01 09:00:00.000
My SQL statement:
UPDATE [MyDatabase].[dbo].[deom1] SET [endTime]=DATEADD(DAY, DATEDIFF(DAY, [endTime], GETDATE()), [endTime])
And this the result:
startTime endTime
2019-03-28 15:55:10.690 2019-03-28 09:00:00.000
For more details, please see DATEADD (Transact-SQL).
Hope this helps.
thank you Leon -- Vladimir's answer happened to be exactly what I was looking for. I appreciate your time and answer.
– Joe Ruder
Mar 28 at 8:22
add a comment |
I tried this and successfully. This is my table.
startTime endTime
2019-03-28 15:55:10.690 2019-01-01 09:00:00.000
My SQL statement:
UPDATE [MyDatabase].[dbo].[deom1] SET [endTime]=DATEADD(DAY, DATEDIFF(DAY, [endTime], GETDATE()), [endTime])
And this the result:
startTime endTime
2019-03-28 15:55:10.690 2019-03-28 09:00:00.000
For more details, please see DATEADD (Transact-SQL).
Hope this helps.
thank you Leon -- Vladimir's answer happened to be exactly what I was looking for. I appreciate your time and answer.
– Joe Ruder
Mar 28 at 8:22
add a comment |
I tried this and successfully. This is my table.
startTime endTime
2019-03-28 15:55:10.690 2019-01-01 09:00:00.000
My SQL statement:
UPDATE [MyDatabase].[dbo].[deom1] SET [endTime]=DATEADD(DAY, DATEDIFF(DAY, [endTime], GETDATE()), [endTime])
And this the result:
startTime endTime
2019-03-28 15:55:10.690 2019-03-28 09:00:00.000
For more details, please see DATEADD (Transact-SQL).
Hope this helps.
I tried this and successfully. This is my table.
startTime endTime
2019-03-28 15:55:10.690 2019-01-01 09:00:00.000
My SQL statement:
UPDATE [MyDatabase].[dbo].[deom1] SET [endTime]=DATEADD(DAY, DATEDIFF(DAY, [endTime], GETDATE()), [endTime])
And this the result:
startTime endTime
2019-03-28 15:55:10.690 2019-03-28 09:00:00.000
For more details, please see DATEADD (Transact-SQL).
Hope this helps.
edited Mar 28 at 8:17
answered Mar 28 at 8:05
Leon YueLeon Yue
2,3161 gold badge1 silver badge7 bronze badges
2,3161 gold badge1 silver badge7 bronze badges
thank you Leon -- Vladimir's answer happened to be exactly what I was looking for. I appreciate your time and answer.
– Joe Ruder
Mar 28 at 8:22
add a comment |
thank you Leon -- Vladimir's answer happened to be exactly what I was looking for. I appreciate your time and answer.
– Joe Ruder
Mar 28 at 8:22
thank you Leon -- Vladimir's answer happened to be exactly what I was looking for. I appreciate your time and answer.
– Joe Ruder
Mar 28 at 8:22
thank you Leon -- Vladimir's answer happened to be exactly what I was looking for. I appreciate your time and answer.
– Joe Ruder
Mar 28 at 8:22
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55392030%2fchange-just-the-date-portion-of-a-sql-record%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown