Date can be pretty dramatic

I always have to refer back when every time I need to query dates.

Here are some useful SQL statements I normally have it with me.

Remember ColdFusion has current date as now() and Oracle has the wonderful build-in function sysdate.

Simple command like sysdate+1 will add one day to the current date and sysdate-10 will minus 10 days from the current date.

Format date on select (output): TO_CHAR(date, ‘format‘)
Format on input: TO_DATE(string, ‘format‘)

MM Numeric month (e.g. 07)
MON Abbreviated month name (e.g. JUL)
MONTH Full month name (e.g. JULY)
DD Day of month (e.g. 14)
DY Abbreviated name of day (e.g. FRI)
YYYY 4-digit year (e.g. 1999)
YY Last 2 digits of the year (e.g. 99)
RR Like YY, but the two digits are “rounded” to a year in the range 1950 to 2049. Thus, 06 is considered 2006 instead of 1906
AM (or PM) Meridian indicator
HH Hour of day (1-12)
HH24 Hour of day (0-23)
MI Minute (0-59)
SS Second (0-59)


Try:

1
2
3
 SELECT TO_CHAR(sysdate+100, 'DD-Mon-YYYY HH24:MI:SS Dy') AS "Current Time" 
 FROM dual
 /

And now the tricky part is to compare two dates. Standard comparison operators does work; such as =, !=, > and <.


To get a date count between two dates – use round function.
For this example, I am querying all ex boyfriends in the past 365 days.

1
2
3
4
 SELECT firstname, lastname
 FROM boyfriendList 
 WHERE ROUND(sysdate-breakupDate) < 365
 /



To validate a date exist between two dates – use between function.
For this example, I am getting all ex boyfriends that has the breakupDate exists between two dates.
Note: in ColdFusion, the variable startDate and endDate is wrapped around the pound sign.

1
2
3
4
5
 SELECT firstname, lastname
 FROM boyfriendList
 WHERE TRUNC(breakupDate) BETWEEN 
    TO_DATE('#startDate#','mm/dd/yyyy') AND TO_DATE('#endDate#','mm/dd/yyyy')
 /

OR

1
2
3
4
5
 SELECT firstname, lastname
 FROM boyfriendList
 WHERE TRUNC (breakupDate) BETWEEN 
    TO_DATE(sysdate-31) AND TO_DATE(sysdate-1)
 /



Now, what is the difference between TRUNC and ROUND?

ROUND will always round the number to the closest integer.


For example:

1
2
3
4
5
 SELECT 
    ROUND(33.99) AS round_number, 
    TRUNC(33.99) AS trunc_number
 FROM DUAL
 /

will return result of 34 for round_number and 33 for trunc_number.

Print Friendly
Share on TwitterShare via email

It is a reserved word, duh!

Created a table with this column name: CURRENT_DATE.

I never thought of an underscore within words could be just another reserved word.

Decided to look up for a listing of these words and found them here:
- Oracle
- SQL


The problem I came across is not an error when creating the table, but is when I ran my select statement.

1
2
 SELECT current_date FROM table_name
 /



And it always returns the current date! I am not sure if there is a good way around it, but I did rename the column; else my query result does look really messy and totally wrong.


Other reserved words such as “to”, “from”, “like”, “and”, “all”; could be bypass using the double quote around it.

1
2
 SELECT "like", "and", "all" FROM table_name
 /



But somehow for the CURRENT_DATE, it cannot be done. Puzzled and I am still wondering if there is any better way? Create a view perhaps?

I think prevention is always the best. Watch out on how you name your column!!!

Print Friendly
Share on TwitterShare via email

Capitalize first character

A very simple way.

1
2
 <cfset rc.firstname = ucase(left(rc.firstname,1)) & lcase(right(rc.firstname,len(rc.firstname)-1))/>
 <cfset rc.lastname = ucase(left(rc.lastname,1)) & lcase(right(rc.lastname,len(rc.lastname)-1))/>

Or here is a function you could use: http://www.cflib.org/udf/CapFirst

Print Friendly
Share on TwitterShare via email

Unfriend me, maxrows

So, what if I only need to read just 10 records from a table that contains 10 million records?


I can use maxrows:

1
2
3
4
 <cfquery name="getTen" dataSource="#request.DSN#" maxrows="10">
    SELECT firstname, lastname
    FROM boyfriendList
 </cfquery>



but as far as I know, it will read all of that 1 million records and then return 10 – which slows down the overall performance.


Instead, use ROWNUM (for Oracle) and this will just read 10 records!

1
2
3
4
5
 <cfquery name="getTen" dataSource="#request.DSN#">
    SELECT firstname, lastname 
    FROM boyfriendList
    WHERE ROWNUM <= 10
 </cfquery>


Print Friendly
Share on TwitterShare via email

Intro

I decided to write, just because the knowledge I received daily from work and coworkers amuses me that I felt the need to keep it written somewhere.

Since last November, on my new position as a contractor for National Science Foundation, I was introduced to Framework One and Object Oriented Programming with heavy usage on CSS, jQuery, AJAX, JSON, and CFCs. That was the best job I ever had. Quite stressful because there is so much to learn, but at the same time, it pushed me to the next level of becoming a better developer.

Next week, I will start my new project with Department of Labor. Excited and I know they are using ColdBox – which is very new to me, and I can’t wait to get my hands on it and will try to write down what I learned onto this blog.

Look for more to come… ciao!

Print Friendly
Share on TwitterShare via email
Subscribe to RSS Feed Follow me on Twitter!