Oracle - retrieve date having timestamp values - oracle

We have a situation, where we have a table (say Forms) in Oracle DB which has a column (say edition_date) of type date. It was strictly meant to hold the date information in YYYY-MM-DD (ex: 2012-11-23)format with no timestamp value.
Unfortunately, due to a code problem, lot of rows got created with timestamp values. There are tons of records in this table, and I want to update only those rows which had this bad data. I can do that using the query
UPDATE forms SET edition_date = TRUNC( edition_date )
I want to add a where clause to it, so it updates only the bad data. The problem, I am not sure how to retrieve those rows that has timestamp added to it. Below is a snapshot of the data I have:
FORM_ID EDITION_DATE
5 2012-11-23
6 2012-11-23 11:00:15
..
11 2010-07-11 15:23:22
..
13 2011-12-31
I want to retrieve only the row with form ids 6 and 11. I trioed using the length functions but I think that is good for Strings only. Is there any way to do this. Thanks anyone who can help me.

A date has no format; you're only seeing how it's displayed. However, the answer to your question is, effectively, what you've said:
I want to add a where clause to it, so it updates only the bad data.
So, where the date is not equal to the date without time:
update forms
set edition_date = trunc(edition_date)
where edition_date <> trunc(edition_date)
To ensure that this doesn't happen again you can add a check constraint to your table:
alter table forms
add constraint chk_forms_edition_date
check ( edition_date = trunc(edition_date) );
I would advise against all of the above. Don't destroy potentially useful data. You should simply select trunc(edition_date) where you do not want time. You may want to use the time in the future.
You're correct, do not use LENGTH() etc for dates, it depends on your NLS_DATE_FORMAT settings and so could be different on a session-by-session basis. Always use date functions when dealing with dates.

Related

Datedim function not returning yesterdays date webi

My Datedim function is not returning yesterdays date in webi, any ideas on how to show 13/04/2022, even if it has null values?
Thanks
If you have gaps in your date data the simplest way to fill them in is to create a variable with the TimeDim() function. However, that will not work for you since you do not have a true gap because your missing date is at the end.
You need a data source with all the dates you want to display regardless of if you have data for those dates or not and then merge on your date dimension. I answered a question very similar to this here. I am copying my answer from there below...
The TimeDim() function will fill in the empty periods in your time
data. The problem with that though is if it is the end of your date
range that is missing data those dates will not show up. Let me show
you what I mean. Here is my sample data from 12/01/2021 through
12/26/2021 (note missing dates) in the table on the left. The table on
the right is the my Var Data Date TimeDim variable defined as…
=TimeDim([Data Date]; DayPeriod)
So we have our missing dates in the middle, but not at the end
(12/25/2021 and 12/26/2021). To get those dates you need a query to
return all the dates in your specified range. If you have a universe
based on a
calendar
you could use that. Free-hand SQL based on a calendar table would
suffice as well.
If you have neither of those we can still get it to work using
free-hand SQL with a CTE. This is SQL Server syntax. You will have to
modify this SQL to work for whatever database platform you have if it
isn’t SQL Server.
Here is the SQL…
;with dates ([Date]) as (
Select convert(date,‘2021-12-01’) as [Date] – Put the start date here
union all
Select dateadd(day, 1, [Date])
from dates
where [Date] < ‘2021-12-26’ – Put the end date here
)
select [Date]
from dates
option (maxrecursion 32767) – Don’t forget to use the maxrecursion option!
Source: Generate a Date Table via Common Table Expression (CTE) |
Data and Analytics with Dustin
Ryan
Here is a
demo.
Now that you have a query returning all of the dates in your range
you can merge the date from that query to your Data Date.
You can then put the date object with all of dates or the Merged Date
in table with any measures from your pre-existing query and there you
have it.
If you need to add dimensions from you pre-existing query I think you
will need to create variables for them with Qualification set to
“Detail” and the Associated dimension set to “Merged Date” (or
whatever you called it). And if you do that I believe you will also
need to check “Avoid duplicate row aggregation” check box within the
Format Table properties.
Let us know how it goes.
Hopefully that will get you on the right track.

Query not filtering with date in Oracle

There are records in table for particular date. But when I query with that value, I am unable to filter the records.
select * from TBL_IPCOLO_BILLING_MST
where LAST_UPDATED_DATE = '03-09-21';
The dates are in dd-mm-yy format.
To the answer by Valeriia Sharak, I would just add a few things since your question is tagged Oracle. I was going to add this as a comment to her answer, but it's too long.
First, it is bad practice to compare dates to strings. Your query, for example, would not even execute for me -- it would end with ORA-01843: not a valid month. That is because Oracle must do an implicit type conversion to convert your string "03-09-21" to a date and it uses the current NLS_DATE_FORMAT setting to do that (which in my system happens to be DD-MON-YYYY).
Second, as was pointed out, your comparison is probably not matching rows due LAST_UPDATED_DATE having hours, minutes, and seconds. But a more performant solution for that might be:
...
WHERE last_update_date >= TO_DATE('03-09-21','DD-MM-YY')
AND last_update_date < TO_DATE('04-09-21','DD-MM-YY')
This makes the comparison without wrapping last_update_date in a TRUNC() function. This could perform better in either of the following circumstances:
If there is an index on last_update_date that would be useful in your query
If the table with last_update_date is large and is being joined to other tables (because it makes it easier for Oracle to estimate the number of rows from your table that are inputs to the join).
Your column might contain hours and seconds, but they can be hidden.
So when you filter on the date, oracle implicitly adds time to the date. So basically you are filtering on '03-09-21 00:00:00'
Try to trunc your column:
select * from TBL_IPCOLO_BILLING_MST
where trunc(LAST_UPDATED_DATE) = '03-09-21';
Hope, I understood your question correctly.
Oracle docs

