How to add check constraint for flag column when we don't know exact format in sql?











up vote
1
down vote

favorite
1












enter image description here



Here first letter should be an alpha, column may contain any number of data separated by " / " ,for every stating letter I want alphabet, after first letter it may contain 3 or 4 letter. How to do this dynamically.



I am thinking of... first i can find position of " / " add it to one, here we will get alphabet position then I can check whether it is an alpha or numerical character










share|improve this question




















  • 4




    For some reason, this looks like a sequence of codes to me. If so, you should not be storing them in a string. You should have a separate row for each code.
    – Gordon Linoff
    Nov 7 at 12:45






  • 2




    You can do this in T-SQL, but not without a function, and not efficiently. Gordon is right on the money: if it's possible to store these "flags" in a properly normalized way, you should do so. A check for each individual component is much simpler, as it avoids the hairy problem of string splitting in T-SQL.
    – Jeroen Mostert
    Nov 7 at 12:49






  • 1




    You could do this with a check constraint, however, you're going to need to create a scalar function to do so, which could ruin performance of your INSERT/UPDATE statements. Ideally, as @GordonLinoff recommended, you need to normalise your data. I would also recommend tagging the version of SQL Server you are using.
    – Larnu
    Nov 7 at 12:50










  • @JeroenMostert you just beat me to it.
    – Larnu
    Nov 7 at 12:50






  • 4




    And as a bonus: Read Is storing a delimited list in a database column really that bad?, where you will see a lot of reasons why the answer to this question is Absolutely yes!
    – Zohar Peled
    Nov 7 at 12:54















up vote
1
down vote

favorite
1












enter image description here



Here first letter should be an alpha, column may contain any number of data separated by " / " ,for every stating letter I want alphabet, after first letter it may contain 3 or 4 letter. How to do this dynamically.



I am thinking of... first i can find position of " / " add it to one, here we will get alphabet position then I can check whether it is an alpha or numerical character










share|improve this question




















  • 4




    For some reason, this looks like a sequence of codes to me. If so, you should not be storing them in a string. You should have a separate row for each code.
    – Gordon Linoff
    Nov 7 at 12:45






  • 2




    You can do this in T-SQL, but not without a function, and not efficiently. Gordon is right on the money: if it's possible to store these "flags" in a properly normalized way, you should do so. A check for each individual component is much simpler, as it avoids the hairy problem of string splitting in T-SQL.
    – Jeroen Mostert
    Nov 7 at 12:49






  • 1




    You could do this with a check constraint, however, you're going to need to create a scalar function to do so, which could ruin performance of your INSERT/UPDATE statements. Ideally, as @GordonLinoff recommended, you need to normalise your data. I would also recommend tagging the version of SQL Server you are using.
    – Larnu
    Nov 7 at 12:50










  • @JeroenMostert you just beat me to it.
    – Larnu
    Nov 7 at 12:50






  • 4




    And as a bonus: Read Is storing a delimited list in a database column really that bad?, where you will see a lot of reasons why the answer to this question is Absolutely yes!
    – Zohar Peled
    Nov 7 at 12:54













up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





enter image description here



Here first letter should be an alpha, column may contain any number of data separated by " / " ,for every stating letter I want alphabet, after first letter it may contain 3 or 4 letter. How to do this dynamically.



I am thinking of... first i can find position of " / " add it to one, here we will get alphabet position then I can check whether it is an alpha or numerical character










share|improve this question















enter image description here



Here first letter should be an alpha, column may contain any number of data separated by " / " ,for every stating letter I want alphabet, after first letter it may contain 3 or 4 letter. How to do this dynamically.



I am thinking of... first i can find position of " / " add it to one, here we will get alphabet position then I can check whether it is an alpha or numerical character







sql sql-server tsql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 7 at 12:52









Larnu

13.5k31430




13.5k31430










asked Nov 7 at 12:41









sachi

154




