I am trying to update several dates to the end of the month. However, my table has two separate date formats ('DD-MON-YY' and 'YYYYMMDD'). How can I update both dates in an update statement? Also, I want the new dates to be in 'YYYYMMDD' format.
Update MY_TABLE
set MY_DATE = TO_CHAR(LAST_DAY(TO_DATE(MY_DATE,'DD-MON-YY')),'YYYYMMDD');
As David noted in the comments, the "real" solution here would be to add a date column and use that. If that's not an option, you could differentiate old data from new data based on the existence of the - character:
UPDATE my_table
SET my_date =
TO_CHAR(LAST_DAY(TO_DATE(my_date, CASE WHEN my_date LIKE '%-%'
THEN 'DD-MON-YY'
ELSE 'YYYYMMDD'
END)),
'YYYYMMDD')
Related
I have a table that have multiple date format on big query table. The Date format is followed:
(please note, this is coming from staging table so the data type is defaulted as string type.
mm/dd/yyyy
yyyy-mm-dd
I'm trying to standardized date format to yyyy-mm-dd. Here is the query as followed:
select calendar_date,
FORMAT_DATE('%Y-%m-%d',safe_cast(calendar_date as date))
FROM `calendar_dates.table`
The result that came out as null
please let me know if there is another way to solve this.
Use below
SELECT
calendar_date,
COALESCE(SAFE.PARSE_DATE('%Y-%m-%d', calendar_date), SAFE.PARSE_DATE('%d/%m/%Y', calendar_date))
FROM `calendar_dates.table`
The below query, that uses safe.parse_date seems to work for me:
WITH stg AS (
SELECT "9/9/2001" AS input_date
UNION ALL
SELECT "2021-01-01" AS input_date)
SELECT
CASE
WHEN safe.parse_DATE("%Y-%m-%d", input_date) IS NULL THEN
safe.parse_DATE("%d/%m/%Y", input_date)
ELSE safe.parse_DATE("%Y-%m-%d",input_date)
END AS output_date
FROM
stg
The table
becomes:
The logic is that: BQ tests one format and if it cannot make any sense of it, it tests the other format.
If none of them work the result is NULL.
How can we compare a date to a format in Oracle?
Something like this: if MyDate is on format DD MONTH YYYY THEN /....
elsif MyDate is on format YYYY-MONTH-DD Then...
EDIT: My dates are in varchar2 and i want to keep them that way. I want just to know how to write a regex that would reprensent for example 10 October 2010.
Is it possible ? If it is a regex how would its format be please
Echoing what was mentioned in the comments to your question, best practice would be to have an actual DATE type field instead of VARCHAR2, and if you needed specific display formats, store those in another field as a format pattern. That said, you can use REGEXP_LIKE to check the format using the patterns in the below example.
with dateinfo as (
select 1 as id, '2018-MARCH-10' as dtString from dual
union all
select 2 as id, '10 MARCH 2018' as dtString from dual )
select id, dtString,
case
when regexp_like(dtString, '^[0-9]{4}-.[a-zA-Z]{3,}-.[0-9]{1,2}$') then 'format1'
when regexp_like(dtString, '^[0-9]{1,2} [a-zA-Z]{3,} [0-9]{4}$') then 'format2'
else 'no format'
end as dtFormat
from dateinfo;
I'm trying to do simple select and Update in Oracle 11g using a date where clause and I can't produce the result I expect.
The date field is datemodified and currently has values like 12-JUL-14 and of data type date.
I'm doing the following and don't get result:
select *
from table
where datemodified = to_date(datemodified, '12-JUL-14')
I tried the following and still did not produce result:
select * from table
where to_date(datemodified, 'DD-MON-YY') = to_date('12-JUL-15',
'DD-MON-YY')
or
Update table
set column = 'some value'
where datemodified = to_date('12-JUL-15', 'DD-MON-YY')
What am I doing wrong?
You can try this query
select * from table
where to_char(datemodified, 'dd-MON-yy') = '12-JUL-15'
Oracle date fields include the time of day. Also, filtering on function results such as to_char() slows down production. This method works:
where dateModified >= the date you want
and dateModified < the day after the date you want.
It's ok to use functions for the values, this for example
where dateModified >= trunc(sysdate)
or
where dateModified >= to_date('12-JUL-15','DD-MON-YY')
but not on the field itself
I want to update one date column in oracle table for the below scenario...
currentYR=Get the Current Year from the SYSDATE (2015)
currentYR=currentYR-2(2013)
Need to set date field like 01-JUN-13(Last TWO DIGITS OF currentYR(2013)
Basically i just want to set set date like year=2 years earlier from sysdate and month=june,date=1. and date format is like DD-MON-YY(01-JUN-13)
Please guide me to resolve this problem.
try this:
update yourtable
set yourcolumn = add_months(sysdate, -24)
-24 means 2 years earlier
Based on the comment above I think you would like to set the value of the column to a date where only the last two digits of the year are provided. You can do so by using a Datetime format model reflecting this:
UPDATE <your table> SET <yourcolumn> = TO_DATE('01-JUN-13','DD-MON-YY') WHERE ...;
Finally found the answer. This is the query which i wanted...
SELECT ADD_MONTHS (TRUNC (SYSDATE, 'YEAR'), -19 ) FROM DUAL;
QUERY RESULT
01-JUN-13
I have a column in the oracle database with data type varchar, this column is accepting the different types of the data as follows
03/01/2012
01 JANUARY 2012
1 FEBRUARY 2013
13-JAN-2012
03/01/2012 MM-ss
now I want to convert in to the one single format as 'DD/MM/YY or else any one of the standard format
Tha
As discussed above:
ALTER TABLE table_name MODIFY column_name DATE
To add new column:
ALTER TABLE table_name add (column_name DATE);
To drop column:
ALTER TABLE table_name drop column col_name1;
I suggest a methodical approach. Step 1 is to add a new column to your table with a date datatype. Step 2 is to modify all applicable applications so that they use the new column, not the old one. Then do a series of update queries.
update yourTable
set newDateColumn = to_date(oldVarcharColumn, 'mask')
where newDateColumn is null
and regexp_like (oldVarcharColumn, pattern)
Finally drop the oldVarcharColumn.
As others have suggested, if you need to convert the date values into a single data type in the table, then you can follow a step-by-step approach.
Since you have various patterns of values in your existing column, you can use a CASE statement in conjunction with the regular expressions (REGEXP_LIKE) to convert the values, as below:
SELECT
text_date,
CASE
WHEN REGEXP_LIKE(text_date, '^\d{2}/\d{2}/\d{4}$') THEN to_date(text_date, 'MM/DD/YYYY')
WHEN REGEXP_LIKE(text_date, '^\d{2} ([A-Z]|[a-z])+ \d{4}$') THEN to_date(text_date, 'DD MONTH YYYY')
WHEN REGEXP_LIKE(text_date, '^\d{1} ([A-Z]|[a-z])+ \d{4}$') THEN to_date(text_date, 'DD MONTH YYYY')
WHEN REGEXP_LIKE(text_date, '^\d{2}-([A-Z]|[a-z]){3}-\d{4}$') THEN to_date(text_date, 'DD-MON-YYYY')
WHEN REGEXP_LIKE(text_date, '^\d{2}/\d{2}/\d{4} MM-ss$') THEN to_date(text_date, 'MM/DD/YYYY "MM-ss"')
ELSE NULL
END date_value
FROM text_dates
WHERE REGEXP_LIKE(text_date, '^\d{2}/\d{2}/\d{4}$')
OR REGEXP_LIKE(text_date, '^\d{2} ([A-Z]|[a-z])+ \d{4}$')
OR REGEXP_LIKE(text_date, '^\d{1} ([A-Z]|[a-z])+ \d{4}$')
OR REGEXP_LIKE(text_date, '^\d{2}-([A-Z]|[a-z]){3}-\d{4}$')
OR REGEXP_LIKE(text_date, '^\d{2}/\d{2}/\d{4} MM-ss$')
ORDER BY text_date;
In the above query, each pattern is detected using a regular expression.
See SQL Fiddle demo.
Reference:
REGEXP_LIKE Condition on Oracle® Database SQL Language Reference
Regular Expressions Cheat Sheet on PSOUG.ORG