Search code examples
c#sqlsql-serversql-server-2014datetime-format

DateTime2 Comparisons in SQL Editor and C#


I'm having real issues comparing datetimes in my SQL database. In SQL Server Manager 2014 I'll run something like the following:

SELECT 
  *
FROM 
  EmulationSessions
where StaffUser = 'person'
AND Active = 1
AND Created < '2017-02-05 15:38:13'

The row I expect to see looks like this:

enter image description here

But instead I get no result.

Similar attempts to match the format of the database in C# also fail when I convert current time like the following:

System.DateTime currentTime = System.DateTime.Now;
string currentTimeString = System.DateTime
                                    .Parse(currentTime.ToString(), new CultureInfo("en-AU"))
                                    .ToString("yyyy-MM-dd hh24:mm:ss");

Is there another conversion step I'm missing?


Solution

  • SQL Server defualt format is 'YYYY-MM-DD', In your case '02' is considered as month. Check like this,

    SELECT 
          *
        FROM 
          EmulationSessions
        where StaffUser = 'person'
        AND Active = 1
        AND Created < '2017-05-02 15:38:13'