Date / Time related queries

http://viralpatel.net/blogs/useful-oracle-queries/

Here’s a list of 40+ Useful Oracle queries that every Oracle developer must bookmark. These queries range from date manipulation,getting server info,get execution status,calculate database size etc.

Date / Time related queries

  1. Get the first day of the month

    Quickly returns the first day of current month. Instead of current month you want to find first day of month where a date falls,replace SYSDATE with any date column/value.

  • Get the last day of the month

    This query is similar to above but returns last day of current month. One thing worth noting is that it automatically takes care of leap year. So if you have 29 days in Feb,it will return 29/2. Also similar to above query replace SYSDATE with any other date column/value to find last day of that particular month.

  • Get the first day of the Year

    First day of year is always 1-Jan. This query can be use in stored procedure where you quickly want first day of year for some calculation.

  • Get the last day of the year

    Similar to above query. Instead of first day this query returns last day of current year.

  • Get number of days in current month

    Now this is useful. This query returns number of days in current month. You can change SYSDATE with any date/value to know number of days in that month.

  • Get number of days left in current month

    Below query calculates number of days left in current month.

  • Get number of days between two dates

    Use this query to get difference between two dates in number of days.

    Use second query if you need to find number of days since some specific date. In this example number of days since any employee is hired.

  • Display each months start and end date upto last month of the year

    This clever query displays start date and end date of each month in current year. You might want to use this for certain types of calculations.

  • Get number of seconds passed since today (since 00:00 hr)

  • Get number of seconds left today (till 23:59:59 hr)

    Data dictionary queries

  • Check if a table exists in the current database schema

    A simple query that can be used to check if a table exists before you create it. This way you can make your create table script rerunnable. Just replace table_name with actual table you want to check. This query will check if table exists for current user (from where the query is executed).

  • Check if a column exists in a table

    Simple query to check if a particular column exists in table. Useful when you tries to add new column in table using ALTER TABLE statement,you might wanna check if column already exists before adding one.

  • Showing the table structure

    This query gives you the DDL statement for any table. Notice we have pass ‘TABLE’ as first parameter. This query can be generalized to get DDL statement of any database object. For example to get DDL for a view just replace first argument with ‘VIEW’ and second with your view name and so.

  • Getting current schema

    Yet another query to get current schema name.

  • Changing current schema

    Yet another query to change the current schema. Useful when your script is expected to run under certain user but is actually executed by other user. It is always safe to set the current user to what your script expects.

    Database administration queries

  • Database version information

    Returns the Oracle database version.

  • Database default information

    Some system default information.

  • Database Character Set information

    Display the character set information of database.

  • Get Oracle version

  • Store data case sensitive but to index it case insensitive

    Now this ones tricky. Sometime you might querying database on some value independent of case. In your query you might do UPPER(..) = UPPER(..) on both sides to make it case insensitive. Now in such cases,you might want to make your index case insensitive so that they don’t occupy more space. Feel free to experiment with this one.

  • Resizing Tablespace without adding datafile

    Yet another DDL query to resize table space.

  • Checking autoextend on/off for Tablespaces

    Query to check if autoextend is on or off for a given tablespace.

  • Adding datafile to a tablespace

    Query to add datafile in a tablespace.

  • Increasing datafile size

    Yet another query to increase the datafile size of a given datafile.

  • Find the Actual size of a Database

    Gives the actual database size in GB.

  • Find the size occupied by Data in a Database or Database usage details

    Gives the size occupied by data in this database.

  • Find the size of the SCHEMA/USER

    Give the size of user in MBs.

  • Last SQL fired by the User on Database

    This query will display last SQL query fired by each user in this database. Notice how this query display last SQL per each session.

        

    Performance related queries

  • CPU usage of the USER

    Displays CPU usage for each User. Useful to understand database load by user.

        
  • Long Query progress in database

    Show the progress of long running queries.

        
    0
  • Get current session id,process id,client process id?

    This is for those who wants to do some voodoo magic using process ids and session ids.

    • V$SESSION.SID AND V$SESSION.SERIAL# is database process id
    • V$PROCESS.SPID is shadow process id on this database server
    • V$SESSION.PROCESS is client PROCESS ID,ON windows it IS : separated THE FIRST # IS THE PROCESS ID ON THE client AND 2nd one IS THE THREAD id.
  • Last SQL Fired from particular Schema or Table:

        
  • Find Top 10 SQL by reads per execution

  • Oracle SQL query over the view that shows actual Oracle connections.

  • Oracle SQL query that show the opened connections group by the program that opens the connection.

  • Oracle SQL query that shows Oracle users connected and the sessions number for user

  • Get number of objects per owner

    Utility / Math related queries

  • Convert number to words

    More info:

    Output:

  • Find string in package source code

    Below query will search for string ‘FOO_SOMETHING’ in all package source. This query comes handy when you want to find a particular procedure or function call from all the source code.

  • Convert Comma Separated Values into Table

    The query can come quite handy when you have comma separated data string that you need to convert into table so that you can use other SQL queries like IN or NOT IN. Here we are converting ‘AA,BB,CC,DD,EE,FF’ string to table containing AA,CC etc. as each row. Once you have this table you can join it with other table to quickly do some useful stuffs.

  • Find the last record from a table

    This ones straight forward. Use this when your table does not have primary key or you cannot be sure if record having max primary key is the latest one.

  • Row Data Multiplication in Oracle

    This query use some tricky math functions to multiply values from each row. Read below article for more details. More info:

    </td>

    </tr></table>

  • Generating Random Data In Oracle

    You might want to generate some random data to quickly insert in table for testing. Below query help you do that. Read this article for more details. More info:

    </td>

    </tr></table>

  • Random number generator in Oracle

    Plain old random number generator in Oracle. This ones generate a random number between 0 and 100. Change the multiplier to number that you want to set limit for.

    </td>

    </tr></table>

  • Check if table contains any data

    This one can be written in multiple ways. You can create count(*) on a table to know number of rows. But this query is more efficient given the fact that we are only interested in knowing if table has any data.

    </td>

    </tr></table>

  • If you have some cool query that can make life of other Oracle developers easy,do share in comment section.

    版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

    相关推荐


    在正式开始之前,我们先来看下 MySQL 服务器的配置和版本号信息,如下图所示: “兵马未动粮草先行”,看完了相关的配置之后,我们先来创建一张测试表和一些测试数据。 -- 如果存在 person 表先删除 DROP TABLE IF EXISTS person; -- 创建 person 表,其中
    &gt; [合辑地址:MySQL全面瓦解](https://www.cnblogs.com/wzh2010/category/1859594.html &quot;合辑地址:MySQL全面瓦解&quot;) # 1 为什么需要数据库备份 - 灾难恢复:当发生数据灾难的时候,需要对损坏的数据进行恢复和
    物理服务机的CPU、内存、存储设备、连接数等资源有限,某个时段大量连接同时执行操作,会导致数据库在处理上遇到性能瓶颈。为了解决这个问题,行业先驱门充分发扬了分而治之的思想,对大库表进行分割,&#xA;然后实施更好的控制和管理,同时使用多台机器的CPU、内存、存储,提供更好的性能。而分治有两种实现方式:垂直拆
    1 回顾 上一节我们详细讲解了如何对数据库进行分区操作,包括了 垂直拆分(Scale Up 纵向扩展)和&#160;水平拆分(Scale Out 横向扩展) ,同时简要整理了水平分区的几种策略,现在来回顾一下。 2 水平分区的5种策略 2.1 Hash(哈希) 这种策略是通过对表的一个或多个列的Ha
    navicat查看某个表的所有字段的详细信息 navicat设计表只能一次查看一个字段的备注信息,那怎么才能做到一次性查询表的信息呢?SELECT COLUMN_NAME,COLUMN_COMMENT,COLUMN_TYPE,COLUMN_KEY FROM information_schema.CO
    文章浏览阅读4.3k次。转载请把头部出处链接和尾部二维码一起转载,本文出自逆流的鱼yuiop:http://blog.csdn.net/hejjunlin/article/details/52768613前言:数据库每天的数据不断增多,自动删除机制总体风险太大,想保留更多历史性的数据供查询,于是从小的hbase换到大的hbase上,势在必行。今天记录下这次数据仓库迁移。看下Agenda:彻底卸载MySQL安装MySQL_linux服务器进行数据迁移
    文章浏览阅读488次。恢复步骤概要备份frm、ibd文件如果mysql版本发生变化,安装回原本的mysql版本创建和原本库名一致新库,字符集都要保持一样通过frm获取到原先的表结构,通过的得到的表结构创建一个和原先结构一样的空表。使用“ALTER TABLE DISCARD TABLESPACE;”命令卸载掉表空间将原先的ibd拷贝到mysql的仓库下添加用户权限 “chown . .ibd”,如果是操作和mysql的使用权限一致可以跳过通过“ALTER TABLE IMPORT TABLESPACE;”命令恢_alter table discard tablespace
    文章浏览阅读225次。当MySQL单表记录数过大时,增删改查性能都会急剧下降,可以参考以下步骤来优化:单表优化除非单表数据未来会一直不断上涨,否则不要一开始就考虑拆分,拆分会带来逻辑、部署、运维的各种复杂度,一般以整型值为主的表在千万级以下,字符串为主的表在五百万以下是没有太大问题的。而事实上很多时候MySQL单表的性能依然有不少优化空间,甚至能正常支撑千万级以上的数据量:字段尽量使用TINYINT、SMALLINT、MEDIUM_INT作为整数类型而非INT,如果非负则加上UNSIGNEDVARCHAR的长度只分配_开发项目 浏览记录表 过大怎么办
    文章浏览阅读1.5k次。Mysql创建、删除用户MySql中添加用户,新建数据库,用户授权,删除用户,修改密码(注意每行后边都跟个;表示一个命令语句结束):1.新建用户登录MYSQL:@>mysql -u root -p@>密码创建用户:mysql> insert into mysql.user(Host,User,Password) values("localhost_删除mysql用户组
    MySQL是一种开源的关系型数据库管理系统,被广泛应用于各类应用程序的开发中。对于MySQL中的字段,我们需要进行数据类型以及默认值的设置,这对于数据的存储和使用至关重要。其中,有一个非常重要的概念就是MySQL字段默认字符串。 CREATE TABLE `my_...
    MySQL是一个流行的开源关系型数据库管理系统,广泛应用于Web应用程序开发、数据存储和管理。在使用MySQL时,正确设置字符集非常重要,以确保数据的正确性和可靠性。 在MySQL中,字符集表示为一系列字符和字母的集合。MySQL支持多种字符集,包括ASCII、UTF...
    MySQL存储函数 n以内偶数 MySQL存储函数能够帮助用户简化操作,提高效率,常常被用于计算和处理数据。下面我们就来了解一下如何使用MySQL存储函数计算n以内的偶数。 定义存储函数 首先,我们需要定义一个MySQL存储函数,以计算n以内的偶数。下...
    MySQL是一个流行的关系型数据库管理系统,基于客户机-服务器模式,可在各种操作系统上运行。 MySQL支持多种字符集,不同的字符集包括不同的字符,如字母、数字、符号等,并提供不同的排序规则,以满足不同语言环境的需求。 //查看MySQL支持的字符集与校对规...
    在MySQL数据库中,我们有时需要对特定的字符串进行截取并进行分组统计。这种操作对于数据分析和报表制作有着重要的应用。下面我们将讲解一些基本的字符串截取和分组统计的方法。 首先,我们可以使用substring函数对字段中的字符串进行截取。假设我们有一张表stude...
    MySQL提供了多种字符串的查找函数。下面我们就一一介绍。 1. LIKE函数 SELECT * FROM mytable WHERE mycolumn LIKE 'apple%'; 其中"apple%"表示以apple开头的字符串,%表示任意多个字符...
    MySQL 是一种关系型数据库管理系统,广泛应用于各种不同规模和类型的应用程序中。在 MySQL 中,处理字符串数据是很常见的任务。有时候,我们需要在字符串的开头添加一定数量的 0 ,以达到一定的位数。比如,我们可能需要将一个数字转换为 4 位或 5 位的字符串,不足的...
    MySQL是一种流行的关系型数据库管理系统,支持多种数据类型。以下是MySQL所支持的数据类型: 1. 数值型数据类型: - TINYINT 保存-128到127范围内的整数 - SMALLINT 保存-32768到32767范围内的整数 - MEDIU...
    MySQL中存储Emoji表情字段类型 在现代互联网生态中,表情符号已经成为人们展示情感和思想的重要方式之一,因此将表情符号存储到数据库中是一个经常出现的问题。MySQL作为最流行的开源关系型数据库管理系统之一,也需要能够存储和管理这些表情符号的字段类型。 UT...
    MySQL是一种关系型数据库管理系统。在MySQL数据库中,有多种不同的数据类型。而其中,最常见的数据类型之一就是字符串类型。在MySQL中,字符串类型的数据通常会被存储为TEXT或VARCHAR类型。 首先,让我们来看一下VARCHAR类型。VARCHAR是My...
    MySQL字符串取整知识详解 MySQL是一种开源的关系型数据库管理系统,广泛应用于各个领域。在使用MySQL过程当中,我们经常需要对数据进行取整操作。本文将介绍如何使用MySQL字符串取整来处理数据取整问题。 什么是MySQL字符串取整? MySQL...