T-SQL Pattern Match/Regex 0 or 1 occurrences of square bracket - tsql

I've been trying to match, using LIKE or PATINDEX where a string of format
[subdomain.domain.com].[dbo].[MyDatabase]
occurs in a SQL string
While the string [subdomain.domain.com] will always appear in this format
the [dbo] and/or [MyDatabase] may appear with or without the square brackets
I have tried the following
WHERE SQLString LIKE '%[[]subdomain.domain.com[]].[[]{0,1}dbo[]]{0,1}.[[]{0,1}MyDatabase[]]{0,1}%'
or
WHERE SQLString LIKE '%\[subdomain.domain.com\].\[{0,1}dbo\]{0,1}.\[{0,1}MyDatabase\]{0,1}%' ESCAPE '\'
I could create multiple WHERE OR WHERE statements, covering each possiblity, but would like to understand why the regex isn't working, in this case.

The square bracket is a special character in the like clause, that means "Any single character within the specified range (for example [a-z])."
You could just use the % special character wherever there might be a square bracket in your text. Something like:
WHERE SQLString LIKE '%subdomain.domain.com%.%dbo%.%MyDatabase%'
Since that will match "Any string of zero or more characters", if there is a square bracket there, great, if not, it still matches.

Related

Lines that have three upper case characters in parenthesis like (LTI) - remember to escape certain characters :)

In this assignment you will create a regular expression to retrieve a subset data from the purpose column of the taxdata table in the readonly database (access details below). Write a regular expressions to retrieve that meet the following criteria:
Lines that have three upper case characters in parenthesis like (LTI) - remember to escape certain characters :)
Lines that have three upper case characters in parenthesis like (LTI) - remember to escape certain characters :)

Difficulty forming a regular expression

I'm trying to check a string to make sure that it only contains lowercase and uppercased letters, the digits 0-9, underscores, dashes and periods.
The regular expression I've been using for letter, numbers, underscores and dashes works fine and is this:
"[^a-zA-Z0-9_-]"
I'm having difficulty adding the check for spaces and periods though.
I've tried:
"[^a-zA-Z0-9_- ]" (added a space after the dash)
"[^a-zA-Z0-9_-\s\.]" (trying to escape a white space character)
I've also tried putting the \s and \. outside of the main block and also in blocks of their own.
Thanks for any advice.
A hyphen (representing the character) must be at the beginning or at the end of the (negating) character class.
Inside a character class the period is a normal character, it doesn't need to be escaped.
let pattern = "[^a-zA-Z0-9_. -]+"
Be careful about adding characters which have a special meaning: you forgot the hyphen.
I think that this is what you are looking for:
"[\^ a-zA-Z0-9_,\.\-]"

T-SQL Procedure parameter name

