resize SQL Varchar - sql

Is there a way to resize a local varchar in sql?
like:
DECLARE #MYVARIABLE AS VARCHAR(3)
REDIM #MYVARIABLE(50)
That will change the size of #MYVARIABLE to 50 char.
Note: I need to keep the variable because I use it after.
So I cant use another one.
Sample 2:
DECLARE #MYVARIABLE AS VARCHAR(4)
SET #MYVARIABLE = 'TEST'
SET #MYVARIABLE = '12345'
SELECT #MYVARIABLE
here, MYVARIABLE = '1234' at the end. should be '12345'
how to do it (and keep myvariable)
can I delete the current and create a new one?

Set you variable to the largest size you know you will need in the first place e.g.:
DECLARE #MYVARIABLE AS NVARCHAR(50)
Or if you don't know how big you need then you could always use MAX:
DECLARE #MYVARIABLE AS NVARCHAR(MAX)
If the length of the variable is important to you then you can get that after it is assigned a value:
DECLARE #MYVARIABLE AS NVARCHAR(MAX)
DECLARE #LENGTHOFMYVARIABLE AS INT
SET #MYVARIABLE = 'somerandomtext'
SET #LENGTHOFMYVARIABLE = LEN(#MYVARIABLE)
In this case #LENGTHOFMYVARIABLE would be 14.
UPDATE
The only other way I know of how to do this is to use a secondary temp variable:
DECLARE #MYVARIABLE AS NVARCHAR(3)
DECLARE #MYBIGGERTEMPVARIABLE AS NVARCHAR(50)
SET #MYBIGGERTEMPVARIABLE = #MYVARIABLE
Obviously there are some limitations if the variable gets passed along again, but apart from that the only other way would be to change the source variable length to be a more reasonable value. Really there isn't much of a reason to have variables of such a small size.

you can use temp variable :
DECLARE #MYVARIABLE AS VARCHAR(3)
DECLARE #myVal as VARCHAR(50)=#MYVARIABLE

No, you cannot alter the definition of a variable, nor undeclare a variable. It's definition is fixed, from the point it is declared, until the end of the batch.
It's definition even takes effect if the declaration doesn't appear to run:
if 1 = 0
begin
declare #a varchar(3)
print 'declared!'
end
else
begin
print 'not declared?'
end
set #a = 'abc'
print #a
prints:
not declared?
abc
Since you can't change the way that the variable is being declared, you'll have to give up on this attempted approach to your overall problem (but I can't offer suggestions, since you've not described your overall problem)

Related

Why do I have two different return values from the queries in T-SQL

Hello I wonder why I have 2 different return Values.
Here is the first query:
declare #currentcolumn_val varchar
declare #start integer
set #currentcolumn_val = 'state_val'
set #start =1
select #currentcolumn_val from z_skm where id = #start
the returned value is just "s"
Here is the second Query which gives the correct return value:
select state_val from z_skm where id = 1
This query gives me exactly what it should.
I hope you guys can help.
Cheers steven
Your first query is returning a constant. The second is returning the value of the column.
You are returning 's' instead of 'state_val' because of the declaration:
declare #currentcolumn_val varchar;
You have no length on varchar() and in this context, it defaults to a length of 1. Always use length with varchar() in SQL Server.
If you want the column to be dynamic, you need to use dynamic SQL:
declare #currentcolumn_val nvarchar(255);
declare #start integer;
declare #sql nvarchar(max) = 'select #currentcolumn_val from z_skm where id = #start';
set #sql = replace(#sql, '#currentcolumn_val', #currentcolumn_val);
exec sp_executesql #sql, N'#start int', #start = #start;
You can pass a parameter as an argument, but not a column or table name.
You have declared #currentcolumn_val simply as varchar, which defaults to a length of 1.
if you replace it with declare #currentcolumn_val varchar(10) you will see the whole value
First you haven't given the #currentcolumn_val parameter a size so it is equivalent to a VARCHAR(1). This means that this:
set #currentcolumn_val = 'state_val'
Is the essentially the same as:
set #currentcolumn_val = 's'
Then in your SELECT you are returning the value of that variable, not a column from the table.

CAST always returns 1

