Interview Questions Answers.ORG
Interviewer And Interviewee Guide
Interviews
Quizzes
Home
Quizzes
Interviews Databases Programming Interviews:BtrieveClipperData ModelingData StructuresDatabase AdministrationDatabase Administrator (DBA)Database AnalystDatabase DeveloperDB DevelopmentEDI/Data Integration ExpertFirebirdHierarchicalIBM DB2InformixJava DatabaseMariaDBMicrosoft Access DeveloperMongoDBMS SQL ServerMySQL ProgrammingNetworkNoSQLObject RelationalPostgrePostgreSQLProgressRDBMSRelationalSQLSQL AdministratorSQL and PL/SQLSQL Notification ServicesSQL server 2008SSRSStored ProcedureSybaseTeradata
Copyright © 2018. All Rights Reserved
MS SQL Server Interview Question:
How To Convert Binary Strings into Integers in MS SQL Server?
Submitted by: AdministratorBinary strings and integers are convertible implicitly and explicitly. But there several rules you need to remember:
* Binary strings will be implicitly converted into an integer data type, if it is involved in an arithmetical operation with another integer data type operand.
* Binary strings will be implicitly converted into an integer data type, if it is assigned to a variable, a column or a parameter of an integer data type.
* Binary strings will be explicitly converted into integer data types using CAST() and CONVERT() functions.
* When converting binary strings that have more bytes than the target data type size, bytes on the left hand side will be truncated.
* When converting binary strings that have less bytes than the target data type size, 0x00 will be padded on the left hand side.
Examples showing in the tutorial exercise below will help you remembering those rules.
SELECT 0x66 + 44
GO
146
DECLARE @integer INT;
SET @integer = 0x66;
SELECT @integer + 44
GO
146
SELECT CAST(0x66 AS INT) + 44
GO
146
SELECT CONVERT(INT, 0x66) + 44
GO
146
-- Only last 4 bytes are used for INT conversion
SELECT 0x7700000066 + 44
GO
146
-- 8 bytes will be used for BIGINT conversion
SELECT 0x7700000066 + CONVERT(BIGINT,44)
Submitted by: Administrator
* Binary strings will be implicitly converted into an integer data type, if it is involved in an arithmetical operation with another integer data type operand.
* Binary strings will be implicitly converted into an integer data type, if it is assigned to a variable, a column or a parameter of an integer data type.
* Binary strings will be explicitly converted into integer data types using CAST() and CONVERT() functions.
* When converting binary strings that have more bytes than the target data type size, bytes on the left hand side will be truncated.
* When converting binary strings that have less bytes than the target data type size, 0x00 will be padded on the left hand side.
Examples showing in the tutorial exercise below will help you remembering those rules.
SELECT 0x66 + 44
GO
146
DECLARE @integer INT;
SET @integer = 0x66;
SELECT @integer + 44
GO
146
SELECT CAST(0x66 AS INT) + 44
GO
146
SELECT CONVERT(INT, 0x66) + 44
GO
146
-- Only last 4 bytes are used for INT conversion
SELECT 0x7700000066 + 44
GO
146
-- 8 bytes will be used for BIGINT conversion
SELECT 0x7700000066 + CONVERT(BIGINT,44)
Submitted by: Administrator
Copyright 2007-2024 by Interview Questions Answers .ORG All Rights Reserved.
https://InterviewQuestionsAnswers.ORG.
https://InterviewQuestionsAnswers.ORG.