Formatting dates in a SQL query

I know this information is found in thousands of other places on the Internet, but I decided to post a simple way to format dates in SQL here since I can never seem to remember.  And now I can just look at my own blog instead of searching the web.

To format the dates returned from a query:

select TO_CHAR(birth_date, 'YYYY/MM/DD'), TO_CHAR(hire_date, 'YYYY/MM/DD')
 from EMPLOYEE;

To format dates in a WHERE clause:

select * from EMPLOYEE
 where hire_date >  TO_DATE('2010-12-18','YYYY-MM-DD');

Hopefully by writing it down, now I will remember.

Advertisements

About stevewall123

I am a Lead Software Engineer in Minneapolis working for Thomson Reuters. I am currently working on projects using Java, JavaScript, Spring, Drools, Hazelcast, Liquibase and Tomcat. Previously, I used C#, GWT, Grails, Groovy, JMS and JBoss Drools Guvnor. In the past I have worked on projects using J2EE, Swing, Webwork, Hibernate, Spring, Spring-WS, JMS, JUnit and Ant.
This entry was posted in Miscellaneous and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s