I can’t seem to get this right. I am currently trying to convert one of the columns to a different date format, but my code is not working.
The current format of TrackingNumbers.ShipDate is “MM/DD/YYYY hh:mm:ss AM/PM”, and I am trying to make it convert it to “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)
The 5th line is where the trouble is at. How is it supposed to be formatted?
From CAST and CONVERT (Transact-SQL), ANSI style (yyyy-mm-dd) in the CONVERT function would seem to be 102 style, not 120, unless I am mistaken.
How about: