I will need to extract the year from a column name , , it is returning null value and the same number of character. i would want to only extract the date however there is a few column with the same number of character .
sample data in table
10020020
1172053041
597246141
3339110821
26590621
192133643
20190203
20180109
20170204
20190904
I have tried this,
select regexp_extract((colname), '([0-9]{8})', 1) from tablename
however it is returning the result that has the same number of characters with null values. i wish to only extract only the date which is 20170109,20190204 etc etc . what is the best approach and what did i go wrong ?
10020020
26590621
20190203
20180109
20170204
20190904
i have tried using wildcard select regexp_extract((maxvalue), '([0-9]{8})', 1) like '%2019%' from profilingoverviewreport but it returning boolean instead
If you want to match values which have exactly 8 digits, and those 8 digit values correspond to dates, then I suggest the following pattern:
^(20|19)[0-9]{6}$
Your updated SQL code:
SELECT *
FROM tablename
WHERE colname IREGEXP '^(20|19)[0-9]{6}$';
Check the demo to see the regex pattern correctly identifying the dates in your column:
Demo
Related
I have a requirement where I have to find number of records in a special pattern in the field ref_id in a table. It's a varchar column. I need to find all the records where 8th, 9th and 10th character are numeric+XX. That is it should be like 2XX or 8XX. I tried using regexp :digit: but no luck. Essentially I am looking for all records where 8th-10th characters are 1XX, 2XX, 3XX… etc
Using REGEXP_LIKE, replace table with Yours:
SELECT COUNT(*)
FROM table
WHERE REGEXP_LIKE(ref_id,'^.{7}[0-9]XX');
.{7} whatever seven characters
[0-9] 8th character digit
XX 9th and 10th characters X
Or with [:digit:] class as You are mentioning, You may use:
SELECT COUNT(*)
FROM table
WHERE REGEXP_LIKE(ref_id,'^.{7}[[:digit:]]XX');
This can also be achieved using standard non-regex SQL functions
select * from t where s like '________XX%' -- any 8 characters and then XX
AND translate( substr(s,8,1),'?0123456789','?') is null; --8th one is numeric
DEMO
No need for a regexp:
select * from mytable where substr(ref_id, 8, 3) in ('0XX','1XX','2XX','3XX','4XX','5XX','6XX','7XX','8XX','9XX')
or
select * from mytable where substr(ref_id, 8, 3) in ('1XX','2XX','3XX','4XX','5XX','6XX','7XX','8XX','9XX')
I don't know if '0XX' is a valid match or not.
Regexp's tend to be slow.
I am trying to write one postgress sql query , here i want to select all the rows , for which one column must contains string of below format , i am not able to construct regular expression for postgress , as am new to this DB
one column contains a string like PRF:12345678901-1234 like this column values are present , so write a sql query to select all rows which have column value in this format , here PRF: is constant it will never change the only changing value is that string after : symbol , can someone please help me in this .
SELECT * FROM your_table WHERE your_column ~ '^[A-Z]{3}:\d{11}-\d{4}$';
~ tilde is used for regex evaluation and you can evaluate that the expression matches your needs here
You could try
SELECT * FROM <table> WHERE <column> ~ 'PRF:\d{11}-\d{4}'
Here is a demo
I have a column with assignment numbers like - 11827,27266,91717,09818-2,726252-3,8716151-0,827272,18181
Now i am selecting the records like
select assignment_number from table;
But now i want that the column detail is retreived in such a way that numbers are only retrieved without -2 -3 etc like
726252-3---> 726252 8716151-0-->8716151
I know i can use regex for this but i do not know how to use it
This will select everthing before the character -:
^([^-]+)
From 726252-3 will match 726252
You would use regexp() substr:
select regexp_substr(assignmentnumber, '[0-9]+')
This will return the first string of numbers encountered in the string.
I have a column of data in my database that has a project number the numbers are formatted like this, YYYYnnnn.ee (for example 20140124.00). Since there are three distinct parts of the number, the user could search by either the YYYY or the nnnn. I have written a query that searches by the YYYY. I need to write a query for the nnnn.
How would you write a query to search for a specific string in a specific location of another string?
You can use SUBSTRING for that.
The first paramter to SUBSTRING should be the columname, the next parameter is the index of the start character and the last parameter is the number of characters.
The example below will return the nnnn part from your column. In the example below we are searching for records where the nnnn equals 1234.
SELECT *
FROM tablename
WHERE
(SUBSTRING(columnName, 5, 4) = '1234')
Please note, if your column is a number (decimal). You will need to first cast it as a varchar (string). For example:
SELECT *
FROM tablename
WHERE
(SUBSTRING(CAST(columnName AS VARCHAR(20)), 5, 4) = '1234')
I have the following table
DRIVER_GID DRIVER_REFNUM_QUAL_GID
SDL2/C001.100000 SDL2.486900 CURRENT DISTRICT
SDL2/C001.100000 SDL2.486900 PERMANENT DISTRICT
SDL2/C001.100000000 SDL2.486900 CURRENT DISTRICT
SDL2/C001.100000000 SDL2.486900 PERMANENT DISTRICT
SDL2.600119036 SDL2.436001 CURRENT DISTRICT
SDL2.600119036 SDL2.436001 PERMANENT DISTRICT
I need to extract the numeric value after the string "SDL2." from DRIVER_REFNUM_QUAL_GID column. could anyone please recommend a query.
Use regexp_substr function of ORACLE:
select regexp_substr(DRIVER_REFNUM_QUAL_GID, '[[:digit:]]{6}') from YOURTABLE
This will extract adjacent 6 digits from DRIVER_REFNUM_QUAL_GID column of your yourtable.
If you prefer to extract all digits following period then use the following code:
select regexp_substr(DRIVER_REFNUM_QUAL_GID, '(\.)([[:digit:]]+)',1,1,'i',2) from YOURTABLE
To eliminate NULLS, you can use NVL function.
For example,
select NVL(regexp_substr(DRIVER_REFNUM_QUAL_GID, '(\.)([[:digit:]]+)',1,1,'i',2),999999) from YOURTABLE
So, if the result of the regexp_substr function is NULL, the result will be 999999.
Considering you only need numbers after SDL2. and considering that the number of digits could go up to a max of 20 digits, you can use something like-
select REGEXP_SUBSTR('SDL2.486900 CURRENT DISTRICT', '[[:digit:]]{2,20}') numval from dual;
You can edit the number 20 to suit your needs.
So it would look like-
select REGEXP_SUBSTR(DRIVER_REFNUM_QUAL_GID,'[[:digit:]]{2,20}') from your_table;