Validating region/state/province codes for different countries












-1












$begingroup$


I have a large table in which I store region/state/province codes (two letters) from different countries. I use these region codes further downstream for multiple process. One of the steps I do is a cleanup of regions to ensure it is a valid region/state/province code. I have a lookup table consisting of a country vs region code to validate each column.



6 columns are validated separately. Is there a way to do this better?



UPDATE  table_region
SET a_region = NULL
WHERE a_region IS NOT NULL
AND country IS NOT NULL
AND dbo.IsValidRegion(country,a_region) = 0;

UPDATE table_region
SET b_region = NULL
WHERE b_region IS NOT NULL
AND country IS NOT NULL
AND dbo.IsValidRegion(country,b_region) = 0;

UPDATE table_region
SET c_region = NULL
WHERE c_region IS NOT NULL
AND country IS NOT NULL
AND dbo.IsValidRegion(country, c_region) = 0;

UPDATE table_region
SET d_region = NULL
WHERE d_region IS NOT NULL
AND country IS NOT NULL
AND dbo.IsValidRegion(country,d_region) = 0;

UPDATE table_region
SET e_region = NULL
WHERE e_region IS NOT NULL
AND country IS NOT NULL
AND dbo.IsValidRegion(country, e_region) = 0;

UPDATE table_region
SET f_region = NULL
WHERE f_region IS NOT NULL
AND country IS NOT NULL
AND dbo.IsValidRegion(country, f_region) = 0;


The function is like this:



ALTER Function [dbo].[IsValidRegion](@country VARCHAR(20), @value 

VARCHAR(20))
Returns INT
AS
BEGIN
DECLARE @res INT
BEGIN
IF EXISTS (SELECT 1 FROM mapping.region WHERE country = @country AND region_code = @value)
SELECT @res = 1
ELSE
SELECT @res = 0
END
RETURN @res
END









share|improve this question











$endgroup$












  • $begingroup$
    Why don't you just foreign key this table to the table specifying valid country regions?
    $endgroup$
    – Caius Jard
    Jan 4 at 20:33
















-1












$begingroup$


I have a large table in which I store region/state/province codes (two letters) from different countries. I use these region codes further downstream for multiple process. One of the steps I do is a cleanup of regions to ensure it is a valid region/state/province code. I have a lookup table consisting of a country vs region code to validate each column.



6 columns are validated separately. Is there a way to do this better?



UPDATE  table_region
SET a_region = NULL
WHERE a_region IS NOT NULL
AND country IS NOT NULL
AND dbo.IsValidRegion(country,a_region) = 0;

UPDATE table_region
SET b_region = NULL
WHERE b_region IS NOT NULL
AND country IS NOT NULL
AND dbo.IsValidRegion(country,b_region) = 0;

UPDATE table_region
SET c_region = NULL
WHERE c_region IS NOT NULL
AND country IS NOT NULL
AND dbo.IsValidRegion(country, c_region) = 0;

UPDATE table_region
SET d_region = NULL
WHERE d_region IS NOT NULL
AND country IS NOT NULL
AND dbo.IsValidRegion(country,d_region) = 0;

UPDATE table_region
SET e_region = NULL
WHERE e_region IS NOT NULL
AND country IS NOT NULL
AND dbo.IsValidRegion(country, e_region) = 0;

UPDATE table_region
SET f_region = NULL
WHERE f_region IS NOT NULL
AND country IS NOT NULL
AND dbo.IsValidRegion(country, f_region) = 0;


The function is like this:



ALTER Function [dbo].[IsValidRegion](@country VARCHAR(20), @value 

VARCHAR(20))
Returns INT
AS
BEGIN
DECLARE @res INT
BEGIN
IF EXISTS (SELECT 1 FROM mapping.region WHERE country = @country AND region_code = @value)
SELECT @res = 1
ELSE
SELECT @res = 0
END
RETURN @res
END









share|improve this question











$endgroup$












  • $begingroup$
    Why don't you just foreign key this table to the table specifying valid country regions?
    $endgroup$
    – Caius Jard
    Jan 4 at 20:33














-1












-1








-1





$begingroup$


I have a large table in which I store region/state/province codes (two letters) from different countries. I use these region codes further downstream for multiple process. One of the steps I do is a cleanup of regions to ensure it is a valid region/state/province code. I have a lookup table consisting of a country vs region code to validate each column.



6 columns are validated separately. Is there a way to do this better?



UPDATE  table_region
SET a_region = NULL
WHERE a_region IS NOT NULL
AND country IS NOT NULL
AND dbo.IsValidRegion(country,a_region) = 0;

UPDATE table_region
SET b_region = NULL
WHERE b_region IS NOT NULL
AND country IS NOT NULL
AND dbo.IsValidRegion(country,b_region) = 0;