Validating Date Prompt To be less than Current Date OBIEE

I have a requirement to create two validations on the date prompt:
1) The From Date has to be less than To Date
2) The To Date has to be less than or equal to current date
I created a conditional analysis wherein From Date is < To Date, which works, but when I try to create an advanced filter wherein #To_Date <= Current_Date I am getting an error.
Error getting drill information: SELECT date'2016-08-24' saw_0 FROM "Workforce Management - Processed Time Cards Real Time" WHERE(date'#{To_Date}' <= (SELECT VALUEOF("CURRENT_DATE_REP_OTBI") FROM "Workforce Management - Processed Time Cards Real Time" FETCH FIRST 1 ROWS ONLY))
If anyone can help solve this, it'd be really helpful!
Thanks
You need to add a default value when referencing presentation variables in logical SQL queries or formulas. Especially if these are dates.
I created an analysis based on the following LogicalSQL and it worked.
SELECT date'2016-08-26' saw_0 from "subject_area" WHERE (date
#{to_date}{'2016-08-26'} < CURRENT_DATE)
Notice the following:
The presentation variable #{to_date} goes with a default value (noted by the second curly brackets). This helps OBIEE to validate the query. Failing to add the default value will give you the "getting drill information" error.
Instead of a session RPD variable, you can use CURRENT_DATE. It simplifies the query.
The above query will return the date in the SELECT clause, but if the to_date is greater than CURRENT_DATE will return no data.

how to check the last_modified date with null values in oracle

I have a table called orders in which data is loaded through a CSV file into a loader table for every three hours. I have a column last_modified set to SYSDATE which records the insert and update on the table. Recently, I have observed that the last_modified column has null values for more than 100k records when update happens. Is there any way to fix this issue?
Merge into orders d
using (select * from ods_prm_data ) s
on (d.order_id = s.order_id)
when not matched then
insert (d.order_id ,d.ID, d.last_modified)
values (s.order_id, s.ID,s.order_seq.val,SYSDATE)
when matched then
update set d.last_modified = SYSDATE;
Take a look at the WHEN NOT MATCHED branch of the MERGE statement. I'm rather surprised that this statement even compiled because the INSERT field list (d.order_id ,d.ID, d.last_modified) specifies three fields, but the VALUES list (s.order_id, s.ID,s.order_seq.val, SYSDATE)shows FOUR values. I'm not sure how this slipped past the compiler, and I have no idea how it's being executed; however, it may help to explain the issues you're having.
Best of luck.

Insert a datetime value with GetDate() function to a SQL server (2005) table?

I am working (or fixing bugs) on an application which was developed in VS 2005 C#. The application saves data to a SQL server 2005. One of insert SQL statement tries to insert a time-stamp value to a field with GetDate() TSQL function as date time value.
Insert into table1 (field1, ... fieldDt) values ('value1', ... GetDate());
The reason to use GetDate() function is that the SQL server may be at a remove site, and the date time may be in a difference time zone. Therefore, GetDate() will always get a date from the server. As the function can be verified in SQL Management Studio, this is what I get:
SELECT GetDate(), LEN(GetDate());
-- 2010-06-10 14:04:48.293 19
One thing I realize is that the length is not up to the milliseconds, i.e., 19 is actually for '2010-06-10 14:04:48'. Anyway, the issue I have right now is that after the insert, the fieldDt actually has a date time value up to minutes, for example, '2010-06-10 14:04:00'. I am not sure why. I don't have permission to update or change the table with a trigger to update the field.
My question is that how I can use a INSERT T-SQL to add a new row with a date time value ( SQL server's local date time) with a precision up to milliseconds?
Check your table. My guess is that the FieldDT column has a data type of SmallDateTime which stores date and time, but with a precision to the nearest minute. If my guess is correct, you will not be able to store seconds or milliseconds unless you change the data type of the column.
I would guess that you are not storing the GetDate() value in a DateTime field. If you store the value in a datetime field you will get the maximum precision allowed by the DateTime type. Additionally, DateTime is a binary type (a double actually) so 19 means 19 bytes, not 19 characters.
Try to create a simple table with a Datetime field like this
CREATE TABLE [dbo].[DateTable](
[DateField] [datetime] NOT NULL
)
And add a date with
insert into datetable (datefield) values(getdate())
When you execute a select you will get back a value including milliseconds. The following query
select * from datetable
returns
2010-06-11 00:38:46.660
Maybe this would work instead of getdate -
SYSDATETIME()
look here if you can find what you need -
http://msdn.microsoft.com/en-us/library/ms188383.aspx
As you're on SQL 2005, don't forget the getutcdate() function to ensure that, regardless of where your servers are actually located, you have a constant time reference.
Imagine, you have the server in the UK in winter (i.e. GMT+0), and save a record at 10:30am. You then cut over to a SQL server hosted in California (GMT+8) and 8 hours later save another record.
Using getdate(), both saves record the same time "10:30:00". Using getutcdate(), the first save records at "10:30:00", the second save records "18:30:00".
Not really answering the question, but important in your circumstances.
You can use like this in procedure and If there is no procedure use only getdate().
insert into [dbo].[Tbl_User] (UserId,Uvendoremail,UAddress,Ddob,DMobile,
DEmail,DPassword,DAddress,CreatedDate) values (#userid,#vendoremail#address,#dob,#mobile,#email,#dpassword,#daddress,getdate())

Resources