At Amazon Athena, I want to extract only the character string "2017-07-27" from the character string "2017-07-27 12:10:08".
SELECT SUBSTRING (event_datetime.s, 0, 10) FROM production limit 10
I tried it like this which only returns numbers 0 to 10.
At Athena, is it possible to cut character strings? If so, how can I do it?
Or, if you know how to cast "2017-07-27 12:10:08" to date type, that's fine.
Thank you.
You can use SUBSTR to substring a column value.
Here is the string function reference page.
In your case, this would lead to the following statement:
SELECT SUBSTR(event_datetime.s, 1, 10) FROM production limit 10
NOTE that the index position of the first character is 1 (not zero), the same as in standard SQL.
SELECT name
FROM players
WHERE name ~ '(.*){8,}'
It is really simple but I cannot seem to get it.
I have a list with names and I have to filter out the ones with at least 8 characters... But I still get the full list.
What am I doing wrong?
Thanks! :)
A (.*){8,} regex means match any zero or more chars 8 or more times.
If you want to match any 8 or more chars, you would use .{8,}.
However, using character_lenth is more appropriate for this task:
char_length(string) or character_length(string) int Number of characters in string
CREATE TABLE table1
(s character varying)
;
INSERT INTO table1
(s)
VALUES
('abc'),
('abc45678'),
('abc45678910')
;
SELECT * from table1 WHERE character_length(s) >= 8;
See the online demo
0
votes
1 view
Hi,
I have a scenario, where in we have a single record and multiple columns like this
Record_number cd1 cd2 cd3 cd4 cd5 cd6 cd7 cd8 cd9
Here are values for the for the above record
123 12 null 13 14 null 15 16 17 null
Here we have value for cd1 and not for cd2 and we have value for cd3 so cd2 is empty so cd3 should get into cd2 since it was empty so we should move the next available values to previous available spaces.
Does anyone know how to achieve this scenario?
If I understand your question correctly, this can be easily done using an expression transformation.
First create a variable port that would concatenate all the non-null values using some separator, e.g.|, like:
IIF(ISNULL(cd1),'',cd1 || '|') ||
IIF(ISNULL(cd2),'',cd2 || '|') ||
...
IIF(ISNULL(cd9),'',cd9 || '|') ||
This should result in pipe delimited list with the nulls removed, e.g.:
123|12|13|14|15|16|17|
Next create an output port for each column, that would get the correct substring using index of the separator. I'll leave coding this part to you, but if you'd have issues, please let me know.
One approach could be to use an expression transformation as follows:
First concatenate all the ports in a single field using a delimiter (for example ',')
v_CONCAT:= cd1||','||cd2||','||cd3||','||cd4||','||cd5||','||cd6||','||cd7||','||cd8||','||cd9
Now create the output ports using regular expressions
o_cd1:=REG_EXTRACT(v_CONCAT,',*([^,]*),*([^,]*),*([^,]*),*([^,]*),*([^,]*),*([^,]*),*([^,]*),*([^,]*),*([^,]*)',1)
o_cd2:=REG_EXTRACT(v_CONCAT,',*([^,]*),*([^,]*),*([^,]*),*([^,]*),*([^,]*),*([^,]*),*([^,]*),*([^,]*),*([^,]*)',2)
o_cd3:=REG_EXTRACT(v_CONCAT,',*([^,]*),*([^,]*),*([^,]*),*([^,]*),*([^,]*),*([^,]*),*([^,]*),*([^,]*),*([^,]*)',3)
.
.
and so on.
REG_EXTRACT extracts a substring from the input string that matches the regular expression.
() - a group. There will be as many groups as your ports cd1-cd12
,* - zero or more commas, if a value is null two commas will appear consecutively
[^,] - anything other than comma
[^,]* - zero or more of any character other than comma
The third parameter to REG_EXTRACT denotes which group you want as output. For o_cd1 we want the first non-NULL value, so it is 1 and for o_cd2, it is 2 and so on
ok guys, im trying to convert char based on text filename. the name extracted using regex. it works perfectly until meet this date August21st.
select to_char(
to_date(
regexp_replace(
regexp_replace(
regexp_replace(
regexp_replace(
regexp_replace(
substr('UserAndMasterPlanPerAugust21st2015.txt',21),
'.txt',''),
'rd','-'),
'th','-'),
'nd','-'),
'st','-'),
'MonthDD-YYYY'),
'YYYYMMDD')::integer
that code will produce this errors.
ERROR: invalid value "Augu-21st" for "Month"
DETAIL: The given value did not match any of the allowed values for this field.
********** Error **********
ERROR: invalid value "Augu-21st" for "Month"
SQL state: 22007
Detail: The given value did not match any of the allowed values for this field.
i expect the result for this date is
20150821
i already know the problem is on 'st' because there are two 'st', i just trying the best way to solve this.
Thanks
I think that you may use another regular expression, I suggest you to try to use this one to remove ordinals from a date like:
(?<=[0-9])(?:st|nd|rd|th)
example here
You only need to escape it to postgres dialect...
I stripped the date from the string then casted to a date in the format that it is currently in. You then convert to char with the format that you want. CHEERS!
select to_char(
to_date(
substring('UserAndMasterPlanPerAugust21st2015.txt'
from 21 for length('UserAndMasterPlanPerAugust21st2015.txt')-24
), 'MonthDDthYYYY'), 'YYYYMMDD');
The purpose of the -24 is because I only want to move to the right the number of spaces that the date is. If the total length is n and I start from 21 that means i have n-20 characters left. I want to remove the .txt as well so move 4 less spaces. therefore I want to move from 21 -> to length of the string-24.
I've tried searching online for the answer to this, but my Google-fu has failed me.
I have an Access database containing records represented by a string. The first 3 characters of that string are a 3-digit representation of the 366-day calendar date on which the record was created (000-366...yes, leap days count).
I'm having trouble coming up with the correct pattern match to include in a query that matches a 3-digit substring that can be between 000 and 366, where you don't lose the significant figures.
I know the query would be something like:
SELECT * FROM myTable WHERE Field1 LIKE "^[0-2]## or 3[0-5]# or 36[0-6]*";
...but I can't find any resource that says, in MS Access, what the "or" operator is. I tried "||" (double pipe) and "|" (single pipe), neither of which worked.
Is there an "or" operator that can be used with a MS Access pattern match?
The LIKE operator in Access is pretty limited, and doesn't support most of the features more 'fully-fledged' regular expression engines provide.
Instead, use multiple conditions in your WHERE clause like this:
SELECT *
FROM myTable
WHERE Field1 LIKE "[0-2]##*" OR
Field1 LIKE "3[0-5]#*" OR
Field1 LIKE "36[0-6]*"
Another alternative is to simply extract the first 3 characters to a string, convert them to an integer and test to see if their value is within the acceptable range.
Why not just pull the first three characters?
SELECT * FROM myTable WHERE CInt(Left(Field1,3)) <= 366
http://www.techonthenet.com/access/functions/datatype/cint.php