154








  • 4




    For some reason, this looks like a sequence of codes to me. If so, you should not be storing them in a string. You should have a separate row for each code.
    – Gordon Linoff
    Nov 7 at 12:45






  • 2




    You can do this in T-SQL, but not without a function, and not efficiently. Gordon is right on the money: if it's possible to store these "flags" in a properly normalized way, you should do so. A check for each individual component is much simpler, as it avoids the hairy problem of string splitting in T-SQL.
    – Jeroen Mostert
    Nov 7 at 12:49






  • 1




    You could do this with a check constraint, however, you're going to need to create a scalar function to do so, which could ruin performance of your INSERT/UPDATE statements. Ideally, as @GordonLinoff recommended, you need to normalise your data. I would also recommend tagging the version of SQL Server you are using.
    – Larnu
    Nov 7 at 12:50










  • @JeroenMostert you just beat me to it.
    – Larnu
    Nov 7 at 12:50






  • 4




    And as a bonus: Read Is storing a delimited list in a database column really that bad?, where you will see a lot of reasons why the answer to this question is Absolutely yes!
    – Zohar Peled
    Nov 7 at 12:54














  • 4




    For some reason, this looks like a sequence of codes to me. If so, you should not be storing them in a string. You should have a separate row for each code.
    – Gordon Linoff
    Nov 7 at 12:45






  • 2




    You can do this in T-SQL, but not without a function, and not efficiently. Gordon is right on the money: if it's possible to store these "flags" in a properly normalized way, you should do so. A check for each individual component is much simpler, as it avoids the hairy problem of string splitting in T-SQL.
    – Jeroen Mostert
    Nov 7 at 12:49






  • 1




    You could do this with a check constraint, however, you're going to need to create a scalar function to do so, which could ruin performance of your INSERT/UPDATE statements. Ideally, as @GordonLinoff recommended, you need to normalise your data. I would also recommend tagging the version of SQL Server you are using.
    – Larnu
    Nov 7 at 12:50










  • @JeroenMostert you just beat me to it.
    – Larnu
    Nov 7 at 12:50






  • 4




    And as a bonus: Read Is storing a delimited list in a database column really that bad?, where you will see a lot of reasons why the answer to this question is Absolutely yes!
    – Zohar Peled
    Nov 7 at 12:54








4




4




For some reason, this looks like a sequence of codes to me. If so, you should not be storing them in a string. You should have a separate row for each code.
– Gordon Linoff
Nov 7 at 12:45




For some reason, this looks like a sequence of codes to me. If so, you should not be storing them in a string. You should have a separate row for each code.
– Gordon Linoff
Nov 7 at 12:45




2




2




You can do this in T-SQL, but not without a function, and not efficiently. Gordon is right on the money: if it's possible to store these "flags" in a properly normalized way, you should do so. A check for each individual component is much simpler, as it avoids the hairy problem of string splitting in T-SQL.
– Jeroen Mostert
Nov 7 at 12:49




You can do this in T-SQL, but not without a function, and not efficiently. Gordon is right on the money: if it's possible to store these "flags" in a properly normalized way, you should do so. A check for each individual component is much simpler, as it avoids the hairy problem of string splitting in T-SQL.
– Jeroen Mostert
Nov 7 at 12:49




1




1




You could do this with a check constraint, however, you're going to need to create a scalar function to do so, which could ruin performance of your INSERT/UPDATE statements. Ideally, as @GordonLinoff recommended, you need to normalise your data. I would also recommend tagging the version of SQL Server you are using.
– Larnu
Nov 7 at 12:50




You could do this with a check constraint, however, you're going to need to create a scalar function to do so, which could ruin performance of your INSERT/UPDATE statements. Ideally, as @GordonLinoff recommended, you need to normalise your data. I would also recommend tagging the version of SQL Server you are using.
– Larnu
Nov 7 at 12:50












@JeroenMostert you just beat me to it.
– Larnu
Nov 7 at 12:50




@JeroenMostert you just beat me to it.
– Larnu
Nov 7 at 12:50




4




4




And as a bonus: Read Is storing a delimited list in a database column really that bad?, where you will see a lot of reasons why the answer to this question is Absolutely yes!
– Zohar Peled
Nov 7 at 12:54




And as a bonus: Read Is storing a delimited list in a database column really that bad?, where you will see a lot of reasons why the answer to this question is Absolutely yes!
– Zohar Peled
Nov 7 at 12:54












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










You should take on board the comments about normalisation - however you should be able to do this without a UDF.