I have to create a procedure with same parameters names as excel columns. Some loook like this 'xxx/xxx' or 'xxx - xxx'. Is there any work around to name parameteres in a stored procedure like this?
Forward slash (/) or dash (-) are not allowed in variable names
According to http://msdn.microsoft.com/en-us/library/ms175874.aspx, that means that the allowed characters are:
Letters as defined in the Unicode Standard 3.2. The Unicode
definition of letters includes Latin characters from a through z,
from A through Z, and also letter characters from other languages.
Decimal numbers from either Basic Latin or other national scripts.
The at sign (#), dollar sign ($), number sign (#), or underscore (_).
Okay first of all why would you ever want to use special characters? That is like saying I want to fry toast underwater with electricity, why can't I have an outlet to allow that? Special characters denote special things and as such many engines, not just in SQL, but most languages will not allow reserved characters for use in variables. The best you could do was put in parameters with the reversed '_' and then replace that AFTER the object was already created for echoing out. The placeholder name of '#(something)' is really arbitrary and could be #X or #LookAtMe. It's type is important to form a contract that must be fulfilled for execution but the naming is really for hooking up. Having said that if you just must have these weird names echoed out you could do something like this:
CREATE PROC pSimpleParam #My_Param INT
AS
SELECT #My_Param
GO
ALTER PROC pSimpleParam #My_Param INT
AS
SELECT
pr.name AS ParameterName
, REPLACE(pr.name, '_', '-') AS AlteredParameterName
FROM sys.procedures p
INNER JOIN sys.parameters pr ON pr.object_id = p.object_id
AND p.name = 'pSimpleParam'
GO

Check Half width Katakana character in COBOL

I'm working on RedHat6 and using COBOL. I wanna check every single digit of variable, if it's half width --> CONTINUE, Else --> DISPLAY ERROR. Basicly I list all half width characters in WHEN Clause of EVALUATE statement. Like this:
PERFORM VARYING WK-IX FROM 1 BY 1 UNTIL WK-IX > WK-LENGTH
EVALUATE WK-FORMAT-CHK-VALUE(WK-IX:1)
WHEN 'A'
WHEN 'B'
WHEN 'C'
CONTINUE
WHEN OTHER
DISPLAY 'ERROR'
END-EVALUATE
END-PERFORM.
Everything is OK but when compile I have problem with half width katakana character. It said: "The ending quotation mark of the literal is missing. The characters at the end of Area B are assumed to be a literal" with all line check these character:
ツ テ ト ナ ニ ヌ ネ ノ ハ ヒ フ ヘ ホ マ ミ ム メ モ ヤ ユ ヨ ラ リ ル レ ロ ワ ヲ ン
Although I sure there isn't any line of code miss the ending quotation mark. Like this:
WHEN 'ツ'
WHEN 'テ'
WHEN 'ト'
But these character is OK and I don't know why:
ア イ ウ エ オ カ キ ク ケ コ サ シ ス セ ソ タ チ
Anyone can help me? Please!
Sorry for my bad English!
Because the Katakana character set is considered a multi-byte character set (as mentioned by Bill Woodger), you will need to ensure that the NSYMBOL and DBCS compile options are enabled. After that, you should be able to define the literals like this:
EVALUATE WK-FORMAT-CHK-VALUE(WK-IX:1)
WHEN N'ツ'
WHEN N'テ'
WHEN N'ト'
do something
WHEN OTHER
do something else
END-EVALUATE
the N will tell the program that this is a national character and as such is multi-byte.
Your input to the evaluate clause will also need to be defined as a PIC N rather than a PIC X. A PIC X field will not recognise double byte characters.

Is it possible to change an emacs syntax table based on context?

I'm working on improving an emacs major mode for UnrealScript. One of the (many) quirks is that it allows syntax like this for specifying tooltips in the Unreal editor:
var() int MyEditorVar <Foo=Bar|Tooltip=My tooltip text isn't quoted>;
The angle brackets after the variable declaration denote a pipe-separated list of Key=Value metadata pairs, and the metadata is not quoted but can contain quote marks -- a pipe (|) or right angle bracket (>) denotes the end.
Is there a way I can get the emacs syntax table to recognize this context-dependent syntax in a useful way? I'd like everything except for pipes and right angle brackets to be highlighted in some way inside of these variable metadata declarations, but otherwise retain their normal highlighting.
Right now, the single quote character is set up to be a quote delimiter (syntax designator "), so font-lock-mode interprets such a quote as starting a quoted string, which it's not in this very specific instance, so it mishighlights everything until it finds another supposedly matching single quote.
You'll need to setup a syntax-propertize-function which lets you apply different syntax designators to different characters in the buffer, depending on their context.
Grep for syntax-propertize-function in Emacs's lisp directory to see various examples (from simple to pretty complex ones).
You'll probably want to mark the "=" chars after your "Foo" and after your "Tooltip" as "generic string delimiter", then do the same with the corresponding terminating "|" and ">". An alternative could be to mark the char before the ">" as a (closing) generic string delimiter, so that you can then mark the "<" and ">" as open&close parens.