database padding space if the value inserted has smaller length than column size- DB2 - sql

I was checking if DB pads spaces in a column if the inserted string has fewer characters than the designated length of the column. Example:
lets say size of <column1> is 10 but the value entered is abc - then is it abc_______ which the DB stores where _ represents spaces?
I am asking because I used LTRIM-RTRIM while INSERTing the values and on again fetching the value in the very next minute I got the result as abc_______.

You are using the CHAR or CHARACTER datatype for the column. The CHAR or CHARACTER datatype is a fixed length datatype and is padded with space at the end of the value to fill the column size.
You can use VARCHAR to avoid the padding with spaces at the end of the values.
Note: Make sure you are using CHARACTER_LENGTH on CHARACTER columns to get the correct character length (without padding spaces). The result of LENGTH also includes the padding spaces.
demo on dbfiddle.uk

Related

Coverting Float to String with comma without Rounding

I'm using this for adding commas into number.
val commaNumber = NumberFormat.getNumberInstance(Locale.US).format(floatValue)
floatValue is 8.1E-7 , but commaNumber shows just 0.
How to covert Float to String with comma without Rounding?
For the US locale, the maximumFractionalDigits of the number format is 3, therefore, it will try to format 8.1e-7 with only 3 fractional digits, which makes it 0.000. Since the minimumFractionalDigits is also 0, it tries to remove all the unnecessary 0s, making the final result "0".
You should set maximumFractionalDigits to at least 7 if you want to precisely display the number 8.1e-7.
val numberFormat = NumberFormat.getNumberInstance(Locale.US)
numberFormat.maximumFractionDigits = 7
val commaNumber = numberFormat.format(floatValue)
As for the commas, that is already part of the US locale. It uses grouping, and , is its grouping separator. The commas will be inserted if they are needed.

need help for sql server

When I save data byte[] in SQL Server the value change and add 0x0 in the first of value
the correct value (0xFFD8FFE000104A46)
the incorrect value (0x0FFD8FFE000104A46494600010102004C)
0xF and 0x0F are the same number, both are hexadecimal notations of number 15 decimal. A byte contains two hexadecimal 'digits'. If the left most digit is 0, it doesn't affect the value, just like zero-hundred and fifteen is the same as fifteen. The notation with the leading 0 just prints all the bytes, the one without strips the leading zeros.
Where the 494600010102004C part is coming from I don't know.

WHAT is the meaning of Leading Length?

I was checking out the difference between char vs varchar2 from google. I came across a word LEADING LENGTH in this link . THERE it was written that
Suppose you store the string ‘ORATABLE’ in a CHAR(20) field and a VARCHAR2(20) field. The CHAR field will use 22 bytes (2 bytes for leading length). The VARCHAR2 field will use 10 bytes only (8 for the string, 2 bytes for leading length).
Q1:How does the char field will use 22 bytes if the string is of 8 characters if (1 byte = 1 char)?
Q2 What is the LEADING LENGTH ? why it does occupy 2 bytes?
The CHAR() datatype pads the string with characters. So, for 'ORATABLE', it looks like:
'ORATABLE '
12345678901234567890
The "leading length" are two bytes at the beginning that specify the length of the string. Two bytes are needed because one byte is not enough. Two bytes allow lengths up to 65,535 units; one byte would only allow lengths up to 255.
The important point both CHAR() and VARCHAR2() use the same internal format, so there is little reason to sue CHAR(). Personally, I would only use it for fixed-length codes, such as ISO country codes or US social security numbers.

SQL Server 2016 format #,### not displaying 0 value

I am calling
FORMAT(myNum, '#,###') AS myNum
Which works for 123456789 as the output is 123,456,789
Also works for negative numbers
However, 0 is showing up as a blank field.
How do I get 0 to show up as 0? I am also curious as to why 0 is being removed as the query without the format shows 0 in that column's field when there should be a 0.
Note: I do not need any decimals and would prefer to use the above code if at all possible.
If you want to display the 0, if it is zero, you should use:
FORMAT(myNum, '#,###0') AS myNum
According to this Reference:
https://msdn.microsoft.com/en-us/library/ee634206.aspx
0 (zero character)
Digit placeholder. Displays a digit or a zero. If the expression has a digit in the position where the zero appears in the format string, displays the digit; otherwise, displays a zero in that position.
If the number has fewer digits than there are zeros (on either side of the decimal) in the format expression, displays leading or trailing zeros. If the number has more digits to the right of the decimal separator than there are zeros to the right of the decimal separator in the format expression, rounds the number to as many decimal places as there are zeros. If the number has more digits to the left of the decimal separator than there are zeros to the left of the decimal separator in the format expression, displays the extra digits without modification.
# Digit placeholder:
Displays a digit or nothing. If the expression has a digit in the position where the # character appears in the format string, displays the digit; otherwise, displays nothing in that position.
This symbol works like the 0 digit placeholder, except that leading and trailing zeros aren't displayed if the number has fewer digits than there are # characters on either side of the decimal separator in the format expression.

Assigning an empty binary value to a varbinary(MAX) column creates a column 8000 bytes long

I have a table with a varbinary(max) column, i am trying to assign to that column a zero-lengh binary buffer, but instead of getting a zero-length value in the table, i am getting an 8000 bytes long value filled with zeros:
* the dataSize column in the shown query was added using DATALENGHT(data) ("SELECT _index, dataSize=DATALENGHT(data), data FROM....") and shows the actual size on the table of the value
Where does the 8000 bytes long empty buffer come from? is this some kind of default behavior?
If your source column is binary(8000), then DATALENGTH(data) will return 8000 (it is fully padded) and data will contain the full 8000 bytes.
But since you are using
SELECT _index, dataSize=DATALENGTH(data), data FROM
It cannot be a binary(8000) column - because a fixed size column will report the same datalength for all rows. It is likely some data was copied there from a BINARY(8000) variable or other means some time in the past.