Binary(16) truncate to binary(4) in CASE statement - sql

Have a table:
CREATE TABLE [db].[Table1](
[Id] [int] NOT NULL,
[Hash] [binary](16) NOT NULL
)
With data:
Id Hash
1 0x00000000000000000000000000000000
2 0x00000000000000000000000000000000
And trying to execute SQL:
UPDATE Table1 SET Hash = CASE Id
WHEN 1 THEN 0x4cb47abddf8a9c348c7a7c20abd0b1d5
ELSE 0
END
Expect that value in column Hash is 0x4cb47abddf8a9c348c7a7c20abd0b1d5 but actual is 0x00000000000000000000000abd0b1d5.
If I try:
UPDATE Table1 SET Hash = 0x4cb47abddf8a9c348c7a7c20abd0b1d5
Everything is ok.

Binary has the lowest precedence of any datatype in SQL Server (Data Type Precendence (SQL Server)):
SQL Server uses the following precedence order for data types:
user-defined data types (highest)
sql_variant
xml
datetimeoffset
datetime2
datetime
smalldatetime
date
time
float
real
decimal
money
smallmoney
bigint
int
smallint
tinyint
bit
ntext
text
image
timestamp
uniqueidentifier
nvarchar (including nvarchar(max) )
nchar
varchar (including varchar(max) )
char
varbinary (including varbinary(max) )
binary (lowest)
With a CASE expression, all return values are implicitly converted to the datatype with the highest value in the return values. In this case you have the int value 0, and the binary(16) value 0x4cb47abddf8a9c348c7a7c20abd0b1d5. As int > binary, the value 0x4cb47abddf8a9c348c7a7c20abd0b1d5 is converted to an int, and thus you loss data.
If the return types of your data is important, and you are using multiple data types in your return values, explicitly convert all the values to the correct type. In this case:
UPDATE Table1
SET Hash = CASE Id WHEN 1 THEN 0x4cb47abddf8a9c348c7a7c20abd0b1d5
ELSE CONVERT(binary(16),0)
END;

Related

Operand data type numeric is invalid for '~' operator

~ operator is not working for BIGINT datatype,
UPDATE Table
SET attrEx= attrEx & (~576460752303423488 )
where attrEx != 0
attrEx Type : BIGINT
Error:
Operand data type numeric is invalid for '~' operator.
You need to cast it to bigint
UPDATE Table
SET attrEx= attrEx & (~CAST(576460752303423488 AS bigint) )
where attrEx != 0
This is documented here
Functions return bigint only if the parameter expression is a bigint data type. SQL Server does not automatically promote other integer data types (tinyint, smallint, and int) to bigint.
...snip...
Integer constants greater than 2,147,483,647 are converted to the decimal data type, not the bigint data type.

How can varchar containing integer work in calculations