CREATE TABLE #T
(
Flag VARCHAR(100) CHECK (
/*Only contains alpha, digits and slash*/
Flag NOT LIKE '%[^a-zA-Z0-9/]%' COLLATE Latin1_General_100_BIN2 AND
/*Doesn't end in a slash*/
Flag NOT LIKE '%/' AND
/*Start of the string and every slash should be followed by an alpha*/
'/' + Flag NOT LIKE '%/[^a-zA-Z]%' COLLATE Latin1_General_100_BIN2 AND
/*Should never have a digit immediately followed by an alpha*/
Flag NOT LIKE '%[0-9][a-zA-Z]%' COLLATE Latin1_General_100_BIN2 AND
/*Should never have two alpha in a row*/
Flag NOT LIKE '%[a-zA-Z][a-zA-Z]%' COLLATE Latin1_General_100_BIN2 AND
/*Should never have 5 digits in a row*/
Flag NOT LIKE '%[0-9][0-9][0-9][0-9][0-9]%' COLLATE Latin1_General_100_BIN2 AND
/*Should not have codes of less than 4 characters*/
'/' + Flag + '/' NOT LIKE '%/_/%' AND
'/' + Flag + '/' NOT LIKE '%/__/%' AND
'/' + Flag + '/' NOT LIKE '%/___/%'
)
);





share|improve this answer



















  • 1




    I'd upvote this, but technically correct or not, this is the sort of "too clever by half" thing I hope to never see in a database. It's just too hard to maintain if the rules ever change, and too hard to check for edge conditions. (Point in case: the current version deems 'A000/' to be correct, which is not consistent with deeming '/A000' to be incorrect.)
    – Jeroen Mostert
    Nov 7 at 14:02






  • 1




    @JeroenMostert - yep would need a lot of tSQLt tests. And of course the OP likely shouldn't be storing it in the first place in this format
    – Martin Smith
    Nov 7 at 15:58






  • 1




    @JeroenMostert I agree that this type of solution can be kludgy but is not bad provided that rules don't change too often. I appreciate Martin demonstrating an alternative to using a T-SQL scalar UDF which would be a profoundly worse solution. I would probably have a condition for NOT LIKE '[^a-zA-Z]%' which addresses your /A000 example and a number of edge cases.
    – Alan Burstein
    Nov 7 at 22:00








  • 1




    @AlanBurstein - though the scalar UDF solution could be a good one soon. blogs.msdn.microsoft.com/sqlserverstorageengine/2018/11/07/…
    – Martin Smith
    Nov 8 at 9:29










  • @MartinSmith - thanks for posting that - great read and great news!
    – Alan Burstein
    Nov 8 at 15:12











Your Answer






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: "1"
};
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',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fstackoverflow.com%2fquestions%2f53189680%2fhow-to-add-check-constraint-for-flag-column-when-we-dont-know-exact-format-in-s%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








up vote
0
down vote



accepted










You should take on board the comments about normalisation - however you should be able to do this without a UDF.



CREATE TABLE #T
(
Flag VARCHAR(100) CHECK (
/*Only contains alpha, digits and slash*/
Flag NOT LIKE '%[^a-zA-Z0-9/]%' COLLATE Latin1_General_100_BIN2 AND
/*Doesn't end in a slash*/
Flag NOT LIKE '%/' AND
/*Start of the string and every slash should be followed by an alpha*/
'/' + Flag NOT LIKE '%/[^a-zA-Z]%' COLLATE Latin1_General_100_BIN2 AND
/*Should never have a digit immediately followed by an alpha*/
Flag NOT LIKE '%[0-9][a-zA-Z]%' COLLATE Latin1_General_100_BIN2 AND
/*Should never have two alpha in a row*/
Flag NOT LIKE '%[a-zA-Z][a-zA-Z]%' COLLATE Latin1_General_100_BIN2 AND
/*Should never have 5 digits in a row*/
Flag NOT LIKE '%[0-9][0-9][0-9][0-9][0-9]%' COLLATE Latin1_General_100_BIN2 AND
/*Should not have codes of less than 4 characters*/
'/' + Flag + '/' NOT LIKE '%/_/%' AND
'/' + Flag + '/' NOT LIKE '%/__/%' AND
'/' + Flag + '/' NOT LIKE '%/___/%'
)
);





