Invalid type for substring function - sql-server

I have a message error
Invalid int argument data type for substring function argument 1.
I don't understand why!
UPDATE DBO.NUMBER
SET NUMBER = 22675442455
WHERE SUBSTRING(ID,1,2) NOT IN ('67','68')

SUBSTRING required first argument as STRING, so simply convert it to VARCHAR.
UPDATE DBO.NUMBER
SET NUMBER = 22675442455
WHERE SUBSTRING(CONVERT(VARCHAR(15),ID)),1,2) NOT IN ('67','68')

Related

Do we need to remove some charaters from SUBSTRING length if start is not from 0?

I came across this sentence and thing what the difference between
DECLARE #MyStr nvarchar(max) = 'This is a sentence'
SELECT SUBSTRING(#MyStr, 6, LEN(#MyStr)**-5**)
and
DECLARE #MyStr nvarchar(max) = 'This is a sentence'
SELECT SUBSTRING(#MyStr, 6, LEN(#MyStr))
I ran both and the latter doesn't add any spaces or anything.
The third parameter to SUBSTRING is the length of substring to take. In the first example, the length covers exactly the remainder of the string. In the second example, the length exceeds the actual available length of the string, by 5 characters. Regarding what happens when the length is greater than the available length, we can turn to the documentation for SUBSTRING:
length
Is a positive integer or bigint expression that specifies how many characters of the expression will be returned. If length is negative, an error is generated and the statement is terminated. If the sum of start and length is greater than the number of characters in expression, the whole value expression beginning at start is returned.
In other words, if the length parameter plus the start passed is greater than what is available, then SUBSTRING just returns as much as is available.

Specify string length for substring length parameter in T-SQL

According to this msdn article and using the Substring:
SUBSTRING (value_expression ,start_expression ,length_expression )
length_expression
Is a positive integer or bigint expression that specifies how many
characters of the value_expression will be returned. If
length_expression is negative, an error is generated and the statement
is terminated. If the sum of start_expression and length_expression is
greater than the number of characters in value_expression, the whole
value expression beginning at start_expression is returned.
So if I had:
DECLARE #data varchar(max)
SELECT TOP 1 #data = Data
FROM [SomeDatabase].[dbo].[MyTable]
SELECT SUBSTRING(#data, 10, LEN(#data))
My understanding that because SUBSTRING has found that the length asked for is longer than the length of string supplied it will return you everything from the start_index till then end of the string will be taken.
for example if:
#data = "hey there"; // char length of 9
SUBSTRING(#data, 4, 20)
This should return there
Is this a particularly bad thing to do?
Are there any caveats to doing this?
Should I be explicit about the length of string to return?

second case Error converting data type varchar to numeric

The values are stored in varchar.I try to check if the values are numeric then convert to decimal, if not then return the original value.
The first case itself work fine if I comment out the second part.
CASE WHEN ISNUMERIC(Num) = 1 THEN CONVERT(DECIMAL(7,2),CONVERT(FLOAT, Num))
WHEN ISNUMERIC(Num) = 0 THEN Num END,
The problem is that you are returning different types. You need convert the return values to the same type for safe query. Either convert the decimal back to varchar, or return 0 when num is not a number.

sum and length of postgresql integer array?

I am trying to sum the contents of a postgresql array and also determine its length. In all cases, I am working with columns that are of integer array type.
ERROR: function sum(integer[]) does not exist
LINE 1: select sum(interested) from deals;
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
db5=> select SUM(interested) from deals;
ERROR: function sum(integer[]) does not exist
LINE 1: select SUM(interested) from deals;
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
db5=> select array_length(interested) from deals;
ERROR: function array_length(integer[]) does not exist
LINE 1: select array_length(interested) from deals;
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
db5=>
Contrary to what I have read (Select sum of an array column in PostgreSQL, http://www.postgresql.org/docs/8.4/static/functions-array.html) sum(column) and array_length(column, 1) do not seem to perform as expected for me. Am I looking at the wrong docs? How can I get the sum and length of a postgre integer array?
Also, these are the php queries I am using along with pg_query to run these calls:
$query = "UPDATE deals set remaining = max - array_length(interested, 1), until = min -array_length(interested, 1";
$query = "UPDATE deals set remaining = max - (SELECT SUM FROM UNNEST(hours)), until = min - (SELECT SUM FROM UNNEST(hours))";
I modified them to take Patrick's suggestions from the comments into account, but I still get a syntax error, e.g.
Warning: pg_query(): Query failed: ERROR: syntax error at end of input LINE 1: ...ngth(interested, 1), until = min -array_length(interested, 1 ^ in /var/www/html/join.php on line 162
Thank you.
In the update statement you just forgot a bracket at end of the statement (see comment).
In the second update statement, you cannot use sum in UPDATE, because aggregate functions are not allowed in update.
Use a custom sql function which sums all elements in a array and use it in the update:
CREATE FUNCTION array_sum(NUMERIC[]) returns numeric AS
$$
SELECT sum(unnest) FROM (select unnest($1)) as foo;
$$
LANGUAGE sql;
The update statement:
UPDATE deals SET remaining = max - array_sum(hours), until = min - array_sum(hours);

varChar and Char behave different on cast

Why the output is different of this query
SELECT DATALENGTH(CAST('test' AS VARCHAR)), DATALENGTH(CAST('test' AS CHAR))
outPut:
4,30
VarChar always adjusts to the length of the string passed. hence the output is 4.
CHAR has default length of 30 and hence the output is 30.
Because "When n is not specified when using the CAST and CONVERT functions, the default length is 30". But when DataLength is applied to a varchar field, it ignores trailing spaces, while for a char field the length is just the size of the field itself.

Resources