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

Shahab a 261 Reputation points
2022-12-01T19:12:15.94+00:00

Hi All
I See this Error

Msg 241, Level 16, State 1, Line 1  
Conversion failed when converting date and/or time from character string.  
  

When Use This Code ...

DATEDIFF(DAY, '2022/05/06', '2022/07/01') > 3  

How to Solve it ?
Thanks all

Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,666 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Jingyang Li 5,891 Reputation points
    2022-12-01T20:03:56.607+00:00

    Try this :

    Select DATEDIFF(DAY, '05/06/2022' , '07/01/2022')

    Your system is using British/French:

    --103 = dd/mm/yyyy

    0 comments No comments

  2. Erland Sommarskog 113.7K Reputation points MVP
    2022-12-01T22:07:00.107+00:00

    Try:

       SELECT    DATEDIFF(DAY, '20220506', '20220701') > 3  
    

    The format YYYYMMDD without any delimiter is safe. That is, it works no matter the SET DATEFORMAT setting.

    0 comments No comments

  3. PercyTang-MSFT 12,501 Reputation points Microsoft Vendor
    2022-12-02T02:23:14.817+00:00

    Hi @Shahab a

    SQL Server supports many formats, most of which depend on your settings, so sometimes these settings may or may not work.
    The solution to this problem is to use a date format that SQL Server supports. This format is suitable for any situation, regardless of your SQL Server language and date formatting settings.
    YYYYMMDD format, you should be able to work with any version of SQL Server and any language and date formatting settings in SQL Server,As ErlandSommarskog said.

    DATEDIFF(DAY,'20220506','20220701') > 3  
    

    If you want to know more about date formats, you can click on the links below.cast-and-convert-transact-sql

    Best regards,
    Percy Tang

    ----------

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.