share|improve this answer



















  • 1




    I'd upvote this, but technically correct or not, this is the sort of "too clever by half" thing I hope to never see in a database. It's just too hard to maintain if the rules ever change, and too hard to check for edge conditions. (Point in case: the current version deems 'A000/' to be correct, which is not consistent with deeming '/A000' to be incorrect.)
    – Jeroen Mostert
    Nov 7 at 14:02






  • 1




    @JeroenMostert - yep would need a lot of tSQLt tests. And of course the OP likely shouldn't be storing it in the first place in this format
    – Martin Smith
    Nov 7 at 15:58






  • 1




    @JeroenMostert I agree that this type of solution can be kludgy but is not bad provided that rules don't change too often. I appreciate Martin demonstrating an alternative to using a T-SQL scalar UDF which would be a profoundly worse solution. I would probably have a condition for NOT LIKE '[^a-zA-Z]%' which addresses your /A000 example and a number of edge cases.
    – Alan Burstein
    Nov 7 at 22:00








  • 1




    @AlanBurstein - though the scalar UDF solution could be a good one soon. blogs.msdn.microsoft.com/sqlserverstorageengine/2018/11/07/…
    – Martin Smith
    Nov 8 at 9:29










  • @MartinSmith - thanks for posting that - great read and great news!
    – Alan Burstein
    Nov 8 at 15:12















up vote
0
down vote



accepted










You should take on board the comments about normalisation - however you should be able to do this without a UDF.



CREATE TABLE #T
(
Flag VARCHAR(100) CHECK (
/*Only contains alpha, digits and slash*/
Flag NOT LIKE '%[^a-zA-Z0-9/]%' COLLATE Latin1_General_100_BIN2 AND
/*Doesn't end in a slash*/
Flag NOT LIKE '%/' AND
/*Start of the string and every slash should be followed by an alpha*/
'/' + Flag NOT LIKE '%/[^a-zA-Z]%' COLLATE Latin1_General_100_BIN2 AND
/*Should never have a digit immediately followed by an alpha*/
Flag NOT LIKE '%[0-9][a-zA-Z]%' COLLATE Latin1_General_100_BIN2 AND
/*Should never have two alpha in a row*/
Flag NOT LIKE '%[a-zA-Z][a-zA-Z]%' COLLATE Latin1_General_100_BIN2 AND
/*Should never have 5 digits in a row*/
Flag NOT LIKE '%[0-9][0-9][0-9][0-9][0-9]%' COLLATE Latin1_General_100_BIN2 AND
/*Should not have codes of less than 4 characters*/
'/' + Flag + '/' NOT LIKE '%/_/%' AND
'/' + Flag + '/' NOT LIKE '%/__/%' AND
'/' + Flag + '/' NOT LIKE '%/___/%'
)
);





share|improve this answer



















  • 1




    I'd upvote this, but technically correct or not, this is the sort of "too clever by half" thing I hope to never see in a database. It's just too hard to maintain if the rules ever change, and too hard to check for edge conditions. (Point in case: the current version deems 'A000/' to be correct, which is not consistent with deeming '/A000' to be incorrect.)
    – Jeroen Mostert
    Nov 7 at 14:02






  • 1




    @JeroenMostert - yep would need a lot of tSQLt tests. And of course the OP likely shouldn't be storing it in the first place in this format
    – Martin Smith
    Nov 7 at 15:58






  • 1




    @JeroenMostert I agree that this type of solution can be kludgy but is not bad provided that rules don't change too often. I appreciate Martin demonstrating an alternative to using a T-SQL scalar UDF which would be a profoundly worse solution. I would probably have a condition for NOT LIKE '[^a-zA-Z]%' which addresses your /A000 example and a number of edge cases.
    – Alan Burstein
    Nov 7 at 22:00








  • 1




    @AlanBurstein - though the scalar UDF solution could be a good one soon. blogs.msdn.microsoft.com/sqlserverstorageengine/2018/11/07/…
    – Martin Smith
    Nov 8 at 9:29










  • @MartinSmith - thanks for posting that - great read and great news!
    – Alan Burstein
    Nov 8 at 15:12













