0

Here's my SQL:

SELECT * 
FROM MY_TABLE 
WHERE MY_DATE_COLUMN >= CONVERT(DATETIME2, '2022-04-04 15:22:20', 1)

I get this message:

The input character string does not follow style 100, either change the input character string or use a different style.

I've also seen this message at times but haven't determined what causes the different messages:

Conversion failed when converting date and/or time from character string.

I've also tried at least 10 other style values and none of them work. What is wrong with my SQL?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
coder
  • 6,111
  • 19
  • 54
  • 73
  • Does this answer your question? [How to return only the Date from a SQL Server DateTime datatype](https://stackoverflow.com/questions/113045/how-to-return-only-the-date-from-a-sql-server-datetime-datatype) – Ken White Apr 05 '23 at 04:13
  • The datetime string format `2022-04-04 15:22:20` is actually style `121` and not `1`. Change to style `121` – Squirrel Apr 05 '23 at 04:54

1 Answers1

2

If you let TRY_CONVERT() handle the formatting, it will work:

SELECT TRY_CONVERT(datetime, '2022-04-04 15:22:20');  -- 2022-04-04 15:22:20.000

Demo

If you want a date only, then use:

SELECT TRY_CONVERT(date, '2022-04-04 15:22:20');  -- 2022-04-04
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360