how to remove time from datetime

前端 未结 10 1558
清酒与你
清酒与你 2020-12-01 10:18

The field DATE in the database has the following format:

2012-11-12 00:00:00

I would like to remove the time from the date and return the d

相关标签:
10条回答
  • 2020-12-01 10:42

    In mysql at least, you can use DATE(theDate).

    0 讨论(0)
  • 2020-12-01 10:45

    Use this SQL:

    SELECT DATE_FORMAT(date_column_here,'%d/%m/%Y') FROM table_name;
    
    0 讨论(0)
  • 2020-12-01 10:49

    I found this method to be quite useful. However it will convert your date/time format to just date but never the less it does the job for what I need it for. (I just needed to display the date on a report, the time was irrelevant).

    CAST(start_date AS DATE)
    

    UPDATE

    (Bear in mind I'm a trainee ;))

    I figured an easier way to do this IF YOU'RE USING SSRS.

    It's easier to actually change the textbox properties where the field is located in the report. Right click field>Number>Date and select the appropriate format!

    0 讨论(0)
  • 2020-12-01 10:53

    First thing's first, if your dates are in varchar format change that, store dates as dates it will save you a lot of headaches and it is something that is best done sooner rather than later. The problem will only get worse.

    Secondly, once you have a date DO NOT convert the date to a varchar! Keep it in date format and use formatting on the application side to get the required date format.

    There are various methods to do this depending on your DBMS:


    SQL-Server 2008 and later:

    SELECT  CAST(CURRENT_TIMESTAMP AS DATE)
    

    SQL-Server 2005 and Earlier

    SELECT  DATEADD(DAY, DATEDIFF(DAY, 0, CURRENT_TIMESTAMP), 0)
    

    SQLite

    SELECT  DATE(NOW())
    

    Oracle

    SELECT  TRUNC(CURRENT_TIMESTAMP)
    

    Postgresql

    SELECT  CURRENT_TIMESTAMP::DATE
    

    If you need to use culture specific formatting in your report you can either explicitly state the format of the receiving text box (e.g. dd/MM/yyyy), or you can set the language so that it shows the relevant date format for that language.

    Either way this is much better handled outside of SQL as converting to varchar within SQL will impact any sorting you may do in your report.

    If you cannot/will not change the datatype to DATETIME, then still convert it to a date within SQL (e.g. CONVERT(DATETIME, yourField)) before sending to report services and handle it as described above.

    0 讨论(0)
  • 2020-12-01 10:57

    Personally, I'd return the full, native datetime value and format this in the client code.

    That way, you can use the user's locale setting to give the correct meaning to that user.

    "11/12" is ambiguous. Is it:

    • 12th November
    • 11th December
    0 讨论(0)
  • 2020-12-01 10:59
    SELECT DATE('2012-11-12 00:00:00');
    

    returns

    2012-11-12
    
    0 讨论(0)
提交回复
热议问题