Posts

Showing posts from January, 2014

45 Useful Oracle Queries

Image
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 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. SELECT TRUNC (SYSDATE, 'MONTH') "First day of current month" FROM DUAL; 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. SELECT TRUNC (LAST_DAY (SYSDATE)) "Last day of current month" FROM DUAL; Get th

Generating Random Data in Oracle

Image
Most of the times, production data is not available in development environments. Here, I would like to share a single sql command which can generate random data. But before that, let’s address another issue faced by a lot of new oracle users. We need to generate a sequence of numbers using a sql statement. This will generate a number sequence. Nothing fancy, simplest of sql known :) Here it is: SELECT ROWNUM FROM DUAL CONNECT BY LEVEL < 10000; Now, to generate the random data, the oracle build in package “dbms_random” comes handy. I am referring to the version 10GR2, there might be additional features available on 11G . Other build in functions can also be used. Here is a sample sql: SELECT      LEVEL                                                        empl_id,             MOD (ROWNUM, 50000)                                         dept_id,             TRUNC (DBMS_RANDOM.VALUE (1000, 500000), 2)                 salary,             DE

Row Data Multiplication in Oracle

Image
Aggregate functions return a single result row based on a group of rows. This differentiates them from Single-Row functions which act on each row. These functions are extensively used with the GROUP BY clause in SQL statements. AVG (), COUNT (), SUM () … are few aggregate functions which are quite commonly used. Today, one of my colleague asked me if there is some aggregation function for Multiplication . I thought about it for a while and found myself surprised that I have never thought about doing such a thing :) So, How do we do the multiplication then? I tried it but just couldn’t do it in SQL. So, I asked this question on our internal self help channel and I got a pretty impressive reply: “Using a mathematical approach…” After understanding the solution, I was surprisingly happy with the simplicity of the approach and found it worth sharing. Let’s assume that we have a table “tbl” with one column “num”. This table has three rows having values 2, 3 & 4 f

How To Convert Number into Words using Oracle SQL Query

Image
How can you convert a number into words using Oracle Sql Query? What I mean by Number to Word is: 12 = Twelve 102 = One Hundred Two 1020 = One Thousand Twenty Here’s a classy query which will convert number into words.Please see the query below: select to_char(to_date(:number, 'j' ), 'jsp' ) from dual; If I pass 234 in number, then the output will : two hundred thirty-four SELECT TO_CHAR (TO_DATE (234, 'j' ), 'jsp' ) FROM DUAL; // Output : two hundred thirty-four SELECT TO_CHAR (TO_DATE (24834, 'j' ), 'jsp' ) FROM DUAL; // Output : twenty-four thousand eight hundred thirty-four SELECT TO_CHAR (TO_DATE (2447834, 'j' ), 'jsp' ) FROM DUAL; // Output : two million four hundred forty-seven thousand eight hundred thirty-four So how the query works? Well here’s why: If you look into the inner most part of the query to_date(:number,'j') the ‘j’ or J

Generate tag cloud using Wordle

Image
Browsing through internet, I came to this wonderful tool called Wordle which generates a tag cloud with different orientation and effects. It takes input as plain text or your blogs/websites rss/atom feed or your sites URL and generate a cool tag cloud of the words that it encounters on your site. You can change the font and effects on this tag cloud. Also you can remove unwanted words from it. I tried several URLs and following are the Wordle generated by this site. SLASHDOT.COM                TECHCRUNCH.COM                 TECHNORATI.COM                 NEWS.GOOGLE.COM              

Java Locale List

Image
Here is a complete list of Locales in Java. This list is compiled using the mighty java.text.SimpleDateFormat class. The class SimpleDateFormat provides a method getAvailableLocales() which gives array of java.util.Locale objects. This is java locale list. We can iterate over this list and get all relevant information about these locales. By the way, the above tag cloud image is generated using all java locales. If you interested here’s how to generate your own tag cloud: Generate Tag Cloud Getting Locales List in Java Following java program will print the list of all locales. import java.text.SimpleDateFormat; import java.util.Locale; public class Java_Locale_List { public static void main(String[] args) { //returns array of all locales Locale locales[] = SimpleDateFormat. getAvailableLocales(); //iterate through each locale and print // locale code, display name and country for (int i = 0; i < locales.length; i++) {

jQuery window height is not correct

Image
I was working on a small jQuery snippet and got this weird issue. I was trying to get window height using jquery’s $(window).height() function.  Ideally $(window).height() returns the pixel less height of browser window. This is always the height of current browser window. If you resize browser this value should change. Also you can get $(document).height() . $(document).height() returns an unit-less pixel value of the height of the document being rendered. If the actual document’s body height is less than the viewport height then it will return the viewport height instead. i.e. if you have less content in a page and window is sufficiently open to show this content then document height will be less than jquery window height. Problem However recently when I was playing with these values it seems both $(window).height() and $(document).height() gave me same value! Thus the $(window).height() was giving incorrect result. Check out below source: <

Java 8 Default Methods Tutorial

Image
Interfaces in Java always contained method declaration not their definitions (method body). There was no way of defining method body / definition in interfaces. This is because historically Java didn’t allow multiple inheritance of classes. It allowed multiple inheritance of interfaces as interface were nothing but method declaration. This solves the problem of ambiguity in multiple inheritance. Since Java 8 it is now possible to add method bodies in interfaces. Java 8 has a new feature called Default Methods . It is now possible to add method bodies into interfaces!   public interface Math { int add(int a, int b); default int multiply(int a, int b) { return a * b; } }   In above Math interface we added a method multiply with actual method body. Why we need Default Methods? Why would one want to add methods into Interfaces? We’ll it is because interfaces are too tightly coupled with their implementation classes. i.e. it is not possible to add a met

Top 10 Best Android Apps For Blogger

Image
Blogger is a platform where you can expressing yourself and your thoughts and what you know on the internet. Sometimes its difficult for you to post regularly on your blog when you are away from your computer or laptop. But know you can post daily or expressing your thoughts whenever you want with help of android apps for blogger. Now a days android market become a most popular with their newly apps. Android is a operating system for mobiles. It is a Linux based operating system.With the help of these applications you can post directly from your mobile whenever you want. So below i can introduce you some best application for blogger.   1. WordPress WordPress is a most popular blogging system on the web. If you have a WordPress blog you can editing, publishing and checking your analytic data through the installation of android app. If you are busy and out of home or away from your PC this app allows you to post whenever you want from your mobile phone by which