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

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...

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!!

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.

creating table in Oracle with Date

I want to create a table in Oracle 10g and I want to specify the date format for my date column. If I use the below syntax:
create table datetest(
........
startdate date);
Then the date column will accept the date format DD-MON-YY which I dont want.
I want the syntax for my date column to be MM-DD-YYYY
Please let me know how to proceed with this.
Regards,
A DATE has no inherent format. It is not simply a string that happens to represent a date. Oracle has its own internal format for storing date values.
Formats come into play when actual date values need to be converted into strings or vice versa, which of course happens a lot since interactively we write dates out as strings.
The default date format for your database is determined by the settings NLS_DATE_FORMAT, which you probably have set to DD-MON-YYYY (which I believe is the default setting for American English locales). You can change this at the database level or for a single session for convenience, but in general it is safer programming practice to be explicit so that you don't get errors or, worse, wrong results if your code is run in a different environment.
The simplest way to specify a date value unambiguously is a date literal, which is the word 'date' followed by a string representing the date in YYYY-MM-DD format, e.g. date '2012-11-13'. The Oracle parser directly translates this into the corresponding internal date value.
If you want to use a different format, then I recommend explicitly using TO_CHAR/TO_DATE with your desired format model in your code. Examples:
INSERT INTO my_table (my_date) VALUES ( TO_DATE( '11-13-2012', 'MM-DD-YYYY' ) );
SELECT TO_CHAR( my_date, 'MM-DD-YYYY' ) FROM my_table;
dates rdo not have a format like you're suggesting. they are stored internally as a 7 byte number. to format the date when selecting, please use TO_CHAR(yourdatefield, 'format')
where formats are all shown here: http://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements004.htm#i34924
eg to_char(startdate, 'mm-dd-yyyy')

Updating a date in Oracle SQL table

I am trying to update a date in a SQL table. I am using Peoplesoft Oracle. When I run this query:
Select ASOFDATE from PASOFDATE;
I get 4/16/2012
I tried running this query
UPDATE PASOFDATE SET ASOFDATE = '11/21/2012';
but it is not working.
Does anyone know how I would change the date to the one desired?
This is based on the assumption that you're getting an error about the date format, such as an invalid month value or non-numeric character when numeric expected.
Dates stored in the database do not have formats. When you query the date your client is formatting the date for display, as 4/16/2011. Normally the same date format is used for selecting and updating dates, but in this case they appear to be different - so your client is apparently doing something more complicated that SQL*Plus, for example.
When you try to update it it's using a default date format model. Because of how it's displayed you're assuming that is MM/DD/YYYY, but it seems not to be. You could find out what it is, but it's better not to rely on the default or any implicit format models at all.
Whether that is the problem or not, you should always specify the date model:
UPDATE PASOFDATE SET ASOFDATE = TO_DATE('11/21/2012', 'MM/DD/YYYY');
Since you aren't specifying a time component - all Oracle DATE columns include a time, even if it's midnight - you could also use a date literal:
UPDATE PASOFDATE SET ASOFDATE = DATE '2012-11-21';
You should maybe check that the current value doesn't include a time, though the column name suggests it doesn't.
Here is how you set the date and time:
update user set expiry_date=TO_DATE('31/DEC/2017 12:59:59', 'dd/mm/yyyy hh24:mi:ss') where id=123;
If this SQL is being used in any peoplesoft specific code (Application Engine, SQLEXEC, SQLfetch, etc..) you could use %Datein metaSQL. Peopletools automatically converts the date to a format which would be accepted by the database platform the application is running on.
In case this SQL is being used to perform a backend update from a query analyzer (like SQLDeveloper, SQLTools), the date format that is being used is wrong. Oracle expects the date format to be DD-MMM-YYYY, where MMM could be JAN, FEB, MAR, etc..
Just to add to Alex Poole's answer, here is how you do the date and time:
TO_DATE('31/DEC/2017 12:59:59', 'dd/mm/yyyy hh24:mi:ss')