UPDATE table_region
SET c_region = NULL
WHERE c_region IS NOT NULL
AND country IS NOT NULL
AND dbo.IsValidRegion(country, c_region) = 0;

UPDATE table_region
SET d_region = NULL
WHERE d_region IS NOT NULL
AND country IS NOT NULL
AND dbo.IsValidRegion(country,d_region) = 0;

UPDATE table_region
SET e_region = NULL
WHERE e_region IS NOT NULL
AND country IS NOT NULL
AND dbo.IsValidRegion(country, e_region) = 0;

UPDATE table_region
SET f_region = NULL
WHERE f_region IS NOT NULL
AND country IS NOT NULL
AND dbo.IsValidRegion(country, f_region) = 0;


The function is like this:



ALTER Function [dbo].[IsValidRegion](@country VARCHAR(20), @value 

VARCHAR(20))
Returns INT
AS
BEGIN
DECLARE @res INT
BEGIN
IF EXISTS (SELECT 1 FROM mapping.region WHERE country = @country AND region_code = @value)
SELECT @res = 1
ELSE
SELECT @res = 0
END
RETURN @res
END









share|improve this question











$endgroup$




I have a large table in which I store region/state/province codes (two letters) from different countries. I use these region codes further downstream for multiple process. One of the steps I do is a cleanup of regions to ensure it is a valid region/state/province code. I have a lookup table consisting of a country vs region code to validate each column.



6 columns are validated separately. Is there a way to do this better?



UPDATE  table_region
SET a_region = NULL
WHERE a_region IS NOT NULL
AND country IS NOT NULL
AND dbo.IsValidRegion(country,a_region) = 0;

UPDATE table_region
SET b_region = NULL
WHERE b_region IS NOT NULL
AND country IS NOT NULL
AND dbo.IsValidRegion(country,b_region) = 0;

UPDATE table_region
SET c_region = NULL
WHERE c_region IS NOT NULL
AND country IS NOT NULL
AND dbo.IsValidRegion(country, c_region) = 0;

UPDATE table_region
SET d_region = NULL
WHERE d_region IS NOT NULL
AND country IS NOT NULL
AND dbo.IsValidRegion(country,d_region) = 0;

UPDATE table_region
SET e_region = NULL
WHERE e_region IS NOT NULL
AND country IS NOT NULL
AND dbo.IsValidRegion(country, e_region) = 0;

UPDATE table_region
SET f_region = NULL
WHERE f_region IS NOT NULL
AND country IS NOT NULL
AND dbo.IsValidRegion(country, f_region) = 0;


The function is like this:



ALTER Function [dbo].[IsValidRegion](@country VARCHAR(20), @value 

VARCHAR(20))
Returns INT
AS
BEGIN
DECLARE @res INT
BEGIN
IF EXISTS (SELECT 1 FROM mapping.region WHERE country = @country AND region_code = @value)
SELECT @res = 1
ELSE
SELECT @res = 0
END
RETURN @res
END






sql sql-server






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 3 mins ago









Jamal

30.3k11116226




30.3k11116226










asked Jan 4 at 17:02









VK_217VK_217

1043




1043












  • $begingroup$
    Why don't you just foreign key this table to the table specifying valid country regions?
    $endgroup$
    – Caius Jard
    Jan 4 at 20:33


















  • $begingroup$
    Why don't you just foreign key this table to the table specifying valid country regions?
    $endgroup$
    – Caius Jard
    Jan 4 at 20:33
















$begingroup$
Why don't you just foreign key this table to the table specifying valid country regions?
$endgroup$
– Caius Jard
Jan 4 at 20:33




$begingroup$
Why don't you just foreign key this table to the table specifying valid country regions?
$endgroup$
– Caius Jard
Jan 4 at 20:33










1 Answer
1






active

oldest

votes


















-2












$begingroup$

Here's a one hit way of doing this:



    UPDATE t
SET
a_region = ra.region_code,
b_region = rb.region_code,
c_region = rc.region_code,
d_region = rd.region_code,
e_region = re.region_code,
f_region = rf.region_code
FROM
table_region t
LEFT OUTER JOIN mapping.region ra WHERE t.country = ra.country AND t.a_region = ra.region_code
LEFT OUTER JOIN mapping.region rb WHERE t.country = rb.country AND t.b_region = rb.region_code
LEFT OUTER JOIN mapping.region rc WHERE t.country = rc.country AND t.c_region = rc.region_code
LEFT OUTER JOIN mapping.region rd WHERE t.country = rd.country AND t.d_region = rd.region_code
LEFT OUTER JOIN mapping.region re WHERE t.country = re.country AND t.e_region = re.region_code
LEFT OUTER JOIN mapping.region rf WHERE t.country = rf.country AND t.f_region = rf.region_code


