Oracle: One attribute with date, another with date and time? - sql

I am taking an introductory course to databases so I am a complete beginner. In the database I am supposed to create I have two tables, each contain a DATE datatype.
In the first table, I want it to only display a date (DD-MM-YY) and in the second table display a date and time (DD-MM-YY HH24:MM).
How can I format each attribute to have these respective formats? I've looked around and tried the following command:
ALTER SESSION SET nls_date_format = 'YYYY-MM-DD HH24:MI:SS'
Which works nicely for the date and time field but leaves 00:00:00 for the date only field. Which I do not want, so I reverted it back to nls_date_format = 'DD-MM-YY'
As of right now the following:
INSERT INTO ITEM (ITEM_ENDDATEANDTIME)
VALUES ('13-AUG-13 23:56:00');
Gives me the error: date format picture ends before converting entire input string
Any ideas? Again, I'm a beginner a lot of this is new to me! Thanks!

What you want to do is always store your date values as dates. Data manipulation is infinitely easier when you have stored them in this format instead of in a text based format.
Then, you output them into a more human readable format through a query using syntax similar to this:
SELECT DateField,
TO_CHAR(DateField, 'YYYY-MM-DD HH24:MI:SS') AS Date1,
TO_CHAR(Datefield, 'DD-MM-YY') AS Date2
FROM MyTable
This takes the date data and outputs it as a formatted string. I hope this helps.

Related

Casting Date type to only display time

In my database(an Oracle 11 database) I have a attribute which is of Date type but has a time in the value for some reason, idk why it is Date type and not DateTime. When I select this " Position_time" Of course it just displays the date but when i attempt a filter on the column more options are shown of the same date for multiple times so a time value is present in this column even though it is of date type.
Link to picture of position_time context
As seen in the image even though the attribute is of type Date it contains a time "component" This is not shown in the overview btw only when i try to filter the column idk of that matters.
Id like to extract this time from my date. I've seen plenty of posts explaining how to extract from a DateTime column but not from a Date. I cannot change the type of this column. Is there any way to achieve this?
for example
select
format(tr.position_time)
from positions
Do you mean like this :
select to_char(to_date(position_time,'dd-mm-yyyy HH24:MI:SS'),
'HH24:MI:SS') time from positions;
if you already passing the date type as parameter then just use to_char function for extract the time from it.
E.g:
Select to_char(position_time,'HH24:MI:SS') from positions;
You would convert to a string:
select to_char(tr_position_time, 'HH24:MI:SS')
from positions;
In Oracle, date datatype consist of date + time.
It is the NLS setting of your IDE which is displaying the data like this.
If you want to show date and time then use:
select
To_char(tr.position_time,'dd-mon-rrrr hh24:mi:ss')
from positions
Or if you want just time portion then use:
select
To_char(tr.position_time,'hh24:mi:ss')
from positions
If you want to see all the dates in your session with time then alter your session's NLS setting.
ALTER SESSION SET NLS_DATE_FORMAT = 'dd-mon-yyyy hh24:mi:ss';
select
tr.position_time -- formatting is not needed
from positions
In your IDE also, there must be setting to change the NLS setting.
Cheers!!

Transform every row in a column to date, using first unix_timestamp

I have rows with the following format and I would like to transform then into valid Hive timestamps. Format in my data:
28/04/2017 00:00:00|20550|22/05/2017 00:00:00|
I'm only interested in the first and third column, separated with |, in MY case the format is, then:
dd/MM/yy HH:mm:ss
I've discovered this can't be used as timestamp in Hive.
I find myself unable to transform all that first and third column to the proper format using queries similar to:
select from_unixtime(unix_timestamp('28/04/2017','dd/MM/yy HH:mm:ss'),'yyyy-MM-dd') from `20170428_f_pers_pers`
I'm trying different instances of that query but since I can't access the documentation (internet is capped here at work), I can't see how to properly use this two functions, from_unixtime and unix_timestamp
I've made the following assumptions:
I can reorder the days and years. If this isn't true, I have no idea how to transform my original data into proper Hive format
When I do this select, it affects the whole column. Further, after doing this with success I should be able to change the format of the whole column from string to timestamp (maybe I have to create a new column for that, not sure)
I do not care about doing both columns at once, but right now when I do the query showed first I get as many nulls as data has my table, and I'm unsure my assumptions are even partially right since every example I come accross is simpler (they do not change days and years arround, for instance).
I would like to know how to apply the query to a specific column, since I haven't understood how to do that from the examples studied so far. I do not see them using any type of column ID for that, which is weird to me, using data from the column to change the column itself.
Thanks in advance.
edit: I am now trying something like
select from_unixtime(unix_timestamp(f_Date, 'dd/MM/yyyy HH:mm:ss')) from `myTable`
But I get from HUE the following error:
Error while processing statement: FAILED: Execution Error, return code 2 from org.apache.hadoop.hive.ql.exec.mr.MapRedTask
The format should be completely covered by the input string.
In other words -
The format can be equal in length to the the input string or shorter, but not longer.
28/04/2017 00:00:00
|||||||||||||||||||
dd/MM/yyyy HH:mm:ss
select from_unixtime(to_unix_timestamp('28/04/2017 00:00:00', 'dd/MM/yyyy HH:mm:ss'))
2017-04-28 00:00:00
28/04/2017 00:00:00
||||||||||
dd/MM/yyyy
select from_unixtime(to_unix_timestamp('28/04/2017 00:00:00', 'dd/MM/yyyy'))
2017-04-28 00:00:00
The result can be converted from string to timestamp using cast
select cast (from_unixtime(to_unix_timestamp('28/04/2017 00:00:00', 'dd/MM/yyyy HH:mm:ss')) as timestamp)

