Solved

Read datetime field in as Date in oracle sql

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Fill Null values 5 54
date show only hh:mm 2 50
Reference datasets inside PL/SQL cursor and dumping queried datasets 3 54
oracle query 4 30
Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
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.

732 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