Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Read datetime field in as Date in oracle sql

Posted on 2014-04-01
4
Medium Priority
?
544 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
  • 3
4 Comments
 
LVL 23

Accepted Solution

by:
paquicuba earned 1400 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

Industry Leaders: 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

Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
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.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.
Suggested Courses

926 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