How come string can contain integer. Even if I assume string storing numeric values as string, but even i can use in it calculation and getting the result as well. Just to try I wrote 5 in inverted commas and still calculation works fine. Not sure how?
declare #x varchar(20)
declare #y int
select #x='5'
select #y=6
select #x+#y
SQL Server -- and all other databases -- convert values among types when the need arises.
In this case, you have + which can be either string concatenation or number addition. Because one argument is an integer, it is interpreted as addition, and SQL Server attempts to convert the string to a number.
If the string cannot be converted, then you will get an error.
I would advise you to do your best to avoid such implicit conversions. Use the correct type when defining values. If you need to store other types in a string, use cast()/convert() . . . or better yet, try_cast()/try_convert():
try_convert(int, #x) + #y
A varchar can contain any character from the collations codepage you are using. For the purposes of this answer, I'm going to assume you're using something like the collation SQL_Latin1_General_CP1_CI_AS (which doesn't have any "international" characters, like Kanji, Hiragana, etc).
You first declare the variable #x as a varchar(20) and put the varchar value '5' in it. This is not an int, it's a varchar. This is an important distinction as a varchar and a numerical data type (like an int) behave very differently. For example '10' has a lower value than '2', where as the opposite is true for 10 and 2. (This is one reason why using the correct data type is always important.)
Then the second variable you have is #y, which is an int and has the value 6.
Then you have your expression SELECT #x+#y;. This has 2 parts to it. Firstly, as you have 2 datatypes, Data Type Precedence comes into play. int has a higher precedence than a varchar, and so #x is implicitly converted to an int. Then the expression is calculated, uses + as an addition operator (not a concatenation operator). Therefore the expression is effectively derived like this:
#x + #y = '5' + 6 = CONVERT(int,'5') + 6 = 5 + 6 = 11
SQL Server uses the following precedence order for data types:
user-defined data types (highest)
sql_variant
xml
datetimeoffset
datetime2
datetime
smalldatetime
date
time
float
real
decimal
money
smallmoney
bigint
int
smallint
tinyint
bit
ntext
text
image
timestamp
uniqueidentifier
nvarchar (including nvarchar(max) )
nchar
varchar (including varchar(max) )
char
varbinary (including varbinary(max) )
binary (lowest)

where not dataype sql

I'm trying to filter some data - I have a column which looks like it is mainly smallint/int. Is there anyway I can run a where statement to say where not int or where not small int??
Microsoft SQL Server manager.
If you want a where clause that can tell you if the column contain information that can't be converted to int or smallint, you can use try_cast:
SELECT *
FROM <TableName>
WHERE TRY_CAST(<ColumnName> AS Int) IS NULL
You can change the int to smallint to get values that can't be converted to smallint but might be convertible to int.
Don't forget to replace <TableName> and <ColumnName> to the names of the relevant table and column.
The Try_Cast built in function will return null if the value in <ColumnName> is null or if it can't be converted to int (and since all smallint values can also be converted to int, it also can't be converted to smallint).

Nvarchar working with logical operator working?

Just need your help here.
I have a table T
A (nvarchar) B()
--------------------------
'abcd'
'xyzxcz'
B should output length of entries in A for which I did
UPDATE T
SET B = LEN(A) -- I know LEN function returns int
But when I checked out the datatype of B using sp_help T, it showed column B as nvarchar.
What's going on ?
select A
from T
where B > 100
also returned correct output?
Why is nvarchar working with logical operators ?
Please help.
Check https://learn.microsoft.com/en-us/sql/t-sql/data-types/data-type-conversion-database-engine?view=sql-server-2017 where it is said that data types are converted explicitly or implicitly when you move, compare or store a variable. In your case, you are comparing column B with 100, forcing sql server to implicitly convert it to integer type (check the picture about conversions on the same page). As a prove, try to alter a row putting some text in column B and, after repeating your select query B>100, sql server will throw a conversione error trying to obtain an integer out of your text.
It works because of implicit conversion between types.
Data type precedence
When an operator combines expressions of different data types, the data type with the lower precedence is first converted to the data type with the higher precedence. If the conversion isn't a supported implicit conversion, an error is returned.
Types precedence:
16. int
...
25. nvarchar (including nvarchar(max) )
In you example:
select A
from T
where B > 100
--nvarchar and int (B is implicitly casted to INT)
when adding a column to a table in ssms, not adding a datatype a "default" datatype is chosen. for me on 2017 developer it's nchar(10). if you want it to be int define the column with datatype of int. in tsql it'd be
create table T (
A nvarchar --for me the nvarchar without a size gives an nvarchar(2)
,B int
);
sp_help T
--to make a specific size, largest for nvarchar is 4000 or max...max is the replacement for ntext of old, as.
create table Tmax (
A nvarchar(max)
,B int
);
--understanding nvarchar and varchar for len() and datalength()
select
datalength(N'wibble') datalength_nvarchar -- nvarchar is unicode and uses 2 bytes per char, so 12
,datalength('wibble') datalength_varchar -- varchar uses 1 byte per so 6
,len(N'wibble') len_nvarchar -- count of chars, so 6
,len('wibble') len_varchar -- count of char so still 6
nvarchar(max) and varchar(max)
hope this helps, the question is a bit discombobulated

datatype for an iif statement

If in a SELECT statement there is the following line:
iif(fr.BoundID=0,'OutBound','InBound') as 'FlightBound'
Then when I perform a CREATE TABLE statement, should I include the actual datatype of the BoundID field which is a tinyint in the database table, or shall the datatype be varchar because I think (but not 100% sure looking at the existing code) that the previous person writing this code is saying display 'OutBound, InBound' if the ID is 0?
In your case it will be VARCHAR(8). You can always check metadata using sys.dm_exec_describe_first_result_set:
SELECT *
FROM sys.dm_exec_describe_first_result_set(
'SELECT iif(BoundID=0,''OutBound'',''InBound'') as ''FlightBound''
FROM #tab',NULL,0)
LiveDemo
What datatype to choose for new table TINYINT vs Textual representation is dependent on your business requirements. I would probably stay with TINYINT (search for lookup table named like BoundType or ask senior developer/architect).
The return type is the type in true_value and false_value with the highest precendence (reference, see return types)
Returns the data type with the highest precedence from the types in true_value and false_value. For more information, see Data Type Precedence (Transact-SQL).
Data type precendence here up to SQL Server 2016:
user-defined data types (highest)
sql_variant
xml
datetimeoffset
datetime2
datetime
smalldatetime
date
time
float
real
decimal
money
smallmoney
bigint
int
smallint
tinyint
bit
ntext
text
image
timestamp
uniqueidentifier
nvarchar (including nvarchar(max) )
nchar
varchar (including varchar(max) )
char
varbinary (including varbinary(max) )
binary (lowest)