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.
Related
I am in the process of rolling over a bunch of old stored procedures that take NVARCHAR(MAX) strings of comma and/or semicolon separated values (never mind about one value per variable etc.). The code is currently using the CHARINDEX approach described in this question, though in principle any of the approaches would work (I'm tempted to replace it with the XML one, because neatness).
The question, though, is what is the most efficient may of handing escaped delimiters? Obviously the lowest level approach is a character by character parser, but I can't shake the feeling that (1) that's going to be horrible when executed a million times in close succession and (2) it'll be overcomplicated for the situation.
Basically, I want to handle 3 possible escapes:
"\\", "\,", and "\;" somewhere in my string. What's the best way to do it? I should add that, ideally, I don't want to make any assumptions about what characters are included in the string.
Sample data would look something like the below.
Value1,Value\,2,ValueWithSlashAtTheEnd\\,ValueWithSlashAndCommaAtTheEnd\\\,
I'm actually splitting to rows rather than columns, but the principle is the same; I'd expect the below output typically:
SomeName
^^^^^^^^
Value1
Value,2
ValueWithSlashAtTheEnd\
ValueWithSlashAndCommaAtTheEnd\,
Needless to say, the escapes could occur anywhere in a value, and ideally I'd like to handle for semicolons as well, but I'll probably be able to infer that from the comma behaviour.
Just provide your function edited string:
replace(replace(#yourstring, '\\', '^'), '\,', '#')
Then replace back:
replace(replace(#returnedstring, '#', ','), '^', '\')
Replace ^ and # with any characters that are not on the string.
I currently look for an advice on the below piece of code which consists of efficiently looping through a dataset (of cell type) and extracting each column as data vector.
[i,j]=size(fimat);
k=2;
while k<=j % looping through columns
[num2str(k-1),'yr']=cell2mat(fimat(:,k)); %extract each column as vector
k=k+1;
end
My matter undeniably lies in the following statement:
[num2str(k-1),'yr']
that correctly concatenates numbers (reflected by variable k) and string name 'yr'. However the syntax fails in assigning for instance (during 1st iteration)
1yr=cell2mat(fimat(:,2))
The resulting error speaks from itself
Error: An array for multiple LHS assignment cannot contain LEX_TS_STRING.
but I'm still figuring out a way to do it. Thus any feedback would be appreciated.
Thanks
First of all, in matlab, a variable name cannot start with a digit. You should modify your code such that the variable name starts with either a letter or an underscore.
For instance ['yr' num2str(k-1)] or ['_' num2str(k-1) 'yr'] would be better.
Then, what you are trying to do is very strongly discouraged by everyone, including The Mathworks. It would be much better to use a cell yr and call to yr{k} rather than iterative variable names:
yr = cell(j,1);
for k = 2:j
yr{k-1} = cell2mat(fimat(:,k));
end
Anyway, if you still want to do this, you can use eval
while k<=j
eval(['_' num2str(k-1) 'yr = cell2mat(fimat(:,k));']);
k=k+1;
end
Best,
You can not dynamically create variable names like you did. The left side of the = must be a identifier, not a char. The alternative I recommend is to use a cell array instead of individual variable names. For example:
yr{k-1}=cell2mat(fimat(:,k))
If you must use variable names with numbers, which I strongly recommend not to do, you have to use eval for the line. Alternatives which I strongly recommend to check before using eval are struct with dynamic field names and containers.Map
Here is my answer to the question, for sharing purposes. Hope it will help and Thanks to the contributors of this post.
[i,j]=size(fimat); %get dimension of dataset (of cell type)
numdata=cell2mat(fimat(1:i,2:j)); %extract only numeric from dataset
for k=1:j-1
eval(sprintf('yr%d = numdata(:,k)', k));
end
What is the best way to store the following value in SQL Server ?
1234-56789 or
4567-12892
The value will always have 4 digits followed by a hyphen and 5 digits
char(10) is a possibility that I was thinking of using or removing the hyphen and storing as int
If it is a business requirement to have "The value will always have 4 digits followed by a hypen and 5 digits" Then CHAR(10) but if you think Users should be able to add values even if isnt in the expected format then VARCHAR(10) or VARCHAR(15) whatever suits you better.
You should store those kind of values as int only if really represents a number as opposed to a series of digits. Number means something that you can make calculations on, compare are numbers, etc.
Otherwise store it as char. Make it length of 10 if the format is set and won't change.
Another option would be to create a CHAR(4) column and a CHAR(5) column. This would be useful (only) if you envision ever having to query against one or the other part independently.
Very easy to concatenate these back together using a view, computed column, or inline - so you don't have to waste storage space on a dash that will always be there, and so that you can keep these two pieces of data separate if, in fact, they are independent.
Since you didn't provide much detail about what these "numbers" represent or how they will be used / queried, you're going to get a whole bunch of opinions, some of which might not be very relevant to your data model.
Well, if it's guaranteed to always be like that, a char(10) datatype seems appropriate.
But you should also add a check constraint:
column LIKE '[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9][0-9]'
Here is a SO answer that should help you sort out what you need -
nchar and nvarchar can store Unicode characters.
char and varcharcannot store Unicode characters.
char and nchar are fixed-length which will reserve storage space for number of characters you specify even if you don't use up all that space.
varchar and nvarchar are variable-length which will only use up spaces for the characters you store. It will not reserve storage like char or nchar.
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.
What is the best way to append spaces at the end of a char variable in SQL Server?
I have found 3 ways. Any ideas which one is better? Here I am trying to pad 2 spaces at the end of FOO
1)
declare #var char(5)
set #var = convert(char(5),'FOO')
2)
declare #var char(5)
set #var = cast('FOO' AS char(5))
3)
declare #var char(5)
set #var = 'FOO'
what is the difference between each of them?
When I have to parse huge data which option will be quicker and efficient taking less memory?
The spaces are comming from the way the variable is declared: char(5). Being a fixed length type, the value will be automatically space appended.
You should also look at SET ANSI PADDING setting. For varchar(5) type (variable length) the setting of ANSI PADDING may result in trimming existing spaces from the end of the value:
Trailing blanks in character values
inserted into a varchar column are
trimmed. Trailing zeros in binary
values inserted into a varbinary
column are trimmed.
My guess is that they are all identical.
The T-SQL parser probably creates an internal expression tree from each statement, and after abstracting it, each one becomes the same tree.
Cast and convert are practically identical, the only difference being that a cast is required to switch between decimal and numeric types. Implicit conversion is fine too. The execution is identical.
The only thing to watch for is if your input is longer than your variable size, the end will be trimmed off without warning.