It basically works by left joining the region table 6 times, once for each of the *_region columns.




  • If the relation works out, then the r*.region_code will be populated with a value (which means the *_region column is set to the same value it currently is, i.e. a non-op).

  • If it doesn't work out, then the join fails, null is present in r*.region_code


After you run this as a one time fix, to identify the ones that are failed (maybe you should remove them to another table) you should consider making table_region(country,*_region) have multiple foreign keys to mapping.region(country,region_code) to ensure that records cannot be inserted into table_region that don't have a valid country/region mapping






share|improve this answer








New contributor




Caius Jard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






$endgroup$









  • 1




    $begingroup$
    You have presented an alternative solution, but haven't reviewed the code. Please edit to show what aspects of the question code prompted you to write this version, and in what ways it's an improvement over the original. It may be worth (re-)reading How to Answer.
    $endgroup$
    – Toby Speight
    17 hours ago











Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");

StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f210885%2fvalidating-region-state-province-codes-for-different-countries%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









-2












$begingroup$

Here's a one hit way of doing this:



    UPDATE t
SET
a_region = ra.region_code,
b_region = rb.region_code,
c_region = rc.region_code,
d_region = rd.region_code,
e_region = re.region_code,
f_region = rf.region_code
FROM
table_region t
LEFT OUTER JOIN mapping.region ra WHERE t.country = ra.country AND t.a_region = ra.region_code
LEFT OUTER JOIN mapping.region rb WHERE t.country = rb.country AND t.b_region = rb.region_code
LEFT OUTER JOIN mapping.region rc WHERE t.country = rc.country AND t.c_region = rc.region_code
LEFT OUTER JOIN mapping.region rd WHERE t.country = rd.country AND t.d_region = rd.region_code
LEFT OUTER JOIN mapping.region re WHERE t.country = re.country AND t.e_region = re.region_code
LEFT OUTER JOIN mapping.region rf WHERE t.country = rf.country AND t.f_region = rf.region_code


It basically works by left joining the region table 6 times, once for each of the *_region columns.




  • If the relation works out, then the r*.region_code will be populated with a value (which means the *_region column is set to the same value it currently is, i.e. a non-op).

  • If it doesn't work out, then the join fails, null is present in r*.region_code


After you run this as a one time fix, to identify the ones that are failed (maybe you should remove them to another table) you should consider making table_region(country,*_region) have multiple foreign keys to mapping.region(country,region_code) to ensure that records cannot be inserted into table_region that don't have a valid country/region mapping






share|improve this answer








New contributor




Caius Jard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






$endgroup$









  • 1




    $begingroup$
    You have presented an alternative solution, but haven't reviewed the code. Please edit to show what aspects of the question code prompted you to write this version, and in what ways it's an improvement over the original. It may be worth (re-)reading How to Answer.
    $endgroup$
    – Toby Speight
    17 hours ago
















-2












$begingroup$

Here's a one hit way of doing this:



    UPDATE t
SET
a_region = ra.region_code,
b_region = rb.region_code,
c_region = rc.region_code,
d_region = rd.region_code,
e_region = re.region_code,
f_region = rf.region_code
FROM
table_region t
LEFT OUTER JOIN mapping.region ra WHERE t.country = ra.country AND t.a_region = ra.region_code
LEFT OUTER JOIN mapping.region rb WHERE t.country = rb.country AND t.b_region = rb.region_code
LEFT OUTER JOIN mapping.region rc WHERE t.country = rc.country AND t.c_region = rc.region_code
LEFT OUTER JOIN mapping.region rd WHERE t.country = rd.country AND t.d_region = rd.region_code
LEFT OUTER JOIN mapping.region re WHERE t.country = re.country AND t.e_region = re.region_code
LEFT OUTER JOIN mapping.region rf WHERE t.country = rf.country AND t.f_region = rf.region_code


It basically works by left joining the region table 6 times, once for each of the *_region columns.




  • If the relation works out, then the r*.region_code will be populated with a value (which means the *_region column is set to the same value it currently is, i.e. a non-op).

  • If it doesn't work out, then the join fails, null is present in r*.region_code


After you run this as a one time fix, to identify the ones that are failed (maybe you should remove them to another table) you should consider making table_region(country,*_region) have multiple foreign keys to mapping.region(country,region_code) to ensure that records cannot be inserted into table_region that don't have a valid country/region mapping






share|improve this answer








New contributor




Caius Jard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






$endgroup$









  • 1




    $begingroup$
    You have presented an alternative solution, but haven't reviewed the code. Please edit to show what aspects of the question code prompted you to write this version, and in what ways it's an improvement over the original. It may be worth (re-)reading How to Answer.
    $endgroup$
    – Toby Speight
    17 hours ago














