Toad SQL Issue for Timestamp without Seconds - oracle

I am executing the following sql in Toad. Oracle is RDBMS
I only need Date in yyyymmdd HH24:mi, but I get Date only as shown below
alter session set nls_date_format = 'DD/MM/YYYY HH24:MI';
SELECT to_date('22/07/1980 00:00','dd/mm/yyyy hh24:mi') dt FROM dual
22/07/1980
Required Output
22/07/1980 00:00

You are looking for to_char() -- you want to return the date as a string, not a date. As far as I know, the date is returned without the time, and I don't think the NLS changes that.
So:
SELECT to_char(to_date('22/07/1980 00:00', 'dd/mm/yyyy hh24:mi'
), 'DD/MM/YYYY HH24:MI'
) as dt
FROM dual

I have used sql plus and spooled the file instead of Toad, used NLS Date format conversion here, but as this is command based, wanted to use GUI based TOAD.

Related

Date importing from oracle to hive using spark hive context format in hive should be YYYYMMDD (dt_skey)

I have to import a table from Oracle to Hive using Spark and Scala, date column in Oracle looks like this Oracle column date, I have to cast it to the dt_skey format (YYYYMMDD) in Hive. Table format in Hive is Parquet. How can I do that? I googled it a lot but I didn't find any solution.
Thanks in advance
Assuming your input data is supposed to mean yy-mm-dd (so 16-09-15 means year 2016, month 09, day 15) you probably need a transformation like this:
select to_char( to_date (dt, 'yy-mm-dd'), 'yyyymmdd') from ...
Example:
with my_table ( dt ) as ( select '16-09-15' from dual)
-- this creates a test table my_table with column dt and value as shown
select dt,
to_char( to_date (dt, 'yy-mm-dd'), 'yyyymmdd') as dt_skey
from my_table
;
DT DT_SKEY
-------- --------
16-09-15 20160915
You could also manipulate the input string directly but I would strongly recommend against that. Translating to date and back will catch invalid "dates" in your data before you try to push them to an application. Also, the string manipulation would become complicated if the input strings are inconsistent (for example if something like 16-9-15 is allowed along with 16-09-15).
EDIT: In a comment to his original question, the OP stated that dt is already in DATE format in Oracle. In that case, it should NOT be wrapped within to_date() - that will lead to errors. Rather, the solution is MUCH simpler, all is needed is
select to_char(dt, 'yyyymmdd') from ...

Oracle Date Format Conversion Issue

Am at the end of my tether so hoping someone can help me! I'm really new to Oracle, but do have a SQL background which is why I'm finding this so frustrating!
We have a system that runs Oracle at the back end. I've got very limited access to the system and can only write select queries.
I've written a query that gets the data I want but the date format is coming out as mm dd yyyy what I need is dd/mm/yyyy
I ran SELECT sysdate FROM dual and that come back as:
SYSDATE
03 11 2015
So my select statement reads (action_date is the column in question)
Select username, action_date from adminview
I've tried everything I can think of to change the date format including:
to_date(action_date,'dd/mm/yyyy')
to_date(action_date,'dd/mm/yyyy','nls_language=English')
to_date(to_date(action_date,'mm dd yyyy'),'dd/mm/yyyy')
I've also tried to_char along the same lines.
If you want to format a DATE value, use TO_CHAR():
SELECT username, TO_CHAR(action_date, 'DD/MM/YYYY') AS action_date
FROM adminview;
If it's not a DATE value, then you'll want to convert it to a DATE (based on what it currently looks like), then use TO_CHAR() to format.

Months between sysdate to earlier date

I want to get the months between current date to earlier date.
SELECT MONTHS_BETWEEN(to_date(fld_valid_from,'yyyy-mm-dd hh24:mi:ss'),TO_DATE(sysdate, 'yyyy-mm-dd hh24:mi:ss')) num_months
FROM tbl_customer
But it's not working. I don't know if that's correct or not.
sysdate is already a date, and does not need to be converted to one using to_date(). I suspect that fld_valid_from is also a date.
What error message are you getting? Did you try comparing the two variables without the TO_DATE command?
SELECT MONTHS_BETWEEN (trunc(fld_valid_from),trunc(sysdate))
FROM tbl_customer
This would work if your column fld_valid_from is a date type. You're comparing a date with another date. You use TO_DATE to convert a string data type to a date data type.
With the TRUNC function, you remove the timestamp from the date, and get only...well, the date:
SELECT SYSDATE, TRUNC(SYSDATE) FROM DUAL;
SYSDATE TRUNC(SYSDATE)
---------------------------------------
16/07/2013 10:45:53 16/07/2013
Hope it helps.
Regards.

