0

My query looks like this:

SELECT o.CustomerID, null emptyColumn, o.ShipFirstName, o.ShipCountry, c.EmailAddress

FROM Orders o, Customers c

WHERE o.CustomerID = c.CustomerID

And the results are like,

1,,John,United States,john@example.com
2,,Peter,Canada,peter@example.com

But I need to change "United States" to "US" and "Canada" to "Ca". How can I do this?

4

5 回答 5

2

If it's for a large range of countries, then do a search for ISO country codes (for example) - import that data into a new table and then join to that table.

Or put your selection of countries into a CTE and join to that, e.g.,

WITH Countries(ISO_Code, ISO_Name) AS
(
    SELECT
     *
    FROM (VALUES ('AF', 'AFGHANISTAN')
                ,('AX', 'ÅLAND ISLANDS')
                ,('AL', 'ALBANIA')
                ,('DZ', 'ALGERIA')
                ,('AS', 'AMERICAN SAMOA')
                ,('AD', 'ANDORRA')
                ,('AO', 'ANGOLA')
                ,('AI', 'ANGUILLA')
                ,('AQ', 'ANTARCTICA')
                ,('AG', 'ANTIGUA AND BARBUDA')) nTab(nCol1, nCol2)
)
SELECT
 *
FROM Countries c
JOIN ...
于 2013-08-16T08:32:09.833 回答
1

You can use case,

SELECT  o.CustomerID, null emptyColumn, o.ShipFirstName, 
        case when o.ShipCountry = 'United States' then 'US'
        when o.ShipCountry = 'Canada' then 'CA'
        end,
        c.EmailAddress
FROM Orders o, Customers c
WHERE o.CustomerID = c.CustomerID
于 2013-08-16T08:29:40.053 回答
1

For anyone coming along this thread as I did and looking for state abbreviations instead of countries, here's a quick copy paste of all US states and Canadian provinces in WHEN/THEN format. Adapt to your own CASE statement needs.

CASE "YOUR COLUMN CONTAINING FULL STATE NAMES"
WHEN 'Alabama' THEN 'AL' 
WHEN 'Alaska' THEN 'AK' 
WHEN 'Arizona' THEN 'AZ' 
WHEN 'Arkansas' THEN 'AR' 
WHEN 'California' THEN 'CA' 
WHEN 'Colorado' THEN 'CO' 
WHEN 'Connecticut' THEN 'CT' 
WHEN 'Delaware' THEN 'DE' 
WHEN 'District of Columbia' THEN 'DC' 
WHEN 'Florida' THEN 'FL' 
WHEN 'Georgia' THEN 'GA' 
WHEN 'Hawaii' THEN 'HI' 
WHEN 'Idaho' THEN 'ID' 
WHEN 'Illinois' THEN 'IL' 
WHEN 'Indiana' THEN 'IN' 
WHEN 'Iowa' THEN 'IA' 
WHEN 'Kansas' THEN 'KS' 
WHEN 'Kentucky' THEN 'KY' 
WHEN 'Louisiana' THEN 'LA' 
WHEN 'Maine' THEN 'ME' 
WHEN 'Maryland' THEN 'MD' 
WHEN 'Massachusetts' THEN 'MA' 
WHEN 'Michigan' THEN 'MI' 
WHEN 'Minnesota' THEN 'MN' 
WHEN 'Mississippi' THEN 'MS' 
WHEN 'Missouri' THEN 'MO' 
WHEN 'Montana' THEN 'MT' 
WHEN 'Nebraska' THEN 'NE' 
WHEN 'Nevada' THEN 'NV' 
WHEN 'New Hampshire' THEN 'NH' 
WHEN 'New Jersey' THEN 'NJ' 
WHEN 'New Mexico' THEN 'NM' 
WHEN 'New York' THEN 'NY' 
WHEN 'North Carolina' THEN 'NC' 
WHEN 'North Dakota' THEN 'ND' 
WHEN 'Ohio' THEN 'OH' 
WHEN 'Oklahoma' THEN 'OK' 
WHEN 'Oregon' THEN 'OR' 
WHEN 'Pennsylvania' THEN 'PA' 
WHEN 'Rhode Island' THEN 'RI' 
WHEN 'South Carolina' THEN 'SC' 
WHEN 'South Dakota' THEN 'SD' 
WHEN 'Tennessee' THEN 'TN' 
WHEN 'Texas' THEN 'TX' 
WHEN 'Utah' THEN 'UT' 
WHEN 'Vermont' THEN 'VT' 
WHEN 'Virginia' THEN 'VA' 
WHEN 'Washington' THEN 'WA' 
WHEN 'West Virginia' THEN 'WV' 
WHEN 'Wisconsin' THEN 'WI' 
WHEN 'Wyoming' THEN 'WY' 
WHEN 'Alberta' THEN 'AB' 
WHEN 'British Columbia' THEN 'BC' 
WHEN 'Manitoba' THEN 'MB' 
WHEN 'New Brunswick' THEN 'NB' 
WHEN 'Newfoundland and Labrador' THEN 'NL' 
WHEN 'Northwest Territories' THEN 'NT' 
WHEN 'Nova Scotia' THEN 'NS' 
WHEN 'Nunavut' THEN 'NU' 
WHEN 'Ontario' THEN 'ON' 
WHEN 'Prince Edward Island' THEN 'PE' 
WHEN 'Quebec' THEN 'QC' 
WHEN 'Saskatchewan' THEN 'SK' 
WHEN 'Yukon Territory' THEN 'YT' 
    ELSE NULL
END
于 2020-07-23T00:24:00.747 回答
0

Though the below code will work, it will be better if you have another column in the country table with abbreviations

select 
    case 
    When o.ShipCountry = 'UnitedState' Then 'US'
    When o.ShipCountry = 'Canada' Then 'CA'
    ELSE o.ShipCountry
    End 
于 2013-08-16T08:40:57.403 回答
0

Try this,

SELECT o.CustomerID, null emptyColumn, o.ShipFirstName, o.ShipCountry = 
CASE WHEN oShipCountry ='United States' THEN 'US' 
WHEN o.ShipCountry = 'Canada' then 'CA' END, 
c.EmailAddress
FROM Orders o, Customers c
WHERE o.CustomerID = c.CustomerID
于 2013-08-16T08:45:46.677 回答