I want to spit an expression in qlikview, I have this values:
Column1
564564/455654
546546/POMLK21435
458798/454687/P44545
I want to pick in each line the number which is contain 6 num like this bellow:
Column1 Column2 Column3
564564/455654 564564 455654
546546/POMLK21435 546546
458798/454687/P44545 458798 454687
How can I do that please
Thank you for your help
Take a look at subfield() and isnum(). Something like
If(isnum(Subfield([Column1],'/',1)),Subfield([Column1],'/',1),Null()) as [Column2],
If(isnum(Subfield([Column1],'/',2)),Subfield([Column1],'/',1),Null()) as [Column3]
would probably work for you.
Related
I have a table with a column that has some variable data. I would like to select only the rows that have values with numerical characters [0-9]
The column would look someting like this:
time
1545123
none
1565543
1903-294
I would want the rows with the first and third values only (1545123 and 1565543). None of my approaches have worked.
I've tried:
WHERE time NOT LIKE '%[^0-9]+%'
WHERE NOT regexp_like(time, '%[^0-9]+%')
WHERE regexp_like(time, '[0-9]+')
I've also tried these expressions in a CASE statement, but that was also a no go. Am I missing something here?
This is on Amazon Athena, which uses an older version of Presto
Thanks in advance
You can use regexp matching only numbers like '^[0-9]+$' or '^\d+$':
-- sample data
WITH dataset (time) AS (
VALUES
('1545123'),
('none'),
('1565543'),
('1903-294')
)
--query
select *
from dataset
WHERE regexp_like(time, '^[0-9]+$')
Output:
time
1545123
1565543
Another option which I would say should not be used in this case but can be helpful in some others is using try with cast:
--query
select *
from (
select try(cast(time as INTEGER)) time
from dataset
)
where time is not null
I have a column to check if contains number from 0-9 and a decimal. Since in the version of SQL am using the below does not seem working
select *
from tablename
whwere columnname like '%[^.0-9]%'
Also tried using column name like '%[0-9]%' and columnname not like '%.%' but if there is a negative sign it is not getting captured. Please advise.
The column data type is float. So can someone provide me a query to check if the column contains values from 0-9 and also it can contain decimal values these two are permitted. If say for example if I have value 9,9.99 ,-1.24 the query should output -1.24 I need this value other than decimal and number –
The issue with your LIKE clause is bad predicate logic ...like '%[^.0-9]%'should be NOT LIKE '%[^0-9.]%'
Take this sample data.
DECLARE #table TABLE (SomeNbr VARCHAR(32));
INSERT #table VALUES ('x'),('0'),('0.12'),('999'),('-29.33'),('88.33.22'),('9-9-'),('11-');
What you were trying to do would be accomplished like this:
SELECT t.someNbr
FROM #table AS t
WHERE someNbr NOT LIKE '%[^0-9.]%';
The problem here is we'll also return "88.33.22" and miss "-29.33", both valid float values. You can handle hyphens by adding a hyphen to your LIKE pattern:
SELECT t.someNbr, LEN(t.SomeNbr)-LEN(REPLACE(t.SomeNbr,'.',''))
FROM #table AS t
WHERE someNbr NOT LIKE '%[^0-9.-]%';
But now we also pick up "9-9-" and stuff with 2+ dots. To ensure that each starts with a number OR a hyphen, to ensure hyphens only exist in the front of the string (if at all) and that we a maximum of one dot:
--==== This will do a good job but can still be broken
SELECT t.someNbr
FROM #table AS t
WHERE someNbr NOT LIKE '%[^0-9.-]%' -- Can only contain numbers, dots and hyphens
AND LEN(t.SomeNbr)-LEN(REPLACE(t.SomeNbr,'.','')) < 2 -- can have up to 1 dot
AND LEN(t.SomeNbr)-LEN(REPLACE(t.SomeNbr,'-','')) < 2 -- can have up to 1 hyphen
AND PATINDEX('%-%',t.SomeNbr) < 2 -- hyphen can only be in the front
This does the trick and returns:
someNbr
--------------------------------
0
0.12
999
-29.33
All that said - **DONT DO THIS ANY OF THIS ^^^ **. There is no need to parse numbers in this way except to show others why not to. I can still break this. They way I return valid floats in a scenario like this is with TRY_CAST or TRY_CONVERT. This returns what you need and will perform better.
--==== Best Solution
SELECT t.someNbr
FROM #table AS t
WHERE TRY_CAST(t.SomeNbr AS float) IS NOT NULL;
I have a requirement where in I want to filter all rows in a table, which have a keyword 'Risk' in the description but not 'F Risk'. For example, in the following test data, the query should select rows 1,5,6 and 7.
However, when I am using the below query, it is eliminating id 1 (as it meets the second half of the condition) and just returning 5,6 and 7.
SELECT *
FROM TestDesc
WHERE ([desc] LIKE '%Risk%' AND [desc] NOT LIKE '%F Risk%')
I believe I would have to use regular expressions to achieve the desired result, but I am not very familiar with regular expression.
Any help/suggestion would be appreciated. Thanks!
I think you need to tweak the search for "F Risk":
SELECT *
FROM TestDesc
WHERE [desc] LIKE '%Risk%' AND [desc] NOT LIKE '%[ (]F Risk%'
I seem to have a small problem with updating a column within my database (postgres). I am trying to update column1, based on the string which is in column2. The logic behind it is; IF string contains "rail", then column 2 = "rail". IF string contains "air", then column 2 = "air".
I am trying to use CASE to make this work, and would eventually like to put this in a trigger.
I have searched through stackoverflow to find the solution, I see many similar problems but none of them worked for me.
UPDATE table1
SET column1 = CASE
WHEN column2 LIKE '%rail%' THEN 'rail'
WHEN column2 LIKE '%air%' THEN 'air'
ELSE 'other'
END;
I expect column 1 to output either rail, air or other. But it is only printing "other".
(UPDATE) - Solved problem, turns out case sensitivity within the '%%' field is important!
I have the following records and need to sort them accordingly:
AB*1
AB*2
AB*10
AB*100
I am using the following SQL Statement which works perfectly, BUT only for records which are filtered to a specific criteria.
SELECT Column1
FROM dbo.Table1
ORDER BY LEN(Column1), Column1
WHERE Column1 Like 'AB*'
When I remove the Where clause in the example, the record AB*100 appears way down below. Obiously, it has grouped together all the records with the Length of 4 then it starts all over with the records with length of 5 and so on.
Is it possible to order these so all before the asterisk are grouped together and sorted correctly?
The first character "alphabetically" is actually "no character". So it's not so much that "a string of 4 comes first". For example, the following are in order...
AB*1
AB*2
AB*10
AB*100
CD*1
CD*2
CD*10
CD*100
You could order by the first 3 characters as a string, and the trailing characters as a number. Provided that's how your data behaves...
ORDER BY
LEFT(column1, 3),
CLNG(MID(column1, 4, 8000))
Or you could pad out your values and order them as if they looked like this...
AB*100
AB*10Z
AB*1ZZ
AB*2ZZ
Using this kind of ORDER BY statement...
ORDER BY
column1 & string(8000 - LEN(column1), "z")
But these are all work-arounds to force an 'un-natural' ordering, as at present you're getting the 'correct' ordering.
This is likely to be slow:
ORDER BY Mid(Column1,1,Instr(Column1,"*"))
Edit re comment
SELECT Column1
FROM Table
ORDER BY Mid([Column1],1,InStr([Column1],"*")),
Val(Mid([Column1],InStr([Column1],"*")+1));
Sample data used in test:
ID
ab*1
ab*10
ab*2
abcdef*a1
abcdef*10
abcdef*40a
For nulls, just add a few zero-length strings.
SELECT column1
FROM Table
ORDER BY Mid([column1] & "",1,InStr([column1] & "","*")),
Val(Mid([column1] & "",InStr([column1] & "","*")+1));
it seems this will do what you want, unless I misunderstand
SELECT Column1
FROM dbo.Table1
ORDER BY left(Column1,2), LEN(Column1)