CAST always returns 1 - sql

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))

Related

How to check Palindrome in SQL Server

To check palindrome I am using REVERSE function of SQL Server.
I wanted to check how reverse function works with this sample code:
declare #string nvarchar
set #string = 'szaaa'
SELECT REVERSE(#string)
But the output was 's' in case of 'aaazs' which I expected. How should I capture the reverse? Is there any better way to find palindrome?
In SQL Server, always use lengths with the character types:
declare #string nvarchar(255);
set #string = 'szaaa';
SELECT REVERSE(#string);
The default length varies by context. In this case, the default length is "1", so the string variable only holds one character.
To check palindrome, You can use CASE Statement
DECLARE #string NVARCHAR(255);
SET #string = 'szaaa';
SELECT CASE WHEN #string=REVERSE(#string)THEN 'Is palindrome'
ELSE 'Is not palindrome'
END

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.

Extracting nvarchar value from XML in T-SQL: only one character returned

In my T-SQL procedure I'm trying to extract a string value from the XML node using the .value() method, like this:
declare #criteria xml;
set #criteria = N'<criterion id="DocName"><value>abcd</value></criterion>';
declare #val nvarchar;
set #val = #criteria.value('(criterion[#id="DocName"]/value)[1]', 'nvarchar');
select #val;
I expected to get 'abcd' as a result, but I surprisingly got just 'a'.
So, the value method returns only the 1st character of the string. Can anybody tell me, what am I doing wrong? Thanks a lot.
P.S. I'm using MS SQL Server 2012
Don't use nvarchar without size. From documentation:
When n is not specified in a data definition or variable declaration
statement, the default length is 1. When n is not specified with the
CAST function, the default length is 30.
If you don't know exact length, you always can use nvarchar(max):
declare #criteria xml;
set #criteria = N'<criterion id="DocName"><value>abcd</value></criterion>';
declare #val nvarchar(max);
set #val = #criteria.value('(criterion[#id="DocName"]/value)[1]', 'nvarchar(max)');
select #val;
sql fiddle demo

Why does SQL LEN function return '1' for a string with several characters?

Simple question - why when I print the value of the #len variable in the query below would I be getting the value 1, instead of 12 (the number of characters in the specified string)?
DECLARE #string varchar
DECLARE #index int
DECLARE #len int
DECLARE #char char(1)
SET #string = 'content loop'
SET #index = 1
SET #len= LEN(#string)
print #len
Your declaration of #string is wrong. You have no length on the varchar.
Try this:
declare #string varchar(255); -- or whatever
You just learned that the default in this situation is 1.
This is clearly specified in the documentation. As a further note, MS SQL seems to make this rather complicated:
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.
The right habit is to always include the length when using varchar or nvarchar.
You need to give the variable #string an actual length. Print the variable #string and it will probably return 'C'.
Becvause varChar without a length specification is taken as varChar(1)
replace varchar with varChar(30) or varChar(max)

resize SQL Varchar

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)