我的查询如下:
SELECT o.CustomerID, null emptyColumn, o.ShipFirstName, o.ShipCountry, c.EmailAddress
FROM Orders o, Customers c
WHERE o.CustomerID = c.CustomerID
结果就像,
1,,John,United States,john@example.com
2,,Peter,Canada,peter@example.com
但我需要将“美国”改为“美国”,将“加拿大”改为“加州”。我怎么能这样做?
答案 0 :(得分:2)
如果适用于多个国家/地区,请搜索ISO国家/地区代码(for example) - 将该数据导入新表格,然后加入该表格。
或者将您选择的国家/地区纳入CTE并加入,例如,
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 ...
答案 1 :(得分:1)
您可以使用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
答案 2 :(得分:1)
对于像我一样遵循这个思路并寻找州缩写而不是国家/地区的任何人,这里是WHEN / THEN格式的美国所有州和加拿大各省的快速复制粘贴。适应您自己的CASE语句需求。
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
答案 3 :(得分:0)
虽然下面的代码可行,但如果您在国家/地区表中有另一列缩写
会更好select
case
When o.ShipCountry = 'UnitedState' Then 'US'
When o.ShipCountry = 'Canada' Then 'CA'
ELSE o.ShipCountry
End
答案 4 :(得分:0)
试试这个,
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