up vote
0
down vote



accepted







up vote
0
down vote



accepted






You should take on board the comments about normalisation - however you should be able to do this without a UDF.



CREATE TABLE #T
(
Flag VARCHAR(100) CHECK (
/*Only contains alpha, digits and slash*/
Flag NOT LIKE '%[^a-zA-Z0-9/]%' COLLATE Latin1_General_100_BIN2 AND
/*Doesn't end in a slash*/
Flag NOT LIKE '%/' AND
/*Start of the string and every slash should be followed by an alpha*/
'/' + Flag NOT LIKE '%/[^a-zA-Z]%' COLLATE Latin1_General_100_BIN2 AND
/*Should never have a digit immediately followed by an alpha*/
Flag NOT LIKE '%[0-9][a-zA-Z]%' COLLATE Latin1_General_100_BIN2 AND
/*Should never have two alpha in a row*/
Flag NOT LIKE '%[a-zA-Z][a-zA-Z]%' COLLATE Latin1_General_100_BIN2 AND
/*Should never have 5 digits in a row*/
Flag NOT LIKE '%[0-9][0-9][0-9][0-9][0-9]%' COLLATE Latin1_General_100_BIN2 AND
/*Should not have codes of less than 4 characters*/
'/' + Flag + '/' NOT LIKE '%/_/%' AND
'/' + Flag + '/' NOT LIKE '%/__/%' AND
'/' + Flag + '/' NOT LIKE '%/___/%'
)
);





share|improve this answer














You should take on board the comments about normalisation - however you should be able to do this without a UDF.



CREATE TABLE #T
(
Flag VARCHAR(100) CHECK (
/*Only contains alpha, digits and slash*/
Flag NOT LIKE '%[^a-zA-Z0-9/]%' COLLATE Latin1_General_100_BIN2 AND
/*Doesn't end in a slash*/
Flag NOT LIKE '%/' AND
/*Start of the string and every slash should be followed by an alpha*/
'/' + Flag NOT LIKE '%/[^a-zA-Z]%' COLLATE Latin1_General_100_BIN2 AND
/*Should never have a digit immediately followed by an alpha*/
Flag NOT LIKE '%[0-9][a-zA-Z]%' COLLATE Latin1_General_100_BIN2 AND
/*Should never have two alpha in a row*/
Flag NOT LIKE '%[a-zA-Z][a-zA-Z]%' COLLATE Latin1_General_100_BIN2 AND
/*Should never have 5 digits in a row*/
Flag NOT LIKE '%[0-9][0-9][0-9][0-9][0-9]%' COLLATE Latin1_General_100_BIN2 AND
/*Should not have codes of less than 4 characters*/
'/' + Flag + '/' NOT LIKE '%/_/%' AND
'/' + Flag + '/' NOT LIKE '%/__/%' AND
'/' + Flag + '/' NOT LIKE '%/___/%'
)
);






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 7 at 22:04

























answered Nov 7 at 13:36









Martin Smith

338k58566673




