If you ever need to find the next weekday from a given date in Oracle it turns out they have a built in function for doing just that. If you want the next Sunday from yesterday you would do:
SELECT NEXT_DAY(SYSDATE – 1, 'SUN') FROM dual;
Valid entries for the day are: SUN, MON, TUE, WED, THU, FRI, and SAT
Tags: Oracle, SQL
I pulled a large amount of SQL out of some existing code and wanted to have it formated nicely for me. I figured there had to be some type of pretty printer for SQL available outside of applications like TOAD. I have access to TOAD but it would have required a reboot so I figured I would ask google what to do. There seem to be a number of different formaters out there. I found a couple people talking about current options a list here and here. The following list is a summary of the options I found:
I decided that since SQLInform was mentioned a lot I would give it a try. I was suprised at how well it did. I gave it a huge amount of SQL to re-format and it gave it back to me formated. There is a limit on how much you can ask it to format at once but that shouldn't be a big issue. I didn't see that it choked on anything that I gave it and there were all kinds of nasty things going on in the SQL.
One interesting thing to come from this is that it seems there are no open source or truely free unix based formaters out there. It seems like it would be easy enough to get going since the SQL grammar is available for a number of different parser generators.
Tags: SQL, TOAD, SQLInform
A few days ago I made use of a couple Oracle built in functions and it made me happy I didn't have to write a stored proc or some type of mini-app to do it. I needed to parse a timestamp out of a field that was put there by a java program. The timestamp was just the output of System.currentTimeInMillis() and was concatenated onto some other information.
It took a little digging to find out how to convert a epoch style timestamp but here it is:
SELECT new_time( to_date('01011970', 'ddmmyyyy') + 1/24/60/60 * :currenttimeinmillis/1000, 'GMT', 'EDT' ) FROM dual
Note that I convert the output from GMT to EDT here.
Tags: java, oracle
Today I needed to find a way to count the number of unique email domains in a table. I figured there was a way of getting the index of a string in another string and sure enough there is. This did the trick in Oracle:
SELECT count(1), SUBSTR(email, INSTR(email, '@', 1, 1)+1) FROM SOMETABLE GROUP BY SUBSTR(email, INSTR(email, '@', 1, 1)+1) ORDER BY count(1) DESC
The INSTR function gives you the location in a string where another string is located. See the following link for more on the INSTR function: http://www.techonthenet.com/oracle/functions/instr.php
I've always found the way Oracle handles case interesting. It looks like they are changing things a little starting with 10G: http://blogs.ittoolbox.com/database/solutions/archives/005951.asp