Re: question on date (or string after using convert function) comparis



On Mon, 13 Feb 2006 19:05:26 -0800, kei wrote:

select case when '02/12/2005' >= '01/02/2006' then 'T' else 'F' end

the result return is 'T' why?? (2005 should be < 2006)

Hi kei,

You are comparing two string constants. They may look like dates to you
and me (though probably not the same dates - I'm from the part of the
world that uses dd/mm/yyyy), but SQL Server doesn't try to interpret
what you write - it takes you literally.

You could try
SELECT CASE WHEN CAST('02/12/2005' AS datetime) >=
CAST('01/02/2006' AS datetime) THEN 'T' ELSE 'F' END
and pray that SQL Server interprets the ambiguous date format the same
way you do.

Or you could switch to a non-ambiguous date format:
SELECT CASE WHEN CAST('20051202' AS datetime) >=
CAST('20060201' AS datetime) THEN 'T' ELSE 'F' END

For more information, check Tibor Karaszi's article on SQL Server date
and time handling: http://www.karaszi.com/SQLServer/info_datetime.asp

--
Hugo Kornelis, SQL Server MVP
.



Relevant Pages

  • Re: Date Formats
    ... tables of SQL Server. ... I had problem in exporting these fields into Oracle. ... > I presume that the problem is that Oracle doesn't interpret the datetime values from SQL server ... > a format, hence trying to enforce a format for that datatype is meaningless. ...
    (microsoft.public.sqlserver.programming)
  • Re: convert(datetime, datefld, 101) error
    ... datefld is a varchar field in a sql server 2000 table, ... >already in a SQL Server table? ... >format that is completely unambiguous. ... stored as datetime, ...
    (microsoft.public.sqlserver.programming)
  • Re: UTC dates in SQL 2000
    ... Allan Mitchell MCSE,MCDBA, (Microsoft SQL Server MVP) www.SQLDTS.com - The site for all your DTS needs. ... > This db provides dates, which the developer > identifies as UTC, in the format 99999. ... > getutcdatefunction to return the datetime for the> current UTC, or I can CONVERTto convert a> datetime into UTC, but neither will work in an ActiveX> script. ...
    (microsoft.public.sqlserver.dts)
  • Re: Real to datetime - how to...?
    ... would like to have it in hh:mm:ss format. ... The division with 24 is necessary, because a datetime value consists ... Erland Sommarskog, SQL Server MVP, esquel@xxxxxxxxxxxxx ... Books Online for SQL Server 2005 at ...
    (comp.databases.ms-sqlserver)
  • Re: Oh... no....
    ... select castas datetime) ... Obviously it would be better to use the "standard" format. ... Pro SQL Server 2000 Database Design ... >> data type of the date_variable in stored proc have been set to char ...
    (microsoft.public.sqlserver.programming)