inserting regexp_like into d2rq - semantic-web

I want to filter out only integers and FPV.
d2rq:sqlExpression """regexp_like(tbl1.semanticValue1,'^[-+]?[0-9]*\.?[0-9]+')""" ;
It keeps giving me Error parsing file.
illegal escape sequence value: .
I compared results with and without brakets:
[-+]?[0-9]*\.?[0-9]+
returns: 0, -1, -1.0 , 1...
[-+]?[0-9][*\.]?[0-9]+
returns all of the above except 0
I need that zero value...

Related

SQL Error in Non Case condition of CASE WHEN clause

I tried executing the following SQL statement.
SELECT CASE WHEN CHARINDEX('~','test.pdf') > 0
THEN SUBSTRING('test.pdf',CHARINDEX('~', 'test.pdf'), -10)
ELSE NULL
END
This resulted in an error 'Invalid length parameter passed to the substring function.'. However, this was not expected because it is not going to execute anyway.
This query is a simplified version of my requirement. Actually we are computing the value length for the substring. The real scenario is also given below :
SELECT CASE
WHEN CHARINDEX('~', 'test.pdf') > 0 THEN SUBSTRING('test.pdf', CHARINDEX('~', 'test.pdf') + 1, CHARINDEX('~', 'test.pdf', (CHARINDEX('~', 'test.pdf', 1)) + 1) - CHARINDEX('~', 'test.pdf') - 1)
ELSE NULL
END;
In the example its hardcoded as 'test.pdf' but in real scenario it would be values like '111111~22222~33333~4444.pdf' from Table column. Also, I'm not sure this file name should always follow this format. Hence, a validation is required.
Actually, the computation for length is quite expensive, and don't want to use it twice in this query.
You have passed -10 as a constant to substring(). This function does not allow negative values for the third argument:
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.
SQL Server catches this problem during the compile phase. This has nothing to do with CASE expression evaluation, but with parsing the expressions.

Maths Rules in Kotlin

I'm trying to depreciate the value of an item in a straight line over time
If I use itemValue2 = itemvalue1 - itemvalue1*item-Age/item-Life, It works perfectly BUT when I originally
used itemValue2 = itemvalue1 * ( 1 - item-Age/item-Life) it constantly evaluated to itemValue1 .
Why would this be ? is it the type of the number 1 ?
itemValue1 and itemValue2 are Doubles, item-Age and item-Life are Longs
Or is it some maths rule that I haven't understood?
Like mentioned in the comments, Longs that divide will result in a truncated Long value. If your resulting value would've been been less than 1, then the result is a truncated value of 0. That leaves itemValue1 to be multiplied by 1, which is itself. You will have to cast to number types that use decimals without truncating like Double or Float.
This thing called Mathematical precedence in kotlin, mean when you create operation like this 1+2*3 the expected result is 9 but not, 7 is the result, in kotlin be the priority for the multiply's * and the divide's / then came the plus + and minus -.
To resolve this issue you need to add the brackets () it's had priority before any operation symbol in the language.
And the operation will be (1+2)*3it will gave result 9.

STUFF function sql returns null?

I have a specific column in a table, it shall contains only numbers in Nvarchar that have a length of 3. Unfortunately, some users wrote '12' but they should have written '012'. There were not enough validation at the time.
I need to fix that. Here is the logic I used :
UPDATE [Mandats_Approvisionnement].[dbo].[ARTICLE_ECOLE]
SET [UNIT_ADM] = STUFF(UNIT_ADM, 0, 0, '0')
WHERE LEN(UNIT_ADM) = 2;
The error goes like :
Cannot insert the value NULL into column 'UNIT_ADM', table
'Mandats_Approvisionnement.dbo.ARTICLE_ECOLE'; column does not allow
nulls. UPDATE fails.
I can't see where the problem is, I verified and all the records contain at least 2 characters, so the STUFF function cannot returns null as there are no NULL records in that table column [unit_adm]... How do I make it work ?
It should be stuff(UNIT_ADM,1,0,'0') as stuff returns null if the start position is 0.
Citing the documentation:
If the start position or the length is negative, or if the starting
position is larger than length of the first string, a null string is
returned. If the start position is 0, a null value is returned.
You could make this simpler by using
right('0' + UNIT_ADM, 3)
instead of stuff.

find maximum difference in a postgres row

I have a table called compression that lists the initial size and compressed size of each item.
I'd like a query that shows me the best compression stored so far, something like:
select max(
cast(uncompressed_size as int) -
cast(compressed_size as int)
)
from compression
The problem is, this code won't execute because of this error:
ERROR: Invalid digit, Value '2', Pos 0, Type: Integer
Detail:
-----------------------------------------------
error: Invalid digit, Value '2', Pos 0, Type: Integer
code: 1207
context:
query: 362794
location: :0
process: query0_21 [pid=0]
-----------------------------------------------
Something must be going on with the casting, but I'm not sure what is causing this.
It's a postgres database (technically amazon redshift), and I'm really confused why an operation like this might fail.
Check for any extra spaces at the end or beginning of this character field values and try to trim it. If there is empty space in character then casting into integer will fail, so you may want to use NULLIF(compressed_size,'') and then cast that null value which should succeed.

add zero and convert as varchar to a flot using a single query

I have data where I need to add leading zeros to it. But the problem is the data type is float. So whenever I add zeros, it automatically omits them. I have tried to add leading zero to it then try to convert it to varchar(50). But the it is giving an error:
Msg 102, Level 15, State 1, Line 1 Incorrect syntax near 'wallet_sys'.
I have used following query:
select (convert (varchar(50), ('0' + wallet_sys wallet_sys))) from NewSysData1
What have I done wrong?
PS: Some of the sample data are below: 17187383, 87339833, 93838793
I want these to be: 017187383, 087339833, 093838793
You have to add the zero after it's become a string, not before:
select '0' + convert (varchar(50), (wallet_sys)) as wallet_sys from NewSysData1
Normally, most people want to convert to having, say, a fixed width of result, with the appropriate number of leading zeros to make that happen. For that, it's a bit more work:
select RIGHT('0000000000' + convert (varchar(50), (wallet_sys wallet_sys)),10) as wallet_sys
from NewSysData1
Will produce 10 digits, with as many leading zeroes as needed (The number of zeroes in the string literal should be ~equal to the number of desired digits, and this is also the 10 provided at the right hand end of the first line)