Sqlserver
 sql >> Teknologi Basis Data >  >> RDS >> Sqlserver

MS SQL membandingkan tanggal?

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

Harus melakukan apa yang Anda butuhkan.

Kasus Uji

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 

Pengembalian

COMPARISON_WITH_CAST   |  COMPARISON_WITHOUT_CAST
Y                         N


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. SQL:Apa yang lebih baik Bit atau char (1)

  2. C# Setara dengan Tipe Data SQL Server

  3. scope_identity vs ident_current

  4. Berapa nomor telepon dunia terpanjang yang harus saya pertimbangkan dalam SQL varchar(panjang) untuk telepon

  5. tabel SQL Server ke json