How to get DATE from DATETIME Column in SQL?

This question already has an answer here:

  • How to return only the Date from a SQL Server DateTime datatype 37 answers

  • Simply cast your timestamp AS DATE , like this:

    SELECT CAST(tstamp AS DATE)
    

    SQLFiddle Demo

    In other words, your statement would look like this:

    SELECT SUM(transaction_amount)
    FROM mytable
    WHERE Card_No='123'
      AND CAST(transaction_date AS DATE) = target_date
    

    What is nice about CAST is that it works exactly the same on most SQL engines (SQL Server, PostgreSQL, MySQL), and is much easier to remember how to use it. Methods using CONVERT() or TO_DATE() are specific to each SQL engine and make your code non-portable.


    您可以使用

    select * 
    from transaction 
    where (Card_No='123') and (transaction_date = convert(varchar(10),getdate(),101))
    

    使用Getdate()

     select sum(transaction_amount) from TransactionMaster
     where Card_No=' 123' and transaction_date =convert(varchar(10), getdate(), 102)
    
    链接地址: http://www.djcxy.com/p/94374.html

    上一篇: 我怎样才能从datetime列获取日期?

    下一篇: 如何从SQL中的DATETIME列中获取DATE?