Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 1083 Vote(s) - 3.52 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to combine date from one field with time from another field - MS SQL Server

#21
The existing answers do not address the `datetime2` datatype so I will add mine:

Assuming that you want to add a [`time`][1] value to a [`datetime2`][2] value where:

- The `datetime2` value could contain non-zero time component and/or fractional seconds
- The `time` value could contain the value `23:59:59.9999999` which is 86,399.9999999 seconds, 86,399,999,999.9 microseconds or 86,399,999,999,900 nanoseconds¹

Due to the limitations of [`dateadd`][3] function¹ you must add them in two steps:

- Convert the time value to seconds and use `dateadd(second, ...)`
- Extract the nanoseconds from the time value and use `dateadd(nanosecond, ...)` to add them to the date calculated above

```
declare @dv datetime2 = '2000-01-01 12:34:56.7890123';
declare @tv time = '23:59:59.9999999';
select dateadd(
nanosecond,
datepart(nanosecond, @tv),
dateadd(
second,
datepart(hour, @tv) * 60 * 60 + datepart(minute, @tv) * 60 + datepart(second, @tv),
@dv
)
);
-- 2000-01-02 12:34:56.7890122
```

---

¹ Nanosecond values might not fit in [`int`][4] datatype which `dateadd` function expects.


[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:

[To see links please register here]

[4]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through