• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: Format my date in a SQL query, namely to remove seconds?

Resolved: Format my date in a SQL query, namely to remove seconds?

0
By Isaac Tonny on 17/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

Table 1 has a date column in the following format: 2021-05-01 03:00:00
Table 2 has a date columns in this format: 2021-05-01 03:00.0000000 (edited from 2021-05-01 03:00:00)
How can I show the date in Table 2 to be like the date in Table 1?
I search here and elsewhere and I found links like the following: https://www.mssqltips.com/sqlservertip/2655/format-sql-server-dates-with-format-function/ Sql Server select datetime without seconds
but I’m still having difficulty with my code:
SELECT FORMAT(MAX(date1), 'yyyy-mm-dd HH:mm') FROM table2.
What am I doing wrong?

Answer:

Looking at the format docs for dates and times and the date and time data types, we see mm is minutes and MM is months.
It might be faster to convert, but that also means using obscure convert codes.
The different formats suggest table1 is a smalldatetime and table2 is a datetime2. If you have to do this conversion a lot, consider making your datetime columns all the same type.
Demonstration of different datetime types.

If you have better answer, please add a comment about this, thank you!

sql sql-server
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: Unable to run Docker .NET 6 console image

02/04/2023

Resolved: Input Focus between two React Components

02/04/2023

Resolved: linq2db throws exception when filtering by nested collection

02/04/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.