truncate integer - sql

I got a task to chop an integer by 1 digit.
For example, If the new limit is 3 digits then 1234 will become 999 after the change. if the before change value is 12345 then it should becomes 999 after changes. if the pre-change value is 564 then it will remain unchanged.
This has to be done on Oracle as well as SQL server. the truc function only truncates decimal but not integer.
What is the best way to do this in SQL, PL/SQL or T-SQL?
Thanks in advance.

This works for T-SQL. Converting it to other sql dialects should just be as simple as finding the similar methods
declare #numDigits INT = 3;
declare #maxNumber INT = POWER(10,#numDigits)-1 -- gets "999" when using 3 digits, 9999 when using 4 etc
DECLARE #input INT = 1234
DECLARE #output INT = IIF(#input>#maxNumber,#maxNumber,#input)
SELECT #output -- selects 999
Oracle does have the POWER function, but does not have the ternary/IIF function

You could use case statements for this like:
SELECT CASE [yourInt] >= 1000 THEN 999 ELSE [yourInt] END AS 'UpperLimit'
From [YouTable]

Related

how to sum up value within one cell SQL

I have some binary values such as 00, 0000, 001000.11111000, 1111100000
I need to sum it up so it turns into 0, 0, 1, 5, 5 ( sum 0s and 1s up)
how can we do that in SQL please?
Thanks
Assumption:
The binary values are stored as string.
Each value is in its own cell in a table. Something like:
BinaryValues (Consider it a column name)
00
0000
001000
and so on.
You want to add up the individual digits to get the sum.
SQL Product you are usind supports functions, looping, string manipulation like substring, extracting string length etc.
As per my best knowledge these are primitives available in all SQL products.
Solution:
Write a function (call it by any name. Ex: AddBinaryDigits) which will take the binary value in string format as input.
Inside the function and do a string manipulation. Extract each digit and add it up. Return the sum as result.
Call the function:
If using binary values stored in a table:
SELECT AddBinaryDigits(BinaryValues) FROM <WhatEverTableName>
If using fixed value:
SELECT AddBinaryDigits('00')
SELECT AddBinaryDigits('0000')
SELECT AddBinaryDigits('001000')
and so on.
Edited to include the request to create function.
CREATE FUNCTION <funtionName>
(
#ParameterName AS VARCHAR(expected string length like 10/15/20 etc.)
)
RETURNS INT
BEGIN
SQL Code to sum
RETURN SummedUpValue
END
Use the below query. If needed convert it into function.
create function dbo.fnSumChars(#someInt VARCHAR(20))
RETURNS INT
AS
BEGIN
DECLARE #count INT = LEN(#someInt),
#counter INT = 1
DECLARE #Sum INT = 0
WHILE #counter <= #count
BEGIN
SELECT #sum += CAST(SUBSTRING(CAST(#someInt AS VARCHAR), #counter, 1) AS int)
SELECT #counter += 1
END
RETURN #sum --5
END
This is the function and you can call this function like below
SELECT dbo.fnSumChars('1111100000')
If these are already in string format, this is the easiest:
select len(replace('1111100000', '0', ''))
No need for a function either, because it can be inlined in the query. Functions, even the light ones, incure perf penalty.

CEILING returns FLOOR result - SQL SERVER 2008 R2

DECLARE #a int;
DECLARE #b int;
SET #a = 9;
SET #b = 2;
SELECT CEILING (#a/#b);
It is returning as 4 instead of 5. Why?
Edit: I would like to get next smallest integer if the quotient is not whole number.
Try:
SELECT CEILING (#a/CAST(#b AS float))
And consider NULLIF(#b,0) too, to avoid a Division By Zero error.
After dividing 9 by 2 a decimal fraction is Truncated to its integer part - 4, not Rounded to 5. Try:
SELECT 9/2
Resilt is 4. Then CEILING(4) = 4
To get next integer declare variables as data types that can handle decimal part: NUMERIC,FLOAT, REAL.
SQL Server does integer division. So 9/2 = 4 in SQL Server.
Taking the ceiling of an integer is the same integer.

Select Only Numbers From Varchar column [duplicate]

This question already has answers here:
Fastest way to remove non-numeric characters from a VARCHAR in SQL Server
(16 answers)
Closed 9 years ago.
In SQL Server 2008 R2 I have some data in a varchar(12) column, it looks something like this:
Data:
%%1234
%1765
34566
123
%SDRMH
HJG434
I'd like to drop the '%' from all the rows that have it and also only select those which are numbers. I've already tried to use the ISNUMERIC() function, but it looks like that leaves the % in the rows.
Any help is greatly appreciated.
You can use a combination of REPLACE and ISNUMERIC to achieve the result set you want:
SELECT REPLACE(columnName,'%','') FROM tableName
WHERE ISNUMERIC(REPLACE(columnName,'%','')) = 1
You could use the REPLACE function to strip out all the % instances
SELECT REPLACE(column_name,'%','');
GO
This function would be expensive to use, but it could help you create / populate a new column properly typed as an int (for example):
create function [dbo].[is_int]
( #value as varchar(max) )
returns int
as
begin
declare #return int
set #return = 5
while len(#value) > 0
begin
if left(#value,1) in('1','2','3','4','5','6','7','8','9','0')
begin
set #value = right(#value, len(#value)-1)
set #return = 1
end
else
begin
set #return = 0
set #value = ''
end
end
return #return
end
or you could modify it to return the integers themselves, instead of a true/false (1/0).
You could do something like
SELECT REPLACE(column,'%','') WHERE ISNUMERIC(REPLACE(column,'%','')) = 1

SQL: how to get the left 3 numbers from an int

I want to retrieve the left 3 numbers from an integer to be stored in a table. For example, if the int is 1234567, I want to retrieve 123. I want the second number (123) to also be an int; I don't want to convert anything to a string.
(And yes, really I should be working with strings. But I don't have control over that aspect of the issue.)
Thank you!
For SQL Server, the easiest way would definitely be:
SELECT CAST(LEFT(CAST(YourInt AS VARCHAR(100)), 3) AS INT)
Convert to string, take the left most three characters, and convert those back to an INT.
Doing it purely on the numerical value gets messy since you need to know how many digits you need to get rid of and so forth...
If you want to use purely only INT's, you'd have to construct something like this (at least you could do this in SQL Server - I'm not familiar enough with Access to know if that'll work in the Access SQL "dialect"):
DECLARE #MyInt INT = 1234567
SELECT
CASE
WHEN #MyInt < 1000 THEN #MyInt
WHEN #MyInt > 10000000 THEN #MyInt / 100000
WHEN #MyInt > 1000000 THEN #MyInt / 10000
WHEN #MyInt > 100000 THEN #MyInt / 1000
WHEN #MyInt > 10000 THEN #MyInt / 100
WHEN #MyInt > 1000 THEN #MyInt / 10
END AS 'NewInt'
But that's always an approximation - what if you have a really really really large number..... it might just fall through the cracks....
Without casting to string, how about this?
(T-SQL)
select #i / power(10,floor(log10(#i))-2)
Throws an error if the int is less than 100, but seems to work otherwise.
EDIT: To handle the error gracefully, you'd have to use a CASE since TSQL has no GREATEST() function...
select #i / case when #i < 100 then 1 else power(10,floor(log10(#i))-2) end
In access SELECT clng(left(cstr(field), 3)) FROM T should work.
Edit: Infact I bet it wont care about the cstr().
;WITH c10 AS
(
SELECT
Number
FROM
MyTable --?? WHERE Number>=1000
UNION ALL
SELECT Number/10 FROM c10 WHERE Number>=1000
)
SELECT Number FROM c10 WHERE Number < 1000
I can't test this, but it should do the trick. Iterate through until you end up with < 1000, relying on integer division. You may need to filter on the first clause to fine tune it
For a raw TSQL SQL Server 2005 solution only
well if you have access to php you could use substr
echo substr('1234567', 0, 3); and then convert the string back to an int
Converting an integer to a string in PHP
good luck!

How can I convert a character-encoded binary string to hexadecimal in SQL Server?

I'm trying to take a VARCHAR(MAX) with data in it as follows:
"00001001010001010111010101..." etc.
Then encode it as hexadecimal for more efficient return to the client.
Is it possible to do this? Either directly or converting the string into a real binary column first before calling master.dbo.fn_varbintohexstr?
As an example, given the string:
0000100101000101011101011110
We should end up with:
0000 = 0
1001 = 9
0100 = 4
0101 = 5
0111 = 7
0101 = 5
1110 = E
094575E.
Or if there is an even more efficient method (reading binary directly?) then that would be even better. SQL Server 2000 compatible solutions are preferable.
Given your previous question, you're generating this string as part of another query. Why on Earth are you generating a string of ones and zeros when you can just multiply them by the appropriate power of 2 to make an INT out of them instead of a string? Converting from INT to hex string is trivial.
You can always split the string in 4 char groups (starting from end!), using SUBSTRING, and cast the 4 char groups to the hex digit, eg. '0011' to '3' in a big CASE statement. There are only 16 switch cases in the CASE, so is more than manageable. But all you get is a 4 times reduction in length, not sure if it worth the (significant) server overhead just to reduce the traffic.
An alternate solution might be by implementing a user defined function using your favorite .NET language
Are you looking for something like this: http://support.microsoft.com/kb/104829
Here it is in case the link ever dies:
create procedure sp_hexadecimal
#binvalue varbinary(255)
as
declare #charvalue varchar(255)
declare #i int
declare #length int
declare #hexstring char(16)
select #charvalue = '0x'
select #i = 1
select #length = datalength(#binvalue)
select #hexstring = "0123456789abcdef"
while (#i <= #length)
begin
declare #tempint int
declare #firstint int
declare #secondint int
select #tempint = convert(int, substring(#binvalue,#i,1))
select #firstint = floor(#tempint/16)
select #secondint = #tempint - (#firstint*16)
select #charvalue = #charvalue +
substring(#hexstring, #firstint+1, 1) +
substring(#hexstring, #secondint+1, 1)
select #i = #i + 1
end
select 'sp_hexadecimal'=#charvalue