Nonsense Error in SQL Server Management Studio - sql-server

I have this query that spawns the following error:
SELECT * FROM Quota
WHERE LEFT(QtLabel, LEN(QtLabel)-2) IN (
'1032',
'3300',
'9682'
)
Msg 536, Level 16, State 5, Line 1
Invalid length parameter passed to the SUBSTRING function.
Am I doing something wrong? It tends to show up when I use the LEN() function. Might it be a datatype issue?

Is it possible that LEN(QtLabel) <= 2 ?

Are you sure that each QtLabel field is longer than 2 characters?

LEFT probably uses SUBSTRING internally. What happens if the length of QtLabel is <= 2?

It's because some QtLabel values don't contain > 2 characters, so you end up trying to do a LEFT() with a negative value as the number to restrict to.
In your scenario, you're assuming all QtLabel values are 6 characters, so uou should do:
SELECT * FROM Quota
WHERE LEN(QtLabel) = 6
AND LEFT(QtLabel, LEN(QtLabel)-2) IN (
'1032',
'3300',
'9682'
)

SELECT LEFT('MyText', -2)
will throw the same error

Related

Oracle and SQL Server `CEIL/CEILING` functions return different results

In Oracle:
Input: Select CEIL(5.416579) From Dual
Output: 5.42
In SQL Server:
Input: Select CEILING(5.416579)
Output: 6
As per Oracle and SQL Server CEIL/CEILING functions return an integer value, but here both of them return different results.
The Oracle CEIL documentation states:
Syntax
CEIL(n)
Purpose
CEIL returns smallest integer greater than or equal to n.
So CEIL(5.416579) function will always return 6 in Oracle.
The only way I can see for you to get a different result is if someone has create a user-defined CEIL function in your schema and that is being invoked rather than the built-in function:
(Don't do this)
CREATE FUNCTION SCHEMA_NAME.CEIL(n NUMBER)
RETURN NUMBER
AS
i INTEGER := TRUNC( n * 100 );
BEGIN
IF i <> n * 100 THEN
RETURN (i + 1)/100;
ELSE
RETURN i/100;
END IF;
END;
/
But even then, SELECT CEIL(5.416579) FROM DUAL appears to use the global built-in function and returns 6. To override this you have to explicitly state the package:
SELECT SCHEMA_NAME.CEIL(5.416579) FROM DUAL
Outputs:
5.42
db<>fiddle
Check you don't have another function called CEIL but without that, the behaviour you are describing should be impossible.
The precision setting has most likely changed to a minimum precision of 2 on your system.

How to multiply a large number in sql with out getting a "overflow error " error

I know this maybe a silly question; but how do I multiply large numbers in SQL Server without getting this error:
Arithmetic overflow error converting expression to data type int
I need to take a column that contains a list of 6 digit client numbers.
E.g. 123456, 123457 and make it 1234560000000, 1234570000000 & etc.
This is what I tried doing.
update account set sClientIdNo = sClientIdNo * 100000000
But I end up with the overflow error.
Any help will be greatly appreciated.
Edit: i forgot to mention that the column which contained the client numbers had a varchar data type.
This is what worked for me.
UPDATE account SET sClientIdNo = CONVERT(bigint, sClientIdNo ) * 100000000
#shA.t Provided the clue i needed by declaring #a a numeric.
Thanks
I can suggest you to use numeric type like this:
DECLARE #a numeric(14, 0) = 123456
SELECT #a * 100000000
Note that I use 14 for your requirement you can use bigger values for precision.

Select then parse using substring and cast gives different result from computation

I parsed a string which I use substring to get the last 11 i think numbers of characters. I accomplished that one but when I use cast and round it gives a different result from the manual computation I did.
Here's my query
SELECT round(cast(SUBSTRING('351856040520298,241111;1R,141117003059,A,1420.4629N,12058.7028E,0.0,77,0.9,20000006;2R,141117003059,11,98.3,12.58,04.10,282098820.9', 123,11)as float)/3600, 0, 1)
This gives me a result of 583. But when I try to manually compute using the computation below
282098820.9 / 3600
The result is
78360.7835
Is there something wrong with my query?
Thanks for the help.
The problem is with your SUBSTRING. It only returns 2098820.9 instead of 282098820.9. Try using RIGHT to extract the last 11 characters.
SELECT round(cast(right('351856040520298,241111;1R,141117003059,A,1420.4629N,12058.7028E,0.0,77,0.9,20000006;2R,141117003059,11,98.3,12.58,04.10,282098820.9', 11)as float)/3600, 0, 1)

Performing query that doesn't return desire result with like operator

Whenever i am trying to do query with this i am getting words which length is 15 .
select * from 'Table_A' where LENGTH('value') = 5 and value LIKE 'A%S';
LENGTH('value') is always 5. Thus it returns only values that match LIKE 'A%S'
Fix it with:
SELECT * FROM Table_A where LENGTH(value) = 5 and value LIKE 'A%S';
select * from 'Table_A' where LENGTH(value) = 5 and value LIKE 'A%S';
You were checking the length of the literal string 'value', which is always 5. Remove the quotes so you check the length of the data in the column.

Delete last N characters from field in a T-SQL Server database

I have table of over 4 million rows and accidentally in one column there is more data than needed.
For example instead of ABC there is ABC DEFG.
How can I remove that N symbols using TSQL? Please note that I want to delete this characters from database, NOT just select substring. Thank you
UPDATE mytable SET column=LEFT(column, LEN(column)-5)
Removes the last 5 characters from the column (every row in mytable)
I got the answer to my own question, ant this is:
select reverse(stuff(reverse('a,b,c,d,'), 1, N, ''))
Where N is the number of characters to remove. This avoids to write the complex column/string twice
You could do it using SUBSTRING() function:
UPDATE table SET column = SUBSTRING(column, 0, LEN(column) + 1 - N)
Removes the last N characters from every row in the column
This should do it, removing characters from the left by one or however many needed.
lEFT(columnX,LEN(columnX) - 1) AS NewColumnName
You can use function RIGHT [https://www.w3schools.com/sql/func_sqlserver_right.asp]
RIGHT( "string" , number_of_chars_from_right_to_left)
That should look like this:
Query: SELECT RIGHT('SQL Tutorial', 3) AS ExtractString;
Result: "ial"

Resources