I am using this code to extract month and a year from a date:
to_char(to_date(i.CREATION_DATE, 'DD-MON-YYYY'), 'Mon-YY') "Month"
It works as required, however, when I extract the report to .csv, the value returned (e.g. Jan-2021) is formatted as text. When I then load the report to Tableau it creates many issues as Tableau does not recognise "Jan-2021" as a date, but a text.
I have tried many solutions, including EXTRACT, or by placing to_date before the to_char (which resulted in "ORA-01858: a non-numeric character was found where a numeric was expected" error).
Please, how do I make this work? Thank you.
There's no need to convert this to text before bringing into Tableau. Leave it as a date. Make yourself with familiar with Tableau date functions and date formats, and you can solve this very simply.
If it's a date in Tableau, and you want to display as MMM-YY, simply set the default format of that field as a custom format of mmm-yy
Related
I have a wrong date in the database 2022-10-14 12:59:00 , I want to change it to 2022-14-10 12:59:00. Is there a direct convert function I could use, or do I have to split and put the date correctly. Just trying to get some ideas to do this the easy way. I tried the convert functions but I guess the original date as to be corrected first is it?
You can use the CONVERT() function to go from a date type to a string with a particular "style". There are styles for different presentation formats to represent the different date presentation formats around the world.
To make this work, the data needs to be in a date type, not just a string. (or you can convert from a string type to a date type, then convert back to a string).
Date style descriptions
Lets say I have a table Student with columns Name,DOJ,TOJ.
Inorder to enter date in mm/dd/yyyy format and timestamp in the format hh24:mm:ss I used ALTER SESSION SET NLS_DATE_FORMAT='MM/DD/YYYY' and ALTER NLS_TIMESTAMP_FORMAT='HH24:MI:SS' but i want to know an alternative solution to enter in this format without involving session. Please guide me through this.
CLICK HERE TO VIEW COLUMNS AND THEIR DATA TYPES
We store dates/times either in a DATE column (which is Oracle's inappropriate name for a datetime) or a TIMESTAMP column (which has more precision and can handle timezones, too). These types have no format. This is important, because thus comparing and sorting them in the database works fine, because the DBMS knows how to handle datetimes, while the users see the date in their format. I, for instance, have set my Windows to German, so I will see the datetimes in some German format, while you will see the same date in a format you are used to.
There are few situations where you want to store date and time separately. The reason is typically that you can set them null. A date without a time means "the whole day". A time without a date means "every day this time". But then you often want this even more advanced anyway ("every Tuesday and Wednesday", "every December 24", etc.) for which you need soemthing more sophisticated then just date and time split into two.
So, usually we just store date and time combined. For a precision down to seconds we use DATE. If we wanted them separately we'd have to think of an appropriate data type, because Oracle has no real date type and no time type either. So we'd either use DATE and ignore the date part or the time part or we use strings. The former solution is usually preferred, because you cannot mistakenly enter invalid data like February 30 or 23:66:00.
If you want to store formatted dates and times, you are talking about strings. I don't recommend this. Strings are not the appropriate data types for dates and times. And a format '01/02/2000' is ambiguous. Some people will read this as February 1, others as January 2.
If you wanted to do this, you would have to change the column types to VARCHAR2 and simply store values like '02/25/2021' and '13:28:56'.
But if you wanted to sort your data or compare dates/times then or just show them to a foreign user in a format they are used to, you would always have to convert them. E.g.:
select *
from mytable
order by to_date(doj || ' ' || toj, 'mm/dd/yyyy hh24:mi:ss');
I am afraid that to change default format you have no other option but to change NLS_DATE_FORMAT either in database level or session level.
But If your purpose is to show the date in a specific format in the front end then you can use to_char() function as below:
SELECT NAME, to_CHAR(DOJ,'dd/mm/yyyy'),to_CHAR(TOJ,'HH24:MI:SS') FROM table
To change the default date format in system level:
ALTER SYSTEM SET NLS_DATE_FORMAT='DD/MM/YYYY' scope=both;
You can also change the default date format at startup time with a login trigger to change the nls_date_format:
CREATE OR REPLACE TRIGGER CHANGE_DATE_FORMAT
AFTER LOGON ON DATABASE
call DBMS_SESSION.SET_NLS('NLS_DATE_FORMAT','YYYYMMDD');
I am creating a custom audit log as a process in the Apex application I have developed.
Below is the code I have used to log the actions by the user when they use the application. The LOGON_DT and LOGOFF_DT will only need to be in a date format. However, QUERY_SEARCH_TIME will need the time.
INSERT INTO AUDIT_LOG
(USERNAME, ORDER_NO, ORDER_NAME, CUSTOMER_NAME, LOGON_DT, LOGOFF_DT, QUERY_SEARCH_TIME)
VALUES
(:APP_USER, :P10_ORDER_NO, :P10_ORDER_NAME, :P10_CUSTOMER_NAME, SYSDATE, SYSDATE,
SYSDATE(HH24:MI:SS));
The above code works perfectly when HH24:MI:SS is taken off, and I'm not sure where I'm going wrong with this?
Any guidance will be great. Thank you!
Oracle has it's own internal representation for a DATE datatype. You can't dictate its format in the way you are attempting.
It is common to apply a date format on retrieval, which you can do by applying the appropriate format mask as you convert to a string.
TO_CHAR(QUERY_SEARCH_TIME, 'HH24:MI:SS')
A DATE column already captures a time component. It may not be necessary to cut it off. And if you do, you do not want a DATE column as such a column will always capture a whole date, and what would be the point if you want only time.
To visualize the time component of a date you have to apply a format mask to it:
TO_CHAR(SYSDATE, 'HH24:MI:SS'). If you want to capture just that, the time, then change the datatype of the column to a VARCHAR2 type of sufficient length for the format mask you apply.
Read up on datetime datatypes here: Oracle documentation on datetime types
Read up on datetime format models here: Oracle documentation on format models
If you need time part of date only for output, you can use answer of Tom, but if you really need to store time part only, you can calculate it as
sysdate - trunc(sysdate)
I have a column which saves date in a varchar format. I can't change it because it is a part of the existing system. I need to convert this to datetime because I need to apply the datediff function.
The problem is that the dates are in several different formats.
Example:
14/09/2013 dd/mm/yy
20-06-2014 dd-mm-yy
1/29/2013 mm/dd/yy
2013-05-30-15.10.04.812055
8/3/2012 4:22:16 PM dd/mm/yy or mm/dd/yy can't make out the difference
I thought of switching dd and mm but that will be confusing to identify especially with the 1st 3rd and last case. When I convert as is, it gives me an out of range error. How do I fix this?
I am currently using SQL Server 2008
I have date stored in format mm/dd/yyyy in the database, but I need to view as Month Date(with superscript), Year. I am using Oracle 11g
The database just stores dates, in an internal format. It doesn't store them in mm/dd/yyyy, or anything human-readable.
You are sending them to the database in mm/dd/yyyy format, by the sound of things; but how you display them is down to your client application. You should be retrieving them from the database, and then formatting them at the client end.
In other words, don't do this in SQL at all.
The returned date can be manipulated as part of the query.
Example:
SELECT TO_CHAR(ts_col, 'DD-MON-YYYY') FROM date_tab;
The format string you would probably want is: 'MONTH DDTH, YYYY'
I would advise to just take whatever format the database gives you and handle it client-side though.
More information about TO_CHAR can be found in the oracle docs. A full list of format models can be found here. There is also Datetime Format Element Suffixes allowing for 2nd, 3rd, etc.
heres a work around if you want ... i dont know if this exactly what you want
SELECT REPLACE(TO_CHAR(SYSDATE, 'MonthDD,YYYY'),',','nd,') FROM DUAL;