加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

Date / Time related queries

发布时间:2020-12-11 23:55:29 所属栏目:MySql教程 来源:网络整理
导读: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 databas
</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.

    Related Articles

    (编辑:李大同)

    【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    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.

    2. 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.

    3. 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.

    4. Get the last day of the year

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

    5. 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.

    6. Get number of days left in current month

      Below query calculates number of days left in current month.

    7. 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.

    8. 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.

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

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

      Data dictionary queries
    11. 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).

    12. 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.

    13. 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.

    14. Getting current schema

      Yet another query to get current schema name.

    15. 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
    16. Database version information

      Returns the Oracle database version.

    17. Database default information

      Some system default information.

    18. Database Character Set information

      Display the character set information of database.

    19. Get Oracle version

    20. 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.

    21. Resizing Tablespace without adding datafile

      Yet another DDL query to resize table space.

    22. Checking autoextend on/off for Tablespaces

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

    23. Adding datafile to a tablespace

      Query to add datafile in a tablespace.

    24. Increasing datafile size

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

    25. Find the Actual size of a Database

      Gives the actual database size in GB.

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

      Gives the size occupied by data in this database.

    27. Find the size of the SCHEMA/USER

      Give the size of user in MBs.

    28. 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
    29. CPU usage of the USER

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

      ????
    30. Long Query progress in database

      Show the progress of long running queries.

      ???? 0
    31. 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.

    32. V$SESSION.SID AND V$SESSION.SERIAL# is database process id
    33. V$PROCESS.SPID is shadow process id on this database server
    34. 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.
    35. Last SQL Fired from particular Schema or Table:

      ????
    36. Find Top 10 SQL by reads per execution

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

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

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

    40. Get number of objects per owner

      Utility / Math related queries
    41. Convert number to words

      More info:

      Output:

    42. 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.

    43. 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.

    44. 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.

    45. 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>