I'm executing this TSQL Code:
DECLARE #myString varchar;
SET #myString = '123.0'
SELECT CAST(#myString as decimal(25,10))
But I keep getting 1.00000 as an result
Changing myString to '123' doesn't change that.
Any advise on what I'm doing wrong is appreciated.
Thanks in advance!
ALWAYS use length when using varchar() (and related types) in MySQL. The default is 1 in this context. So this fixes your problem:
DECLARE #myString varchar(255);
SET #myString = '123.0';
SELECT CAST(#myString as decimal(25,10));
You are getting 1, because your code is interpreted as
DECLARE #myString varchar(1);
SET #myString = '123.0';
SELECT CAST(#myString as decimal(25,10));
The documentation is not shy about this:
When n is not specified in a data definition or variable declaration
statement, the default length is 1. When n is not specified when using
the CAST and CONVERT functions, the default length is 30.
You are missing the varchar declareation
DECLARE #myString varchar(10);
SET #myString = '123.0'
SELECT CAST(#myString as decimal(25,10))

How to add N (unicode) to a variable (the value already in the variable)

Well, I know there are some similar questions in the forum, but still there is no explicit solution.
I want to add N to a variable to store double byte(Japanese) into db table, but I do not know how to add the N to a variable.
The value is already in the variable #value, which it gets its value(Japanese) automatically from a CSV file.
What I want to do is insert the value into a db table with correct Japanese chars. So I did
declare #finalValue nvarchar(255);
set #finalValue=N#value; --obviously, it is NOT correct.
--insert ........
what should I do for inserting correct #finalValue into table?
The N'some text' is a way of defining a double byte string literal. You don't use it with Transact-SQL variables.
What is the the data type of your #value variable? In most cases you can do this:
create table foo ( dbcs_string nvarchar(2000) not null )
...
declare #my_variable varchar(2000)
set #my_variable = 'the quick brown fox jumped over the lazy dog.'
...
insert foo ( dbcs_string ) values ( #my_variable)
and the conversion will be done implicitly.
You can, however, explicitly coerce you variable into the proper type using the convert() function:
convert(nvarchar(2000),#my_variable)
Have you tried this?
declare #finalValue nvarchar(255);
set #finalValue=cast(#value as nvarchar(255))
also implicit conversion works as well.. may get truncation warning if you have a size mismatch.
declare #finalValue nvarchar(255);
set #finalValue=#value

Return a concatenated value from a SQL stored procedure

Please advise me to get the return value as 4/30 from a stored procedure.
I have declared a stored procedure with one output parameter #result varchar(5) output and no input parameter,
I have declared 3 variables inside a stored procedure say
declare #a float
declare #b float
declare #c varchar
set #a=4
set #b=30
set #c=cast(#a as varchar)+'/'+cast(#b as varchar)
set #result=#c
return #result
The stored procedure executes and returns the return value as 4.
But I need the return value to be 4/30. Is it possible to get 4/30 as return value?
Please suggest me on the above...
Actually I want to set #a to a select query which returns a float number,
and set #b to another select query which returns a float number .. but in the above sp just showed an example. Please advise me to get the return value as 4/30.
You need to specify a size for #c. Without size it will be varchar(1).
declare #c varchar(5)
You should specify the length parameter when declaring the variable #c
declare #c varchar(5)
After writing down query in MS,the thing i clear about is that there is need to specify variable length of variable.....
declare #a int;
declare #b int;
declare #c varchar(5);
set #a=4;
set #b=30;
SET #c=cast(#a as varchar)+'/'+cast(#b as varchar)
select #c;

Tokens for SQL statement fill in blank instead of NULL making it crash

I have a sql statement like this:
DECLARE #MyVariable as varchar(50)
SET #MyVariable = $(TokenValue)
In this the $(TokenValue) will fill in a value from a form (ignore how it's doing it, it's not important, just that if there's a value in the field it relates to it'll get filled in there). If The field in the form was left blank however, there will be nothing put there, leaving the end result like this:
DECLARE #MyVariable as varchar(50)
SET #MyVariable =
rather than
DECLARE #MyVariable as varchar(50)
SET #MyVariable = 'FormInputValue'
Since there's nothing after the =, how can I account for this and make sure the SQL statement doesn't crash? The token never writes null to it's place, always just a blank if the field was left empty... Any ideas?
Thanks,Matt
you can try playing with default parameters e.g
create PROCEDURE dbo.test
#v varchar(50) = ''
AS
BEGIN
select #v
END
GO
and then
declare #a table (v varchar(50))
insert into #a
exec dbo.test -- that represents scenario with no token
insert into #a
exec dbo.test 'blah' -- that if there is a token
select * from #a
So in your case instead of
DECLARE #MyVariable as varchar(50)
SET #MyVariable = $(TokenValue)
you would end up with
DECLARE #MyVariable as varchar(50)
declare #a table (v varchar(50))
insert into #a
exec dbo.test $(TokenValue)
select #myVariable = v from #a
But that is really hacky solution.
As other suggested it should be rather filtered on the application level
I am afraid that you will not be able to bypass this.
This is something that should be handed at the application level.
If $(TokenValue) isnot a param, and you do not include the ' in the string value you can try using
SET #MyVariable = '$(TokenValue)'
This will not work with all var types (INT and FLOAT works, but not decimal)
--WORKS
DECLARE #MyVariable as INT
SET #MyVariable = ''
SELECT #MyVariable
--WORKS
DECLARE #MyVariable as FLOAT
SET #MyVariable = ''
SELECT #MyVariable
--DOES NOT WORK
DECLARE #MyVariable as DECIMAL(18,8)
SET #MyVariable = ''
SELECT #MyVariable
Edited for SQL Server :) If you write your query like this:
set #MyVariable = ''$(TokenValue)''
if #MyVariable = ''''
set #MyVariable = null
else
set #MyVariable = substring(#MyVariable,2,len(#MyVariable)-2)
If nothing is in the place of $(TokenValue), you get:
if '''' = ''''
and #MyVariable will be set to NULL. If there is a string in the place of $(TokenValue), you get:
if '''teststring''' = ''''
^^^^^^^^^^^^
Because '' is the way to escape a single quote, each '' results in one quote in #MyVariable. Those two quotes are stripped away by the substring in the else clause.