Convert timestamp to date data type

SELECT to_date(to_char(SYSTIMESTAMP, 'MM/DD/YYYY'), 'MM/DD/YYYY') FROM DUAL;
==> 04-MAR-16
Can anybody explain why this select statement doesn't result in '03/04/2016'?
How can I write my selection so that it does result in this, as a date type? I have also tried
SELECT to_date(to_char(trunc(SYSTIMESTAMP), 'MM/DD/YYYY'), 'MM/DD/YYYY') FROM DUAL
with the same result.
When a date is returned by a query and displayed, it obviously needs to be formatted in some way. The way a date-type value is formatted is not determined by the query, but by the tool that executes your query and displays the result.
In the case of SQL developer you can set that format as follows:
Choose menu Tools > Preferences.
In the Preferences dialog, select Database > NLS from the left panel.
From the list of NLS parameters, enter "MM/DD/YYYY"
Save and close
See also this question.
Note that to convert a timestamp to date you need just to truncate it: trunc(SYSTIMESTAMP). Converting it to string and then back to a date is unnecessary.
You are converting a datetime to a string and back to a date. Your system defaults the date format to DD-MMM-YY for output purposes; this is the normal default for date in Oracle.
It is important to understand that the internal data structure for date/time types has nothing to do with how they are presented. So, if you want it in another format, convert to a string using to_char().
If you want to change the default format, then look at NLS_DATE_FORMAT. The documentation is here.

Convert date format in Oracle

I have a date format 2011-01-06T06:30:10Z in Excel.I want to just load the date part into a table from excel.How do I get the date part from it.
i.e. 2011-01-06
Thanks
Try this:
select cast(TO_TIMESTAMP_TZ(REPLACE('2011-01-06T06:30:10Z', 'T', ''), 'YYYY-MM-DD HH:MI:SS TZH:TZM') as date) from dual
I think, some more explanation is needed.
Loading data into database is one part, and displaying it after fetching is another part.
If you have loaded the data into database, then all you need to do is use TRUNC. It will truncate the time portion and will display only the date portion.
A DATE always has a datetime part together. TIMESTAMP is an extension to the DATE type. And what you see the date looks like is not the way it is stored in database. The format is for we human beings to understand. A date is stored in 7 byte in internal format.
More information Based on OP's question via comments
NEVER store a DATE as VARCHAR2 datatype. A date is not a string literal. Oracle provides lot of FORMAT MODELS to display the datetime the way you want. Sooner or later, you will run into performance issues due to data conversion. Always use explicit conversion to convert a literal to a perfect DATE to compare it with other date value.

How to retrieve the time column value from oracleDB into .net application

I have a Oracle table where there is one date-time field.
On select query i am able to get all the field values but not timefield value in my .net application.
select abc.Id, abc.Name, abc.When from details abc where abc.Id='"+1234+"'
Could anyone suggest me.
The format of the returned DATE field from Oracle depends upon your default NLS settings in the database.
Oracle stores dates (and times) in an internal representation and when you select the date values you can then format them as you need. An official Oracle explaination is here.
To force the format you can explicitly convert the date to a string representation using:
select abc.Id,
abc.Name,
TO_CHAR(abc.When, 'DD-MON-YYYY HH24:MI:SS') AS when
from details abc
where abc.Id='"+1234+"'
If you are ONLY wanting the time portion of the WHEN column then:
select abc.Id,
abc.Name,
TO_CHAR(abc.When, 'HH24:MI:SS') AS when
from details abc
where abc.Id='"+1234+"'
This will then return it as a string rather than a date and time which may or may not be OK for you depending upon what you then want to do with it.
The format you choose for the date and time could be any of the Oracle date and time formats, see here.
Hope it helps...