-2












-2








-2





$begingroup$

Here's a one hit way of doing this:



    UPDATE t
SET
a_region = ra.region_code,
b_region = rb.region_code,
c_region = rc.region_code,
d_region = rd.region_code,
e_region = re.region_code,
f_region = rf.region_code
FROM
table_region t
LEFT OUTER JOIN mapping.region ra WHERE t.country = ra.country AND t.a_region = ra.region_code
LEFT OUTER JOIN mapping.region rb WHERE t.country = rb.country AND t.b_region = rb.region_code
LEFT OUTER JOIN mapping.region rc WHERE t.country = rc.country AND t.c_region = rc.region_code
LEFT OUTER JOIN mapping.region rd WHERE t.country = rd.country AND t.d_region = rd.region_code
LEFT OUTER JOIN mapping.region re WHERE t.country = re.country AND t.e_region = re.region_code
LEFT OUTER JOIN mapping.region rf WHERE t.country = rf.country AND t.f_region = rf.region_code


It basically works by left joining the region table 6 times, once for each of the *_region columns.




  • If the relation works out, then the r*.region_code will be populated with a value (which means the *_region column is set to the same value it currently is, i.e. a non-op).

  • If it doesn't work out, then the join fails, null is present in r*.region_code


After you run this as a one time fix, to identify the ones that are failed (maybe you should remove them to another table) you should consider making table_region(country,*_region) have multiple foreign keys to mapping.region(country,region_code) to ensure that records cannot be inserted into table_region that don't have a valid country/region mapping






share|improve this answer








New contributor




Caius Jard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






$endgroup$



Here's a one hit way of doing this:



    UPDATE t
SET
a_region = ra.region_code,
b_region = rb.region_code,
c_region = rc.region_code,
d_region = rd.region_code,
e_region = re.region_code,
f_region = rf.region_code
FROM
table_region t
LEFT OUTER JOIN mapping.region ra WHERE t.country = ra.country AND t.a_region = ra.region_code
LEFT OUTER JOIN mapping.region rb WHERE t.country = rb.country AND t.b_region = rb.region_code
LEFT OUTER JOIN mapping.region rc WHERE t.country = rc.country AND t.c_region = rc.region_code
LEFT OUTER JOIN mapping.region rd WHERE t.country = rd.country AND t.d_region = rd.region_code
LEFT OUTER JOIN mapping.region re WHERE t.country = re.country AND t.e_region = re.region_code
LEFT OUTER JOIN mapping.region rf WHERE t.country = rf.country AND t.f_region = rf.region_code


It basically works by left joining the region table 6 times, once for each of the *_region columns.




  • If the relation works out, then the r*.region_code will be populated with a value (which means the *_region column is set to the same value it currently is, i.e. a non-op).

  • If it doesn't work out, then the join fails, null is present in r*.region_code


After you run this as a one time fix, to identify the ones that are failed (maybe you should remove them to another table) you should consider making table_region(country,*_region) have multiple foreign keys to mapping.region(country,region_code) to ensure that records cannot be inserted into table_region that don't have a valid country/region mapping







share|improve this answer








New contributor




Caius Jard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this answer



share|improve this answer






New contributor




Caius Jard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









answered 18 hours ago









Caius JardCaius Jard

97




97




New contributor




Caius Jard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Caius Jard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Caius Jard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 1




    $begingroup$
    You have presented an alternative solution, but haven't reviewed the code. Please edit to show what aspects of the question code prompted you to write this version, and in what ways it's an improvement over the original. It may be worth (re-)reading How to Answer.
    $endgroup$
    – Toby Speight
    17 hours ago














  • 1




    $begingroup$
    You have presented an alternative solution, but haven't reviewed the code. Please edit to show what aspects of the question code prompted you to write this version, and in what ways it's an improvement over the original. It may be worth (re-)reading How to Answer.
    $endgroup$
    – Toby Speight
    17 hours ago








1




1




$begingroup$
You have presented an alternative solution, but haven't reviewed the code. Please edit to show what aspects of the question code prompted you to write this version, and in what ways it's an improvement over the original. It may be worth (re-)reading How to Answer.
$endgroup$
– Toby Speight
17 hours ago




$begingroup$
You have presented an alternative solution, but haven't reviewed the code. Please edit to show what aspects of the question code prompted you to write this version, and in what ways it's an improvement over the original. It may be worth (re-)reading How to Answer.
$endgroup$
– Toby Speight
17 hours ago


















draft saved

draft discarded




















































Thanks for contributing an answer to Code Review Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f210885%2fvalidating-region-state-province-codes-for-different-countries%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'