sql >> Database teknologi >  >> RDS >> Sqlserver

MS SQL sammenligne datoer?

SELECT CASE WHEN CAST(date1 AS DATE) <= CAST(date2 AS DATE) ...
 

Skal gøre, hvad du har brug for.

Testcase

WITH dates(date1, date2, date3, date4) AS (SELECT CAST('20101231 15:13:48.593' AS DATETIME), CAST('20101231 00:00:00.000' AS DATETIME), CAST('20101231 15:13:48.593' AS DATETIME), CAST('20101231 00:00:00.000' AS DATETIME)) SELECT CASE WHEN CAST(date1 AS DATE) <= CAST(date2 AS DATE) THEN 'Y' ELSE 'N' END AS COMPARISON_WITH_CAST, CASE WHEN date3 <= date4 THEN 'Y' ELSE 'N' END AS COMPARISON_WITHOUT_CAST FROM dates

Returnerer

COMPARISON_WITH_CAST | COMPARISON_WITHOUT_CAST Y N

  1. PLS-00539 og PLS-00538 fejl ved oprettelse af UDT-funktioner

  2. Diagnosticering af dødvande i SQL Server 2005

  3. Er det vigtigt at lukke mysql-forbindelsen?

  4. SQL-problem:et til mange forhold og EAV-model