0

我似乎无法做到这一点。我目前正在尝试将其中一列转换为不同的日期格式,但我的代码不起作用。

TrackingNumbers.ShipDate 的当前格式是“MM/DD/YYYY hh:mm:ss AM/PM”,我正在尝试将其转换为“yyyy-mm-dd”。

SELECT TOP 500 -- Selects the first 500 records for a smaller file
    Orders.PONum AS 'order-id',
    TrackingNumbers.TrackingNumber AS 'tracking-number',
    TrackingNumbers.Gateway AS 'carrier-code',
    CONVERT(VARCHAR(10), TrackingNumbers.ShipDate, 120) AS 'ship-date', 
            -- The date format is currently "MM/DD/YYYY  hh:mm:ss AM/PM"
            -- It needs to be yyyy-mm-dd
    CASE TrackingNumbers.ShippingMethodID   -- Converts the shipping Methods so customers can understand them
        WHEN '900' THEN 'USPS Priority (2-14 days)'
        WHEN '103' THEN 'UPS 2nd Day Air'
        WHEN '108' THEN 'UPS Ground'
        WHEN '112' THEN 'UPS Worldwide Expedited'
        WHEN '214' THEN 'USPS Express Mail Intl'
        WHEN '220' THEN 'USPS Priority (2-14 days)'
        WHEN '505' THEN 'UPS Next Day Air Saver'
        WHEN '207' THEN 'USPS First ClASs (7-30 Days)'
        WHEN '222' THEN 'USPS Express Mail Envelope'
        WHEN '102' THEN 'UPS Next Day Air'
        WHEN '217' THEN 'USPS Priority Mail International'
        WHEN '107' THEN 'UPS 3 Day Select'
    END AS 'ship-method'
FROM
    TrackingNumbers, -- Selects two different tables
    Orders  
WHERE 
    Orders.OrderID = TrackingNumbers.OrderID -- Merges Orders with tracking numbers
    AND 
    Orders.PONum<>'' -- Only grabs orders with a PO number
Order By 
    TrackingNumbers.ShipDate Desc -- Sorts by descending ship date of the package (order ship date can be different)

第 5 行是问题所在。它应该如何格式化?

4

1 回答 1

2

CAST 和 CONVERT (Transact-SQL)来看,CONVERT 函数中的 ANSI 样式 (yyyy-mm-dd) 似乎是 102 样式,而不是 120,除非我弄错了。

怎么样:

REPLACE(CONVERT(VARCHAR(10), TrackingNumbers.ShipDate, 102), '.', '-') AS 'ship-date'
于 2012-12-14T06:22:20.090 回答