Solved

Read datetime field in as Date in oracle sql

Posted on 2014-04-01
4
537 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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

705 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