how i can format a column - sql

i want to format a column in oracl 10g
its type is date
I want to show me always only hours and minutes
any solution with alter ..
thanks

If you want to show only hours / minutes from a timestamp field, you can use:
select to_char( sysdate, 'HH-MI' ) as sysdate_format from dual;
Date/Time Formatting in Oracle

Suppose this is what you have now:
SQL> alter session set nls_date_format = 'ddmmyy';
Session altered.
SQL> select sysdate from dual;
SYSDAT
------
131019
One option is to use TO_CHAR:
SQL> select to_char(sysdate, 'dd.mm.yyyy hh24:mi:ss') from dual;
TO_CHAR(SYSDATE,'DD
-------------------
13.10.2019 08:46:16
SQL>
but it requires you to always use it; another is to alter session and set desired format mask (which is probably what you want):
SQL> alter session set nls_date_format = 'dd.mm.yyyy hh24:mi:ss';
Session altered.
SQL> select sysdate from dual;
SYSDATE
-------------------
13.10.2019 08:45:50
SQL> select min(hiredate) from emp;
MIN(HIREDATE)
-------------------
17.12.1980 00:00:00
SQL>

To show just the time component of a date, you need to convert the value to a string.
For the hours in a 24-hour format, you can use:
select to_char(sysdate, 'HH24:MI')
from dual;
Of course, sysdate is a built-in date value. You would use whatever value or column you desire.

Related

Function Next_Day in pl/sql

Function is not working. What is wrong? I use database 18c xe.
It works, but you should use language your database speaks. Mine speaks Croatian.
(Just setting date format; you don't have to do that):
SQL> ALTER SESSION SET NLS_DATE_FORMAT = 'DD.MM.YYYY';
Session altered.
Your query doesn't work in my database either:
SQL> select next_day(sysdate, 'MONDAY') from dual;
select next_day(sysdate, 'MONDAY') from dual
*
ERROR at line 1:
ORA-01846: not a valid day of the week
But, if I use Croatian name for Monday, then it works:
SQL> select next_day(sysdate, 'PONEDJELJAK') from dual;
NEXT_DAY(S
----------
03.01.2022
Or, alter session - then the 1st query works as well:
SQL> ALTER SESSION SET NLS_DATE_LANGUAGE = 'ENGLISH';
Session altered.
SQL> select next_day(sysdate, 'MONDAY') from dual;
NEXT_DAY(S
----------
03.01.2022
SQL>
You're from ... Poland? Follow what I suggested and try with "poniedziaƂek".
select next_day(sysdate, 'mon') from dual
can you try this one?
set the nls_date_language as per your language AND TRY
alter session set NLS_DATE_LANGUAGE = "ENGLISH";

Time stamp date to mm/dd/yyyy in oracle sql

I have a field st_ts that shows as 07/09/2021 5:20:52 PM. How do I get it to show as just 07/09/2021? I've tried TO_DATE('ST_TS','MM/DD/YYYY') but it isn't working. In there where clause I have to do TRUNC(ST_TS) = '09-JUL-2021' to select the date. Thanks.
It depends on what that column's datatype is. I presume it is DATE. If that's so, have a look at the following examples:
SQL> create table test (st_Ts date);
Table created.
SQL> insert into test values (to_date('07/09/2021 05:20:52', 'mm/dd/yyyy hh24:mi:ss'));
1 row created.
Altering the session and setting date format mask:
SQL> alter session set nls_date_format = 'mm/dd/yyyy';
Session altered.
SQL> select * from test;
ST_TS
----------
07/09/2021
SQL> alter session set nls_date_format = 'dd.mm.yyyy hh24:mi:ss';
Session altered.
SQL> select * from test;
ST_TS
-------------------
09.07.2021 05:20:52
Applying TO_CHAR function with desired format mask:
SQL> select to_char(st_ts, 'mm/dd/yyyy') result from test;
RESULT
----------
07/09/2021
Truncating DATE value "resets" time portion to midnight:
SQL> select trunc(st_ts) result2 from test;
RESULT2
-------------------
09.07.2021 00:00:00
SQL>
Therefore, you have various options. It depends on what you're up to. If you're using some reporting tool, I'd suggest you to set field's format mask there. If you just want to display it differently, use TO_CHAR. For the whole session length, alter the session.

Issue with NLS_DATE_FORMAT

Why Am I seeing different date formats below : see the output of select statement and output of anonymous block
13:46:23 13:46:23 SQL> conn prashant-mishra/ *****#*****
Connected to Oracle Database 12c Enterprise Edition Release 12.1.0.2.0
13:47:56 13:47:56 SQL> CREATE TABLE test AS SELECT SYSDATE as test_date
FROM dual;
Table created
13:48:15 13:48:15 SQL> SELECT test_date FROM test;
TEST_DATE
-----------
**10/13/2016**
13:50:17 13:50:17 SQL> BEGIN
2 FOR rec IN (SELECT test_date FROM test) LOOP
3 dbms_output.put_line(rec.test_date);
4 END LOOP;
5 END;
6 /
**13-OCT-16**
PL/SQL procedure successfully completed
Other helpful info may be :
13:50:20 13:50:20 SQL> SELECT value FROM v$nls_parameters
WHERE parameter ='NLS_DATE_FORMAT';
VALUE
----------------------------------------------------------------
DD-MON-RR
13:50:45 13:50:45 SQL> SELECT SYSDATE FROM dual;
SYSDATE
-----------
**10/13/2016**
SQL*Plus uses NLS_DATE_FORMAT to format dates as strings so a complete test case needs to show what this parameter is beforehand. Would you please try the following and see if you get consistent formatting:
SQL> select value from nls_session_parameters where parameter = 'NLS_DATE_FORMAT';
VALUE
----------------------------------------------------------------
DD-MON-RR
SQL> select sysdate from dual;
SYSDATE
------------------
13-OCT-16
SQL> alter session set nls_date_format = 'YYYY-MM-DD';
Session altered.
SQL> select sysdate from dual;
SYSDATE
----------
2016-10-13
SQL> alter session set nls_date_format = 'DD-MON-RR';
Session altered.
SQL> select sysdate from dual;
SYSDATE
------------------
13-OCT-16
SQL>
You get the following output based on your comments:
SQL> select value from nls_session_parameters where parameter = 'NLS_DATE_FORMAT';
VALUE
DD-MON-RR
SQL> select sysdate from dual;
SYSDATE
10/14/2016
SQL> alter session set nls_date_format = 'YYYY-MM-DD';
Session altered
SQL> select sysdate from dual;
SYSDATE
10/14/2016
SQL> alter session set nls_date_format = 'DD-MON-RR';
Session altered
SQL> select sysdate from dual;
SYSDATE
10/14/2016
So - SQLPlus is ignoring the session NLS_DATE_FORMAT and using it's only local format of MM/DD/YYYY. I didn't think there was a date format override in SQL*Plus but could be wrong. Have you tried the same commands in a different client?

How to get time string to Time in oracle sql

I have insert time in form of string in oracle VARCHAR2 column. But when I try to retrieve it in form of time it's not giving me right time, it's only giving me a date which I have not saved.
INSERT INTO table1
(timestr) Select substr(numtodsinterval(MAX(date1)-MIN(date2),'day'),
12,8) from table2 where ....; // stored timestr column value: 00:00:00
Retrieve ...
select TO_DATE(timestr,'hh24:mi:ss') from table1;
... is only giving 10/01/2015
You should use to_char to see the time
select to_char(timestr,'hh24:mi:ss') from table1;
That might be because of your client setting. If you are using SQL Developer, then go to Tools->Preference->Database->NLS and change setting to view timestamp also.
If you are using SQLPlus, the change nls_date_format or see below solution.
https://community.oracle.com/thread/312115?start=0&tstart=0
In Oracle there is no concept of a TIME datatype. There are only DATE and TIMESTAMP, both of which comprise date and time elements.
Your retrieval casts your time column to a DATE: TO_DATE(timestr,'hh24:mi:ss'). Oracle does what we ask it to do, so it displays a date. As there is no date element in timestr it assigns one for us: today's date.
You are only seeing 10/01/2015 because your client's NLS settings are configured to display only the date element. Change the setting to show the time element, in the client or by altering the session as here:
SQL> select to_date('10:08:23', 'hh24:mi:ss') ts from dual
2 /
TS
---------
01-OCT-15
SQL> alter session set nls_date_format='DD-MON-YYYY HH24:MI:SS'
2 /
Session altered.
SQL> select to_date('10:08:23', 'hh24:mi:ss') ts from dual
2 /
TS
--------------------
01-OCT-2015 10:08:23
SQL>
If you only want to see the time you can change the NLS settings ...
SQL> alter session set nls_date_format='HH24:MI:SS'
2 /
Session altered.
SQL> select to_date('10:08:23', 'hh24:mi:ss') ts from dual
2 /
TS
--------
10:08:23
SQL>
...but that will get annoying ....
SQL> select sysdate from dual
2 /
SYSDATE
--------
23:59:52
SQL>
So why not just display the column as a string all without casting to a DATE?
select timestr from table1;

How to add date and time in Oracle

I have the following details:
date_time: 08-Dec-14 07:52:52 along with other fields.
I insert using the following:
insert into employee (DATE_TIME, NAME, EMPLOYEE_ID)
values(TO_DATE('08-Dec-14 07:52:52','DD-MON-YY HH24:MI:SS'), 'XYZ', 123);
When I query the database for a return of thedate_time, I get only 08-Dec-14 and not the time. I have set this field as DATE.
What changes need to be made so I can get the time as well?
Thanks in advance.
use select query like below
select to_char(date_time,'Required date format') from employee.
Required date format: 'DD-MON-YY HH24:MI:SS' or'DD/MM/YYYY' etc.
When I query the database for a return of the date_time, I get only 08-Dec-14 and not the time. I have set this field as DATE.
It is a display format which your client is using. it depends on the locale-specific NLS date settings.
You can override the local NLS settings using TO_CHAR at individual query level.
For example,
SQL> alter session set nls_date_format='DD-MON-YYYY';
Session altered.
SQL> SELECT sysdate FROM dual;
SYSDATE
-----------
24-JUL-2015
Using TO_CHAR to override the NLS dependent format:
SQL> SELECT to_char(sysdate, 'MM/DD/YYYY HH24:MI:SS') dt FROM dual;
DT
-------------------
07/24/2015 12:21:01