I have a messy table with a column called date_time
. It has values in both AM/PM and 24 hour format (see example below). I am creating a new table using the messy table with the following code:
| ID | date_time |
| ------------ | ------------ |
| 1 | 1/24/2022 7:08:00 PM |
| 2 | 1/24/2022 17:37 |
| 3 | 1/24/2022 9:36:00 PM |
| 4 | 1/24/2022 22:14 |
CREATE TABLE NEW_TABLE (ID INT, date_time datetime)
SELECT
ID, date_time
INTO NEW_TABLE
FROM MESSY_TABLE
Desired conversion:
ID | date_time |
---|---|
1 | 1/24/2022 19:08 |
2 | 1/24/2022 17:37 |
3 | 1/24/2022 21:36 |
4 | 1/24/2022 22:14 |
1条答案
按热度按时间ugmeyewa1#
Just use
convert
with appropriatestyle
for date portion of the input string. SQL Server seems to handle 12 and 24 hours format time as expected: