Table of Contents
How does oracle query time format
Home Database Oracle How to query the time format in oracle

How to query the time format in oracle

Jan 26, 2022 pm 03:01 PM
oracle

In Oracle, you can use the "select sysdate from dual;" statement to query the time format. The select statement can perform simple query on the data. This statement is used to format the output date.

How to query the time format in oracle

The operating environment of this tutorial: Windows 10 system, Oracle 11g version, Dell G3 computer.

How does oracle query time format

1. Oracle stores time format data

Oracle stores time in a specific storage format, accounting for 7 bytes, regardless of the time displayed by the query. The stored time includes year, month, day, hour, minute and second. The minimum precision is seconds. Time units below seconds are not stored. Therefore, you should pay attention to this when some front-end programs that support millisecond level (such as PB client programs) connect to the Oracle database. The time format displayed during query is determined by the session environment or defined by the user and has nothing to do with the database

2. Oracle time display format

Usually, the client and the database After the connection is established, Oracle will give a default time format data display form, which is related to the character set used. Generally, the year, month and day are displayed instead of hours, minutes and seconds. For example, when using the us7ascii character set (or other English character sets), the default time format is displayed as: 28-Jan-2003, and when using the zhs16gbk character set (or other Chinese character sets), the default time format is displayed as: :2003-January-28. When inserting data into the table, if you do not use a conversion function, the format of the time field must comply with the time format of the session environment, otherwise the insertion cannot be performed. To view the time format of the current session, you can use the following SQL statement:

SQL> select sysdate from dual;
Copy after login

3. Insert time format data into the oracle table

Insert time format into the oracle table The data is a relatively troublesome thing. Either you write your time value strictly in accordance with the time format of the current session, or you use a conversion function to customize the format of the time data. There are two time-related conversion functions: to_char and to_date. The to_char (time value, time format) function converts the time value into a string form, usually used in queries; to_date (string, time format) converts the format string into a time value, usually used in the insert statement

4 How to modify the date format in the session

It is said that there are three ways to modify the date format in the current session. I have only tried the first two methods, modifying glogin.sql I haven't tried the file method, so I don't know if it really works. Explain one by one below.

(1) Modify the date format of the current session in sql*plus

SQL> alter session set nls_date_format = 'yyyy-mm-dd hh24:mi:ss'; Change the date format of the current session The time format is modified to this format: 2003-01-28 15:23:38, that is, four digits of the year - two digits of the month - two digits of the day, a space, and the hours: minutes: seconds of the 24-hour system.

This modification method is only valid for the current session. Note that this is for the current session, not the current sql*plus window. That is, if you

## are modified like this and then use the connect command to connect to the database as another user or to connect to another database, the date format will become invalid and return to the default date format.

(2) Modify the registry (only for windows systems)

Add a string (8i version) to the primary key of hkey_local_machinesoftwareoraclehome0 in the registry. The string name is nls_date_format, and the value of the string is The time format you want to define, such as: yyyy-mm-dd hh24:mi:ss, then restart sql*plus.

This modification method is valid for the sql*plus window, that is, no matter how many sql*plus windows you open, this time format will be the default. Modifying the server-side registry is invalid. Only modifying the client-side registry is effective.

(3) Modify the glogin.sql file in the $oracle_homesqlplusadmin directory

I have not tried this method, so I dare not say it randomly, so as not to mislead everyone. Please give me some advice from experts who know this method.

5 Priority of date format

If in a specific environment, both the registry and the current session are modified using the alter session command, then which One modification method that works? The alter session command is valid and has the highest priority. That is, no matter what the current client environment is like or how the registry has been modified, as long as you use the alter session command to modify the time format, then your modified time format will be allow. Therefore, if you directly use sql*plus interactive query or insert time format data, both methods can be used. If you use SQL files for batch insertion or scheduled execution, it is best to use the alter session command to modify the session before using time data to ensure that the normal operation of this SQL statement has nothing to do with the environment. Similarly, if the front-end client program needs to use data in time format, it is best to modify the session before use, or use the to_char or to_date function for conversion (the use of conversion functions is highly recommended) to ensure that the program runs in accordance with the environment. Nothing to do.

summary:

The oracle database defines many functions for time type data, especially for the calculation of time data, which is very convenient. Of course, the premise of convenience is that you are familiar with Oracle's time functions. Here I sometimes see some people using string type fields when storing time type data. I strongly oppose this approach, whether it is from the perspective of system overhead, calculation of time data, or the extraction of time data. A certain sub-data in this data, such as getting the month in time, getting the week in time, etc., is not as convenient as directly storing it as time type data. As long as you are familiar with Oracle's time functions, I believe everyone will like Oracle time type data. It is very flexible and powerful. You can do almost everything you can think of about time - it depends on your understanding of Oracle time. Function mastery

Recommended tutorial: "Oracle Video Tutorial"

The above is the detailed content of How to query the time format in oracle. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What to do if the oracle can't be opened What to do if the oracle can't be opened Apr 11, 2025 pm 10:06 PM

Solutions to Oracle cannot be opened include: 1. Start the database service; 2. Start the listener; 3. Check port conflicts; 4. Set environment variables correctly; 5. Make sure the firewall or antivirus software does not block the connection; 6. Check whether the server is closed; 7. Use RMAN to recover corrupt files; 8. Check whether the TNS service name is correct; 9. Check network connection; 10. Reinstall Oracle software.

How to solve the problem of closing oracle cursor How to solve the problem of closing oracle cursor Apr 11, 2025 pm 10:18 PM

The method to solve the Oracle cursor closure problem includes: explicitly closing the cursor using the CLOSE statement. Declare the cursor in the FOR UPDATE clause so that it automatically closes after the scope is ended. Declare the cursor in the USING clause so that it automatically closes when the associated PL/SQL variable is closed. Use exception handling to ensure that the cursor is closed in any exception situation. Use the connection pool to automatically close the cursor. Disable automatic submission and delay cursor closing.

How to create cursors in oracle loop How to create cursors in oracle loop Apr 12, 2025 am 06:18 AM

In Oracle, the FOR LOOP loop can create cursors dynamically. The steps are: 1. Define the cursor type; 2. Create the loop; 3. Create the cursor dynamically; 4. Execute the cursor; 5. Close the cursor. Example: A cursor can be created cycle-by-circuit to display the names and salaries of the top 10 employees.

How to paginate oracle database How to paginate oracle database Apr 11, 2025 pm 08:42 PM

Oracle database paging uses ROWNUM pseudo-columns or FETCH statements to implement: ROWNUM pseudo-columns are used to filter results by row numbers and are suitable for complex queries. The FETCH statement is used to get the specified number of first rows and is suitable for simple queries.

How to stop oracle database How to stop oracle database Apr 12, 2025 am 06:12 AM

To stop an Oracle database, perform the following steps: 1. Connect to the database; 2. Shutdown immediately; 3. Shutdown abort completely.

What steps are required to configure CentOS in HDFS What steps are required to configure CentOS in HDFS Apr 14, 2025 pm 06:42 PM

Building a Hadoop Distributed File System (HDFS) on a CentOS system requires multiple steps. This article provides a brief configuration guide. 1. Prepare to install JDK in the early stage: Install JavaDevelopmentKit (JDK) on all nodes, and the version must be compatible with Hadoop. The installation package can be downloaded from the Oracle official website. Environment variable configuration: Edit /etc/profile file, set Java and Hadoop environment variables, so that the system can find the installation path of JDK and Hadoop. 2. Security configuration: SSH password-free login to generate SSH key: Use the ssh-keygen command on each node

How to create oracle dynamic sql How to create oracle dynamic sql Apr 12, 2025 am 06:06 AM

SQL statements can be created and executed based on runtime input by using Oracle's dynamic SQL. The steps include: preparing an empty string variable to store dynamically generated SQL statements. Use the EXECUTE IMMEDIATE or PREPARE statement to compile and execute dynamic SQL statements. Use bind variable to pass user input or other dynamic values ​​to dynamic SQL. Use EXECUTE IMMEDIATE or EXECUTE to execute dynamic SQL statements.

How to solve garbled code in oracle How to solve garbled code in oracle Apr 11, 2025 pm 10:09 PM

Oracle garbled problems can be solved by checking the database character set to ensure they match the data. Set the client character set to match the database. Convert data or modify column character sets to match database character sets. Use Unicode character sets and avoid multibyte character sets. Check that the language settings of the database and client are correct.

See all articles