String to Date SQL - sql

I have a Comments (Clob) field which is a collection of the time the comment was made, comment made by and the comment itself.
I need to extract the 'Date', 'Comment by' from the field.
Using Regular expressions, I was able to extract the date fields and comment_by field but the date is in character.
I'm not able to convert this field into Date.
Comments Example:
7/18/2018 10:36:29 AM, beginj requesting cancellation as steps are no longer viable.
5/16/2018 8:28:04 AM, josephav Not applicable for GSC
I can get the 'date' using this regular expression (RE):
regexp_substr((dbms_lob.substr(requestcommentsdisplay,50,1)),'[^ ]+',1)
I can get the Comment_by name using this regular expression (RE)
regexp_substr((dbms_lob.substr(requestcommentsdisplay,50,1)),'[a-z]+',1)
So the dates after I extract using RE are like below:
10/5/2017
4/2/2012
12/31/2015
3/16/2014
I need to convert these into Proper dates so that I can use these further in my code
Any Help is appreciated :)
Thanks

Use
SELECT
CASE WHEN (length(<regex_string>).
<9)
Then
TO_DATE('0'||<regex_string>,'MM/DD/YYYY')
but oracle parses it with single digit as well though you can
try to debug and print dates for single digit via this
condition.
ELSE
TO_DATE(<regex_string>,'MM/DD/YYYY')
END CASE FROM TABLE;
to convert the string to date format as the o/p you got from regex_substr is a string.
This will allow you to read the input in the format you are receiving and will convert to actual oracles date format.

You can set nls_territory parameter depending on your country, before calling the query
alter session set nls_territory = 'AMERICA';--alternatively convert to 'TURKEY' as an example
and then try the following
with tab(date_chr) as
(
select '10/5/2017' from dual union all
select '4/2/2012' from dual union all
select '12/31/2015' from dual union all
select '3/16/2014' from dual
)
select to_date(date_chr,'MM/DD/YYYY')
from tab;

Related

Using oracle apex dynamic action to set a date time value that is concatenated

I have a date value in one page item and i have a time value in another. i want to concat the date with the time and set it to another item using a dynamic action. Structure as below
P_DATE = '01-01-2021' (item is a date with format mask dd-mm-yyyy)
P_TIME = '08:30 AM' (item is date with format mask HH:MIAM)
Query:
select to_date(to_char(to_date(P_DATE ,'DD-MON-YYYY'), 'DD-MON-YYYY') || ' '||
to_char(P_TIME,'HH:MIAM'),'DD-MON-YYYY HH:MIAM') a
from dual;
Desired Outcome: 01-01-2021 08:30 AM
Dynamic action is on change the P_DATE item then from an sql query, concat the P_DATE and P_TIME and set it to P_VALUE
When i run the select in sql developer with hardcoded values then it returns the correct stuff but when i try to set the value in the item with the concat date it giving me invalid number error sometimes and not a valid month.
Can you suggest the corrected way or an alternative way of doing this (maybe use a function)
THank you.
You can make that much simpler, example
with test as (
select '01-01-2021' d, '08:30 PM' t
from dual)
select to_date(d||' '|| t,'MM-DD-YYYY HH:MI PM')
from test;
What do you mean by "they are date items" ? Are they of the type "Date Picker" , do they have a source column of type "DATE" in a form, or do they map to a DATE column in the database ?
In APEX, all page items are basically strings. The frontend of the web application doesn't know about oracle datatypes so everything is treated as a plain string and during the processing the conversion is done. So that is how you should treat the page items, not as DATE data type like you would in SQL or PL/SQL. To concatenate a date string and a time string, you can just a plain concatenate without TO_CHAR. This can be done in plain PL/SQL, no need to SELECT FROM DUAL - that is just an unnecessary call to the SQL engine.
This is the "true" action on the change of P_VALUE. Tested on 21.1, so depending on your version there might be some attribute naming differences but it works the same.
Action: execute server side code.
Source:
:P_VALUE := :P_DATE ||' '||:P_TIME
Items to submit: P_DATE, P_TIME.
Items to return: P_VALUE
Since you're dealing with strings there is room for error here, you'll have to ensure proper error handling if the user input does not exactly match the format since that could generate invalid date values.
From comments:
the page items are both date items
Since they are dates, you can simply use:
SELECT p_date + (p_time - TRUNC(p_time)) AS a
FROM DUAL;

