SQL SELECT语句中的更改日期格式(DD / MM / YYYY)

时间:2016-07-22 08:06:54

标签: sql sql-server tsql

原始SQL语句是:

SELECT SA.[RequestStartDate] as 'Service Start Date', 
       SA.[RequestEndDate] as 'Service End Date', 
FROM
(......)SA
WHERE......

输出日期格式为YYYY / MM / DD,但我希望输出日期格式为DD / MM / YYYY。如何在此声明中修改?

6 个答案:

答案 0 :(得分:16)

试试这个......

select CONVERT (varchar(10), getdate(), 103) AS [DD/MM/YYYY]

了解更多信息:http://www.sql-server-helper.com/tips/date-formats.aspx

答案 1 :(得分:4)

更改为:

SELECT FORMAT(SA.[RequestStartDate],'dd/MM/yyyy') as 'Service Start Date', SA.[RequestEndDate] as 'Service End Date', FROM (......)SA WHERE......

不知道您正在使用哪个SQL引擎,对于其他SQL引擎,可以在SELECT语句中使用CONVERT来更改所需格式的格式。

答案 2 :(得分:1)

您将需要使用CONVERT()语句。

尝试以下方法;

SELECT CONVERT(VARCHAR(10), SA.[RequestStartDate], 103) as 'Service Start Date', CONVERT(VARCHAR(10), SA.[RequestEndDate], 103) as 'Service End Date', FROM (......) SA WHERE.....

有关详细信息,请参阅MSDN Cast and Convert

答案 3 :(得分:0)

尝试:

<style>
    #myTable tr {
        background: #FF8000
    }

    #myTable tr:nth-child(odd){
        background: #FFB366
    }
</style>

<h1>Enter Your Name</h1>

<form id="myForm">
    <input type="text" id="myInput" required />
    <input type="submit" />
</form>

<table>
    <thead>
        <tr>
            <th>ID</th>
            <th>Name</th>
        </tr>
    </thead>
    <tbody id="myTable"></tbody>
</table>

<button id="myButton">Reset</button>

<script>
    (function(){
        "use strict";

        var
            form  = document.getElementById("myForm"),
            input = document.getElementById("myInput"),
            table = document.getElementById("myTable"),
            reset = document.getElementById("myButton");


        form.addEventListener("submit", add, true);
        function add(event){
            event.preventDefault();

            if( /^[a-z]+$/i.test(myInput.value) )
                table.innerHTML += '<tr><td>' + (table.children.length + 1) + '</td><td>' + input.value + '</td></tr>';


            return input.value = '';
        }

        function reset(){
            table.innerHTML = '';
        }
    })()
</script>

或者:

SELECT convert(nvarchar(10), SA.[RequestStartDate], 103) as 'Service Start Date', 
       convert(nvarchar(10), SA.[RequestEndDate], 103) as 'Service End Date', 
FROM
(......)SA
WHERE......

答案 4 :(得分:0)

还有另一种方法可以做到这一点-

select TO_CHAR(SA.[RequestStartDate] , 'DD/MM/YYYY') as RequestStartDate from ... ;

答案 5 :(得分:0)

我正在使用 oracle,我必须选择多个列并以 YYYY-MM-DD 格式输出日期列,这行得通 select <column_1>, to_char(<date_column>, 'YYYY-MM-DD') as <Alias_name> from <table_name>