QUOTENAME() with parenthesis, curly brackets, angle brackets, or grave accents - sql-server

While reading the documentation for QUOTENAME(), I see that it supports a variety of delimiter characters:
Square brackets - [] (default)
Parenthesis - ()
Curly brackets - {}
Angle brackets - <>
Single quotes - '
Double quotes - "
Grave accents - `
In addition to wrapping the content in opening and closing delimiters, QUOTENAME() appears to double up any contained characters that match the closing delimiter. For example: QUOTENAME('matrix[2][3]') yields [matrix[2]][3]]] and QUOTENAME('cell(2)(3)', '(') yields (cell(2))(3))).
While I understand the usage of single quotes, double quotes, and square brackets to delimit identifiers and text values (primarily when constructing dynamic SQL), I am not sure what use the other forms might have.
Are there any places in SQL Server syntax where parenthesis, curly bracket, angle bracket, or grave accent delimited content has any special meaning? Are there any languages (XML, JSON, etc.) outside of SQL where those other forms might be useful? Such cases would have to recognize the doubled character as an escape.
This is more a question of interest rather than a particular problem that I am trying to solve.

Related

Google Sheets Regexreplace - remove numbers, double quotes, "X" and "x" from strings

I have a listing of products that include sizes, in a variety of formats.
I need to separate them into 2 columns, 1 with the sizes, the other with the names.
So, for example, "12"x10" Large Widget" becomes [12" x 10"] [Large Widget].
I'm trying to use REGEXREPLACE to remove the numbers, but can't seem to find a combined syntax that will handle "any number, double quotes, the letter X lowercase and capital" in a single expression.
And then I'd need to do the reverse, in other words, extract JUST the numbers and double quotes and X/x, for the other column.
Can't just use SPLIT or RIGHT/LEFT, because the position of the intended split will differ from line to line. Can't "Split Text To Columns", because there are occasional double quotes in the item names too.
So I'm thinking, Step 1: filter out just the numbers and double quotes, Step 2: filter out just the names.
In other words:
Input: [14" x 17" Large Widget]
Output (2 columns): [14" x 17"] [Large Widget]
Input: [7"x2" Small Gadget]
Output (2 columns): [7"x2"] [Small Gadget]
I understand how REGEXREPLACE works, just need help with the syntax.
Thanks in advance!
try:
=ARRAYFORMULA(IFNA(REGEXEXTRACT(A1:A, "(.+"") (.+)")))

SQL [int] versus int

Recently I am working with SQL language. I found two ways of declaring temporary table variables(starting with #). My question is:
Is there any difference between them?
DECLARE
#variable1 [int],
#variable2 int
There is no difference in this instance. However, the square brackets are used as delimiters if you have special characters that need identifying - for example if you name a column "First Name", you'll need square brackets to escape the space character.
Nope those are the same. Typing it with the brackets is just extra keystrokes.

Extra rows for custom expression to display "filtered" sum

So I have a table that gets its resultset from a procedure and everything looks great, including the totals at the bottom. The user has asked for a few custom rows at the bottom that are only to represent the totals for specific criteria or group. Say you have a field called AuctionPrice. In the dataset it is from an aggregate within a sql procedure.
If I place an extra row at the bottom of the table and the "AuctionPrice" field, I use an expression like this
=SUM(IIF(Fields!AuctionPrice > 0, Fields!AuctionPrice, 0))
This produced an error, whereas an expression with using a different field in the comparision works fine.
Is it true that you can not use an expression using a field that is already included in an aggregate (sum) in the dataset?
For the ssrs expression to work you forgot to define .value after your field
The correct expression must be like the one below
=SUM(IIF(Fields!AuctionPrice.value > 0, Fields!AuctionPrice.value, 0))
It is because the Fields!AuctionPrice column has special character(!) in it. You need to escape it using square bracket
select SUM(IIF([Fields!AuctionPrice] > 0, [Fields!AuctionPrice], 0))
From yourtable
Rules for Regular Identifiers
The names of variables, functions, and stored procedures must comply with the following rules for Transact-SQL identifiers.
The first character must be one of the following:
A letter as defined by 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.
The underscore (_), at sign (#), or number sign (#).
Certain symbols at the beginning of an identifier have special meaning in SQL Server. A regular identifier that starts with the at sign always denotes a local variable or parameter and cannot be used as the name of any other type of object. An identifier that starts with a number sign denotes a temporary table or procedure. An identifier that starts with double number signs (##) denotes a global temporary object. Although the number sign or double number sign characters can be used to begin the names of other types of objects, we do not recommend this practice.
Some Transact-SQL functions have names that start with double at signs (##). To avoid confusion with these functions, you should not use names that start with ##.
Subsequent characters can include the following:
Letters as defined in the Unicode Standard 3.2.
Decimal numbers from either Basic Latin or other national scripts.
The at sign, dollar sign ($), number sign,
or underscore.
The identifier must not be a Transact-SQL reserved word. SQL Server reserves both the uppercase and lowercase versions of reserved words. When identifiers are used in Transact-SQL statements, the identifiers that do not comply with these rules must be delimited by double quotation marks or brackets. The words that are reserved depend on the database compatibility level. This level can be set by using the ALTER DATABASE statement.
Embedded spaces or special characters are not allowed.
Supplementary characters are not allowed.
When identifiers are used in Transact-SQL statements, the identifiers that do not comply with these rules must be delimited by double quotation marks or brackets.

How to use Lex Regex to check for a double underscore

I'm trying to find the correct lex regex for finding a string literal that can consist of digits,characters, and underscore as long there no two or more underscore in a row. ie
This__Doesn'tw0rk
This_W1LL_work
So far, I tried using
{Letters}(({Letters}|{Digit})*)(_?)({Letters}|{Digit}+)
but that shouldn't work due the fact it will only have 1 underscore or no underscore. Where it is possible to have more than one underscore as long it not in a row.
{Letters}(({Letters}|{Digit}|_?)*)({Letters}|{Digit})+
This doesn't work due to the fact it can allow more than 1 underscore in a row. I'm going insane reading this(http://dinosaur.compilertools.net/lex/) over and over and trying to resolve it.
I have tried using the {m,n} as noted on the website, but that didn't quite work out well either.
Any pointers would be nice, I'm trying to figure this one last issue.
Simply try
{Letters}(_?({Letters}|{Digit}))*
This is for tokens that begin with a letter, then contain zero or more instances of an optional underscore followed by a letter or a digit. It should match
a
abc
a_b_c
aa_bbbb834758_9zz
There is no way to accept two consecutive underscores since every underscore must be followed by a letter or a digit.
Bonus: you cannot end with an underscore. Add _? to the very end if you would like to allow such a thing.

When are square brackets not to be used?

I understand that square brackets allow the use of reserved names or previously disallowed characters such as spaces in your identifiers. I thought adding them everywhere was good practice. (See What is the use of the square brackets [] in sql statements?)
However, I notice that when I use them in the COL_LENGTH function, I get some unexpected results:
SELECT COL_LENGTH(N'[TestTable]', N'[RatingID]') -- Returns NULL
SELECT COL_LENGTH(N'TestTable', N'[RatingID]') -- Returns NULL
SELECT COL_LENGTH(N'[TestTable]', N'RatingID') -- Returns 10
SELECT COL_LENGTH(N'TestTable', N'RatingID') -- Returns 10
I can see that by defining the column name in single quotes, the square brackets become redundant, but I don't understand why they break the call. That square brackets work for the table argument increases my confusion.
Is there a rule for when square brackets shouldn't be used?
Do not pass square brackets into functions that take strings only use in actual SQL statements. The function is looking for a table with the name including the square brackets.

Resources