How to convert unknown variable of value 2019-12-31 into date format

Data = 2019-12-31
Don't know the data type of the data variable
Below query
Select to_date (data, 'yyyy-mm-dd') from dual
Working as follows
Select to_date (2019-12-31, 'yyyy-mm-dd') from dual;
output is
Error
Input value not long enough for date format
Don't say i haven't given single quotes .becoz i cant give single quotes to 'data' in the query
Can u give solution s assuming scenarios where the data comes without single quote or where data is number or date datatype
select to_char(to_date(20191231,'yyyymmdd'),'mm-dd-yyyy') from dual;
Convert number to date sql oracle

Convert date to month year format and pass it in the query

I have a BIRT report where user will be entering the dates in dd-mm-yyyy format however I need to convert dd-mm-yyyy to MON-YYYY format.
I have tried to use VARCHAR_FORMAT(FIELDNAME,'MON-YYYY') however it doesn't work.
select …….
where VARCHAR_FORMAT(fieldname,'MON-YYYY') = '2017-05-15';
User would end the date as
15/05/2017
The value present in the database for this field is 2017-05-15 07:30:00.0
update
Apparently the column is not a string but a datetime which means the conversion is only
to_date(fieldname, 'MON-YYYY')
But if the column is used in a Where clause it shouldn’t be converted at all.
——
Use to_date and to_char to first convert your string to a date and then back to a string with the right format
to_char(to_date(fieldname, 'DD-MM-YYYY'), 'MON-YYYY')
select *
from table (values
timestamp('2017-05-15-07.30.00')
) t(fieldname)
where
fieldname between to_date('15/05/2017', 'DD/MM/YYYY') and to_date('15/05/2017', 'DD/MM/YYYY') + 1 day
--date(fieldname) = to_date('15/05/2017', 'DD/MM/YYYY')
;
You may run it as is.
Both cases work, but to use the 2-nd one efficiently, you must create an index by the date(fieldname) expression (since db2 10.5) or add generated always column to the table with the same expression and index on it.

BigQuery: Validate that all dates are formatted as yyyy-mm-dd

