SQL Server : How to use variables? [duplicate] - sql

This question already has an answer here:
Varchar variable is not working in WHERE clause
(1 answer)
Closed 8 years ago.
I am trying the following query and it works as I expect it to
SELECT RIGHT('0000' + CAST(MAX(party_id)+1 AS VARCHAR(4)),4) FROM PARTY
The result is:
0147
But when I execute the following query so that I can store this value in a variable
DECLARE #pid varchar;
SELECT #pid = RIGHT('0000' + CAST(MAX(party_id)+1 AS VARCHAR(4)),4) FROM PARTY
SELECT #pid as party_id
it doesn't return 0147 as in the above query, instead what it returns is
0
Can anyone please tell me what I am doing wrong here?

You should always define a length when you declare a varchar !!
This
DECLARE #pid varchar;
gives you a varchar of exactly ONE character length!!
Use
DECLARE #pid varchar(20);
and your problem is solved ...

Yo have not declared size of varchar that is why it is truncating.
Set size sufficiently large to store result.
DECLARE #pid varchar(10);
SELECT #pid = RIGHT('0000' + CAST(MAX(party_id)+1 AS VARCHAR(4)),4) FROM PARTY
SELECT #pid as party_id
From SQL Server MSDN
varchar [ ( n | max ) ]
Variable-length, non-Unicode string data. n
defines the string length and can be a value from 1 through 8,000. max
indicates that the maximum storage size is 2^31-1 bytes (2 GB). The
storage size is the actual length of the data entered + 2 bytes. The
ISO synonyms for varchar are char varying or character varying.
When n is not specified in a data definition or variable declaration
statement, the default length is 1.

DECLARE #pid varchar; implies a varchar of length 1.
Try DECLARE #pid varchar(4);

Related

Conversion of number not match with length of datatype in SQL Server

I refer the following number with datatype bigInt.
DECLARE #Number Bigint = '269687584'
When I convert the datatype to Varbinary(4), it returns 0x10131B20.
But when I calculate the length of the Varbinary data, it returns only 3.
I didn't know exact what wrong.
In SQL Server database, I want to write an query to convert Bigint to
Varbinary(4), but is converted as only as Varbinary(3).
Here is what I have tried:
DECLARE #Number BIGINT = '269687584'
SELECT
#Number,
CONVERT(VARBINARY(4), #Number),
LEN(CONVERT(VARBINARY(4), #Number))
Use DATALENGTH instead:
DECLARE #Number BIGINT ='269687584'
SELECT #Number, CONVERT(VARBINARY(4), #Number), DATALENGTH(CONVERT(VARBINARY(4), #Number))
There is nothing wrong. You should check lenght using datalength:
DECLARE #Number Bigint ='269687584'
SELECT #Number, CAST(#Number AS Varbinary(4)), DATALENGTH(CAST(#Number AS Varbinary(4)))

Difference in the checksum SQL statements

What is the difference between the 2 different statements below? Please explain the output.
SELECT CHECKSUM(CONVERT(NVARCHAR,30))
Result : 51136012
DECLARE #AA NVARCHAR
SET #AA= CONVERT(NVARCHAR,30)
SELECT CHECKSUM(#AA)
Result: 38
The default lengths are different. In the first, it is something like 32. But when you do:
declare #aa nvarchar;
The default length is 1. So, the second is only using the first character.
In SQL Server, always use lengths with the varchar() types:
SELECT CHECKSUM(CONVERT(NVARCHAR(255), 30))
DECLARE #AA NVARCHAR(255);
SET #AA= CONVERT(NVARCHAR(255), 30);
SELECT CHECKSUM(#AA);

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)

Transact-SQL string self-concatenate [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I am new in SQL and want to create some UDF function for MS SQL Server (MS SQL Server 2012), but the very simple expression is not working. I simply want to concatenate 2 strings and save the result to the 1st one:
DECLARE #ret CHAR (20)
SET #ret = '4'
SET #ret = #ret + '55'
After executing, #ret remains at 4! Why? If I introduce another variable for '4', it works. How can I overcome the problem?
That's because you use the char data type.
When you store the value '4' in a char(20), it becomes '4___________________' (20 characters long, the _ represents spaces here).
When you concatenate '4___________________' and '55' you get '4___________________55' (22 characters long).
When you store that back in the char(20) variable, it will be truncated to 20 characters, and you get '4___________________'.
When you have a CHAR(20) the first SET will actually store '4' with 19 blank characters at the end. This is because the CHAR datatype is fixed width, and will pad your data with blank characters until it gets to the size, 20 here.
When you concatenate the '55' onto the end, it will be the 21st and 22nd characters in the string, and fall off when you try to store it inside #ret, which can only hold the first 20 characters.
Using an VARCHAR will solve your problem, allowing #ret to be the exact size you require.
DECLARE #ret VARCHAR(20);
SET #ret = '4';
SET #ret = #ret + '55';
Change datatype of the variable to match what you are concatenating: instead of char use varchar
As CHAR is a fixed length string data type, so any remaining space in the field is filled with blanks.
In your scenario, SET #ret = #ret + '55' will try to store 22 character but unfortunately, the variable is already declared as 20 character long so it truncated the result back to 20 character..Hence, you'll have 20 character with truncating last 2 character i.e. '55'.
Use char data type only when you are sure about the length of data which particular char data type column have to hold....
If you use nvarchar(20) instead of char(20), it will work fine...
DECLARE #ret nvarchar (20)
SET #ret = '4'
SET #ret = #ret + '55'
See the SQLFIDDLE
try using 'cast' in your function to ensure the #ret is a string, changing your code to this works; hope this helps.
DECLARE #ret CHAR (20)
SET #ret = '4'
SET #ret = cast(#ret as varchar(1)) + '55'
Or just change CHAR(20) to NVARCHAR(20) like this:
DECLARE #ret NVARCHAR (20)
SET #ret = '4'
SET #ret = #ret + '55'

Copying a varchar(max) field in a DTS Package

i am using DTS to export data to text file. but some fields are of type varchar(max) and the max size i can specify is varchar(8000). Hence text is getting truncated how can i avoid this.
I am not sure, but I have some logical ways :
1) either you have to add new column, say col1 & col2. Then apply "substring" query on your data, part data into two(or more) parts, as per your requirement. Then store first part in col1, second in col2...ans so on.
2) you have to add new row, with above logic, with row1 storing first part, row2 second part and so on.
There is no other way.
Though both ways would work, but I forcibly suggest you should go with 1st way.
**
I have third solution for you, it will work>
**
You can declare **nVARCHAR(MAX)** instead of nVARCHAR(MAX)
here is an example for you:::
DECLARE #String nVARCHAR(MAX)
DECLARE #i INT
SELECT #i = 10000,#String=''
WHILE #i>0
BEGIN
SELECT #String = #String + 'A'
SET #i = #i - 1
END
SELECT LEN(#String) as Length
Output:
Length
——————–
10000
(1 row(s) affected)
LEN statement returns 10000 and it proves that varchar(max) can store more than 8000 characters.
I would try and convert it to a Text column. A text column can hold up to 2147483647 bytes of binary data.
Thanks, Andrew