Hi
I have below data in 2 fields. I want to update SessionStartTime Date Part only . Time should remain same
Date Start Time
2023/05/26 2023-05-19 17:00:00.000
Result should be 2023/05/26 17:00:00.000
Thanks
Hi
I have below data in 2 fields. I want to update SessionStartTime Date Part only . Time should remain same
Date Start Time
2023/05/26 2023-05-19 17:00:00.000
Result should be 2023/05/26 17:00:00.000
Thanks
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Tuhin PaulRank #7 · 71.9K reputationPosted Apr 23, 2023, 11:53 PM EDT
UPDATE your_table SET SessionStartTime = CAST(CONVERT(date, SessionStartTime) AS datetime) + DATEPART(hour, [Start Time]) * 3600 + DATEPART(minute, [Start Time]) * 60 + DATEPART(second, [Start Time])You can use the `DATEPART` function to extract the time part from the `Start Time` field and then combine it with the `SessionStartTime` field to create the final result. This query will convert the `SessionStartTime` field to a `datetime` value with the date part only, and then add the hour, minute, and second parts of the `Start Time` field to it. This will result in a `datetime` value with the same time as the `Start Time` field but with the date part of the `SessionStartTime` field.
Tuhin PaulRank #7 · 71.9K reputationPosted Apr 24, 2023, 12:00 AM EDT
DateTime sessionStartTime = DateTime.Parse("2023/05/26 17:00:00.000"); DateTime newDate = DateTime.Parse("2023/05/26").Date; TimeSpan timePart = sessionStartTime.TimeOfDay; DateTime newDateTime = newDate + timePart;// Now you can use the newDateTime value to update the SessionStartTime field in the database
In C#, you can use the `DateTime` and `TimeSpan` classes to update the date part of a `DateTime` value while keeping the time part the same. Here we first parse the original `SessionStartTime` value and extract the time part using the `TimeOfDay` property. We then parse the new date value and create a new `DateTime` value by combining the date part from the new date and the time part from the original `SessionStartTime` value. You can use the `newDateTime` value to update the `SessionStartTime` field in the database.
Ali BenchaabanRank #925 · 2.2K reputationPosted Apr 23, 2023, 8:23 AM EDT
Hello Ramco,
You can assign an initial value to a new
DateTimevalue in many different ways:Extension method DateTime
public static DateTime ChangeTime(this DateTime dateTime, int hours, int minutes, int seconds = default, int milliseconds = default) { return new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, hours, minutes, seconds,milliseconds, dateTime.Kind); } DateTime datetime = DateTime.Now; //Your DateTime datetime = datetime.ChangeTime(12, 20, 10);DateTime datetime = DateTime.Now; //Your DateTime datetime = datetime.Date.AddHours(12).AddMinutes(20).AddSeconds(10);DateTime datetime = DateTime.Now; //Your DateTime datetime = datetime.Date.Add(new TimeSpan(12, 20, 10));BENCHAABAN Ali
Amit MohantyRank #112 · 20K reputationPosted Apr 24, 2023, 12:02 AM EDT
You can use the
DateTime.ParseExact()method to parse the Start Time field, extract the time portion of it, and then combine it with the SessionStartTime field to create the desired result.string sessionStartTimeStr = "2023/05/26"; string startTimeStr = "2023-05-19 17:00:00.000"; DateTime sessionStartTime = DateTime.ParseExact(sessionStartTimeStr, "yyyy/MM/dd", CultureInfo.InvariantCulture); DateTime startTime = DateTime.ParseExact(startTimeStr, "yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture); DateTime result = sessionStartTime.Date + startTime.TimeOfDay; string resultStr = result.ToString("yyyy/MM/dd HH:mm:ss.fff");