Using Google BIGQUERY, I need to check that the values in a column called birth_day_col are the correct and desired date format: YYYY-MM-DD. The values in this column are defined as STRING. Also the values in this column are currently of the following format: YYYY-MM-DD.
I researched a lot on the internet and found an interesting workaround. The following query:
SELECT
DISTINCT birth_day_col
FROM `project.dataset.datatable`
WHERE birth_day_col LIKE '[1-2][0-9][0-9][0-9]/[0-1][0-9]/[0-3][0-9]'
AND country_code = 'country1'
But the result is: "This query returned no results."
I then checked with NOT, using the following code:
SELECT
DISTINCT birth_day_col
FROM `project.dataset.datatable`
WHERE NOT(birth_day_col LIKE '[1-2][0-9][0-9][0-9]/[0-1][0-9]/[0-3][0-9]')
AND country_code = 'country1'
Surprisingly it gave all the values in birth_dat_col, which I have verified and are of the correct date format, but this result coud very much be a coincidence.
And it is very strange (wrong) that I used a query that should result only the wrong format dates, but it actually gives me the correct ones. Everything about these two queries seems like an inversation of each one's role.
The expected result of any query for this business case is to make a count of all incorrect formatted dates (even if currently this is 0).
Thank you for your help!
Robert
A couple of things here:
Read the documentation for the LIKE operator if you want to understand how to use it. It looks like you're trying to use regular expression syntax, but the LIKE operator does not take a regular expression as input.
The standard format for BigQuery's dates is YYYY-MM-DD, so you can just try casting and see if the result is a valid date, e.g.:
SELECT SAFE_CAST(birth_day_col AS DATE) AS birth_day_col
FROM `project`.dataset.table
This will return null for any values that don't have the correct format. If you want to find all of the ones that don't have the correct format, you can use SAFE_CAST inside a filter:
SELECT DISTINCT birth_day_col AS invalid_date
FROM `project`.dataset.table
WHERE SAFE_CAST(birth_day_col AS DATE) IS NULL
The result of this query will be all of the date strings that don't use YYYY-MM-DD format. If you want to check for slashes instead, you can use REGEXP_CONTAINS, e.g. try this:
SELECT
date,
REGEXP_CONTAINS(date, r'^[0-9]{4}/[0-9]{2}/[0-9]{2}$')
FROM (
SELECT '2019/05/10' AS date UNION ALL
SELECT '2019-05-10' UNION ALL
SELECT '05/10/2019'
)
If you want to find all dates with either YYYY-MM-DD format or YYYY/MM/DD format, you can use a query like this:
SELECT
DISTINCT date
FROM `project`.dataset.table
WHERE REGEXP_CONTAINS(date, r'^[0-9]{4}[/\-][0-9]{2}[/\-][0-9]{2}$')
For example:
SELECT
DISTINCT date
FROM (
SELECT '2019/05/10' AS date UNION ALL
SELECT '2019-05-10' UNION ALL
SELECT '05/10/2019'
)
WHERE REGEXP_CONTAINS(date, r'^[0-9]{4}[/\-][0-9]{2}[/\-][0-9]{2}$')
Yet another example for BigQuery Standrad SQL - with use of SAFE.PARSE_DATE
#standardSQL
WITH `project.dataset.table` AS (
SELECT '1980/08/10' AS birth_day_col UNION ALL
SELECT '1980-08-10' UNION ALL
SELECT '08/10/1980'
)
SELECT birth_day_col
FROM `project.dataset.table`
WHERE SAFE.PARSE_DATE('%Y-%m-%d', birth_day_col) IS NULL
with result of list of all dates which are not formatted as yyyy-mm-dd
Row birth_day_col
1 1980/08/10
2 08/10/1980
Google BigQuery's LIKE operator does not support matching digits nor does it uses the [ character in its syntax (I don't think ISO standard SQL does either - LIKE is nowhere near as powerful as Regex).
X [NOT] LIKE Y
Checks if the STRING in the first operand X matches a pattern specified by the second operand Y. Expressions can contain these characters:
A percent sign "%" matches any number of characters or bytes
An underscore "_" matches a single character or byte
You can escape "\", "_", or "%" using two backslashes. For example, "\%". If you are using raw strings, only a single backslash is required. For example, r"\%".
You should use REGEX_CONTAINS instead.
I note that string format tests won't tell you if a date is valid or not, however. Consider that 2019-02-31 has a valid date format, but an invalid date value. I suggest using a datatype conversion function (to convert the STRING to a DATE value) instead.

Parse a string to a date without slashes in Oracle

In Oracle, I'm able to parse a string to a date type if the user input: dd/mm/yyyy.
I would like to be able to parse without the slash:
01022012 should be parsed to 01/02/2012
a way is text processing and insert slash, then convert to date type. Is there another simple way?
TNX
select to_date('01022012','DDMMYYYY') from dual
==>
TO_DATE('01022012','DDMMYYYY')
------------------------------------
01-Feb-2012
-------- End of Data --------
1 row(s) fetched
TO_DATE doesn't require you to have any separating characters between your date parts. It might also be worth noting for your future reference that if the user input contains characters oracle doesn't care about (it only cares about date parts like dd, mm, yyyy) then they can be anything. All these work fine:
SELECT to_date('12/12/2012', 'dd/mm/yyyy') from dual
SELECT to_date('12-12-2012', 'dd/mm/yyyy') from dual
SELECT to_date('12#12;2012', 'dd#mm%yyyy') from dual
To_date doesn't insist that the separator characters and other ignored text in the date format picture shall match up with the ignored text in the input