Go Back   CodeCall Programming Forum > Software Development > Tutorials
Register Blogs Search Today's Posts Mark Forums Read

Tutorials Programming Tutorials - Post your tutorials here!

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-01-2009, 01:49 PM
chili5's Avatar
Code Slinger
 
Join Date: Mar 2008
Posts: 7,018
chili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond repute
SQL Date

SQL Date

SQL provides a lot of functions for manipulating dates in a database. You can store values in a date field or a datetime field. If you store the dates as unix_timestamps you can get a lot more flexibility with dates. In fact, this is usually what is done when you are using PHP with the database.

It might even make sense to store them as timestamps anyways. This is because I always convert them to timestamps before I use them.


Creating the Database

We are going to create a test database with a people table. The people table will contain these fields:
  • id
  • name
  • date

First, the code to create the database:

Code:
CREATE DATABASE `test` ;
Now, we need to create the table.

Code:
CREATE TABLE `test`.`people` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` VARCHAR( 100 ) NOT NULL ,
`date` DATE NOT NULL
) ENGINE = MYISAM ;
This table contains 3 fields just like we said it would. Now let us add some data. The format for the date needs to be yyyy-mm-dd.

The date could be when they registered to a web site or when they first made an order, or whatever.

Populating the table

Code:
INSERT INTO `people` (`id`, `name`, `date`) VALUES
(1, 'James', '2009-09-16'),
(2, 'Joe', '2009-09-30'),
(3, 'Jack', '2009-09-18');
We are going to insert 3 rows of data.


UNIX_TIMESTAMP function

Like I said earlier, it is easier to work with dates as numbers. This number is the unix timestamp which is the number of milliseconds that have past since January 1st, 1900.

To see how this works let us select all the UNIX_TIMESTAMPS from the database.

Code:
SELECT UNIX_TIMESTAMP(date) AS theDate FROM people;
The resulting output is:



These values are the unix timestamps representing the dates that we inserted above. We can use these values to compare dates. This allows us to select rows that occur between the two days.

A date occurs between two dates if the unix_timestamp is >= the test date and <= the test date.

BETWEEN clause


We use the BETWEEN clause to test if a date occurs between two dates.

Let us return all results that occur between '2009-09-16' and '2009-09-18'.


The SQL query is simply:

Code:
SELECT * FROM people WHERE UNIX_TIMESTAMP(date) BETWEEN UNIX_TIMESTAMP('2009-09-16') AND UNIX_TIMESTAMP('2009-09-18')
The result is:



I convert the date field that I am comparing to a unix_timestamp and I also convert the dates that I am testing it to a unix timestamp. This makes comparing easy.
Attached Thumbnails
sql-date-between.jpg  
Attached Images
 
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 09-02-2009, 08:01 AM
Jordan's Avatar
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Posts: 24,556
Jordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to all
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan Send a message via Yahoo to Jordan
Re: SQL Date

SQL makes using dates easy. My favorite being the Unix Timestamp. +rep!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 09-02-2009, 08:03 AM
chili5's Avatar
Code Slinger
 
Join Date: Mar 2008
Posts: 7,018
chili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond repute
Re: SQL Date

I like the UNIX_TIMESTAMP function also. You showed me that. You have no idea how useful it was.

I am very thankful that these functions are so easy. My last project used SQL dates a lot. That function made it so much easier for me.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 09-02-2009, 05:09 PM
WingedPanther's Avatar
Super Moderator
 
Join Date: Jul 2006
Age: 36
Posts: 11,435
WingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud of
Re: SQL Date

Oracle + dates = headache.
MySQL + dates = nice
+rep
__________________
CodeCall Blog | CodeCall Wiki | Shareware
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 09-02-2009, 05:11 PM
chili5's Avatar
Code Slinger
 
Join Date: Mar 2008
Posts: 7,018
chili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond repute
Re: SQL Date

I don't know anything about oracle. Why do you say that?

I probably should mention that I'm using MySQL for all my tutorials.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 09-02-2009, 05:32 PM
WingedPanther's Avatar
Super Moderator
 
Join Date: Jul 2006
Age: 36
Posts: 11,435
WingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud of
Re: SQL Date

Oracle has a default date/time string format. If you don't pass it a properly formatted string, it rejects it. You can alter it for your session, or use the to_date function, but if your app is out of sync with the db, you can have major headaches.
__________________
CodeCall Blog | CodeCall Wiki | Shareware
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 09-02-2009, 06:21 PM
BlaineSch's Avatar
Code Warrior
 
Join Date: Apr 2009
Location: Trapped in my own little world.
Age: 19
Posts: 2,169
BlaineSch is a glorious beacon of lightBlaineSch is a glorious beacon of lightBlaineSch is a glorious beacon of lightBlaineSch is a glorious beacon of lightBlaineSch is a glorious beacon of lightBlaineSch is a glorious beacon of light
Send a message via MSN to BlaineSch
Re: SQL Date

I have a book on Oracle, its on my todo list, currently reading database systems (A practical approach to design, implementation, and management).

Databases are always fun to play with. I would rep you but I have no rep for you lol.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calendar Script xXHalfSliceXx JavaScript and CSS 7 05-25-2009 01:26 PM
Tutorial: Advanced SQL Jordan Tutorials 2 04-07-2008 04:18 PM
Date Filter Being Odd, ms access 03, Any Advise? Xantara Visual Basic Programming 1 02-08-2008 08:49 AM
Date comparison with C# and SQL Server 2005 hoser2001 C# Programming 5 08-07-2007 03:49 PM
PHP:Tutorial The Date John PHP Tutorials 0 01-10-2007 07:10 PM


All times are GMT -5. The time now is 06:54 AM.


vBulletin v3.8.0 ©2010, Jelsoft Enterprises Ltd.


no new posts

LinkBacks Enabled by vBSEO 3.1.0