Getting all data between certain date time

I am reading data from remote oracle database with read only access. and I want to query all data between some time frame. I actually wanted to query between mid-night and current time. so the query I was using :
TO_DATE(to_char(sysdate, 'MM-dd-yyyy')||'00:00:00','MM-dd-yyyy HH24:MI:SS' )
AND
TO_DATE(to_char(sysdate, 'MM-dd-yyyy HH24:MI:SS'),'MM-dd-yyyy HH24:MI:SS' )
But the query
select TO_DATE(to_char(sysdate, 'MM-dd-yyyy HH24:MI:SS'),'MM-dd-yyyy HH24:MI:SS' ) from dual
is returning only 18-APR-12 not the time. How do I get time too?
I am running :
ALTER SESSION SET NLS_DATE_FORMAT='DD-MON-YYYY HH24:MI:SS';
But I don't want to run this everytime any other way to overcome this problem?
What you see is not what is really there.
Oracle holds the date as a number represanting the date and the way you see it when you query it depends on your NLS parameters.
The fact that you don't see the time doesn't mean there is no time.
as for your query I'd do something like this:
... BETWEEN trunc(sysdate) AND sysdate
I see no use in casting a Date to a string and then back to a Date with the same format ...

How to populate a timestamp field with current timestamp using Oracle Sql Loader

I'm reading a pipe delimited file with SQL Loader and want to populate a LAST_UPDATED field in the table I am populating. My Control File looks like this:
LOAD DATA
INFILE SampleFile.dat
REPLACE
INTO TABLE contact
FIELDS TERMINATED BY '|'
OPTIONALLY ENCLOSED BY '"'
(
ID,
FIRST_NAME,
LAST_NAME,
EMAIL,
DEPARTMENT_ID,
LAST_UPDATED SYSTIMESTAMP
)
For the LAST_UPDATED field I've tried SYSTIMESTAMP and CURRENT_TIMESTAMP and neither work. SYSDATE however works fine but doesn't give me the time of day.
I am brand new to SQL Loader so I really know very little about what it is or isn't capable of. Thanks.
Have you tried the following:
CURRENT_TIMESTAMP [ (precision) ]
select current_timestamp(3) from dual;
CURRENT_TIMESTAMP(3)
-----------------------------
10-JUL-04 19.11.12.686 +01:00
To do this in SQLLDR, you will need to use EXPRESSION in the CTL file so that SQLLDR knows to treat the call as SQL.
Replace:
LAST_UPDATED SYSTIMESTAMP
with:
LAST_UPDATED EXPRESSION "current_timestamp(3)"
I accepted RC's answer because ultimately he answered what I was asking but my unfamiliarity with some of Oracle's tools led me to make this more difficult than it needed to be.
I was trying to get SQL*Loader to record a timestamp instead of just a date. When I used SYSDATE, and then did a select on the table it was only listing the the date (05-AUG-09).
Then, I tried RC's method (in the comments) and it worked. However, still, when I did a select on the table I got the same date format. Then it occurred to me it could just be truncating the remainder for display purposes. So then I did a:
select TO_CHAR(LAST_UPDATED,'MMDDYYYY:HH24:MI:SS') from contact;
And it then displayed everything. Then I went back to the control file and changed it back to SYSDATE and ran the same query and sure enough, the HH:MI:SS was there and accurate.
This is all being done in SqlDeveloper. I don't know why it defaults to this behavior. Also what threw me off are the following two statements in sqldeveloper.
SELECT CURRENT_TIMESTAMP FROM DUAL; //returns a full date and time
SELECT SYSDATE FROM DUAL; // returns only a date
If you want to use the table defined default you can use:
ROWDATE EXPRESSION "DEFAULT"
In Sql Developer run:
ALTER SESSION SET NLS_DATE_FORMAT='YYYY-MM-DD HH24:MI:SS'
and then check it with
SELECT SYSDATE FROM DUAL

Resources