338k58566673








  • 1




    I'd upvote this, but technically correct or not, this is the sort of "too clever by half" thing I hope to never see in a database. It's just too hard to maintain if the rules ever change, and too hard to check for edge conditions. (Point in case: the current version deems 'A000/' to be correct, which is not consistent with deeming '/A000' to be incorrect.)
    – Jeroen Mostert
    Nov 7 at 14:02






  • 1




    @JeroenMostert - yep would need a lot of tSQLt tests. And of course the OP likely shouldn't be storing it in the first place in this format
    – Martin Smith
    Nov 7 at 15:58






  • 1




    @JeroenMostert I agree that this type of solution can be kludgy but is not bad provided that rules don't change too often. I appreciate Martin demonstrating an alternative to using a T-SQL scalar UDF which would be a profoundly worse solution. I would probably have a condition for NOT LIKE '[^a-zA-Z]%' which addresses your /A000 example and a number of edge cases.
    – Alan Burstein
    Nov 7 at 22:00








  • 1




    @AlanBurstein - though the scalar UDF solution could be a good one soon. blogs.msdn.microsoft.com/sqlserverstorageengine/2018/11/07/…
    – Martin Smith
    Nov 8 at 9:29










  • @MartinSmith - thanks for posting that - great read and great news!
    – Alan Burstein
    Nov 8 at 15:12














  • 1




    I'd upvote this, but technically correct or not, this is the sort of "too clever by half" thing I hope to never see in a database. It's just too hard to maintain if the rules ever change, and too hard to check for edge conditions. (Point in case: the current version deems 'A000/' to be correct, which is not consistent with deeming '/A000' to be incorrect.)
    – Jeroen Mostert
    Nov 7 at 14:02






  • 1




    @JeroenMostert - yep would need a lot of tSQLt tests. And of course the OP likely shouldn't be storing it in the first place in this format
    – Martin Smith
    Nov 7 at 15:58






  • 1




    @JeroenMostert I agree that this type of solution can be kludgy but is not bad provided that rules don't change too often. I appreciate Martin demonstrating an alternative to using a T-SQL scalar UDF which would be a profoundly worse solution. I would probably have a condition for NOT LIKE '[^a-zA-Z]%' which addresses your /A000 example and a number of edge cases.
    – Alan Burstein
    Nov 7 at 22:00








  • 1




    @AlanBurstein - though the scalar UDF solution could be a good one soon. blogs.msdn.microsoft.com/sqlserverstorageengine/2018/11/07/…
    – Martin Smith
    Nov 8 at 9:29










  • @MartinSmith - thanks for posting that - great read and great news!
    – Alan Burstein
    Nov 8 at 15:12








1




1




I'd upvote this, but technically correct or not, this is the sort of "too clever by half" thing I hope to never see in a database. It's just too hard to maintain if the rules ever change, and too hard to check for edge conditions. (Point in case: the current version deems 'A000/' to be correct, which is not consistent with deeming '/A000' to be incorrect.)
– Jeroen Mostert
Nov 7 at 14:02




I'd upvote this, but technically correct or not, this is the sort of "too clever by half" thing I hope to never see in a database. It's just too hard to maintain if the rules ever change, and too hard to check for edge conditions. (Point in case: the current version deems 'A000/' to be correct, which is not consistent with deeming '/A000' to be incorrect.)
– Jeroen Mostert
Nov 7 at 14:02




1




1




@JeroenMostert - yep would need a lot of tSQLt tests. And of course the OP likely shouldn't be storing it in the first place in this format
– Martin Smith
Nov 7 at 15:58




@JeroenMostert - yep would need a lot of tSQLt tests. And of course the OP likely shouldn't be storing it in the first place in this format
– Martin Smith
Nov 7 at 15:58




1




1




@JeroenMostert I agree that this type of solution can be kludgy but is not bad provided that rules don't change too often. I appreciate Martin demonstrating an alternative to using a T-SQL scalar UDF which would be a profoundly worse solution. I would probably have a condition for NOT LIKE '[^a-zA-Z]%' which addresses your /A000 example and a number of edge cases.
– Alan Burstein
Nov 7 at 22:00






@JeroenMostert I agree that this type of solution can be kludgy but is not bad provided that rules don't change too often. I appreciate Martin demonstrating an alternative to using a T-SQL scalar UDF which would be a profoundly worse solution. I would probably have a condition for NOT LIKE '[^a-zA-Z]%' which addresses your /A000 example and a number of edge cases.
– Alan Burstein
Nov 7 at 22:00






1




1




@AlanBurstein - though the scalar UDF solution could be a good one soon. blogs.msdn.microsoft.com/sqlserverstorageengine/2018/11/07/…
– Martin Smith
Nov 8 at 9:29




@AlanBurstein - though the scalar UDF solution could be a good one soon. blogs.msdn.microsoft.com/sqlserverstorageengine/2018/11/07/…
– Martin Smith
Nov 8 at 9:29












@MartinSmith - thanks for posting that - great read and great news!
– Alan Burstein
Nov 8 at 15:12




@MartinSmith - thanks for posting that - great read and great news!
– Alan Burstein
Nov 8 at 15:12


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53189680%2fhow-to-add-check-constraint-for-flag-column-when-we-dont-know-exact-format-in-s%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







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini