Solved

Read datetime field in as Date in oracle sql

Posted on 2014-04-01
4
535 Views
Last Modified: 2014-04-01
Experts,

I have a field that is datetime on an Oracle table.  I would like to read the field in as date in a proc sql statement.  

Can this be done?
0
Comment
Question by:morinia
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 23

Accepted Solution

by:
paquicuba earned 350 total points
ID: 39969530
Yes, you can do it explicitly or you can let Oracle handle the conversion for you:

declare
l_date date;
begin
select systimestamp into l_date from dual;
dbms_output.put_line(l_date);
dbms_output.put_line(trunc(l_date));
dbms_output.put_line(to_char(l_date,'MM/DD/YYYY'));
end;
/
0
 
LVL 23

Expert Comment

by:paquicuba
ID: 39969537
If you have a varchar2 variable, then you can format the date/timestamp as you like:

DECLARE
l_date VARCHAR2(4096);
BEGIN
SELECT to_char(SYSTIMESTAMP,'DD-MON-RR') INTO l_date FROM dual;
dbms_output.put_line(l_date);
END;
/
0
 

Author Comment

by:morinia
ID: 39969556
Experts,

The date is coming in as part of a Select statement.  ie

Select memberid
       ,member,lname
       ,member_fname
       ,member_dob
From members.

Can member_dob be formatted in this type of query.  Some languages you can use datepart(d, member_dob)
0
 
LVL 23

Expert Comment

by:paquicuba
ID: 39969639
OK. What exactly do you want to display?

Run the following query and let me know if anything looks like it:

WITH members AS
(
 SELECT
    1 memberid
   ,'UNCLEAR' member_lname
   ,'MORINIA' member_fname
   ,SYSDATE - INTERVAL '20' YEAR member_dob
  FROM dual
)
SELECT memberid
       ,member_lname
       ,member_fname
       ,to_char(member_dob,'MM/DD/YYYY') member_dob_1
       ,to_char(member_dob,'DD-MON-RR') member_dob_2
       ,EXTRACT(DAY FROM member_dob) member_dob_day
       ,EXTRACT(MONTH FROM member_dob) member_dob_month
       ,EXTRACT(YEAR FROM member_dob) member_dob_year
FROM members
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question