Jump to content

select date


techker

Recommended Posts

hey guy in my databse the date is as :04/06/2009

 

i have a querry that counts the number of installed units done

 

SELECT COUNT( `units` ) AS TotalNumber, SUM( `units` ) AS Totalunits FROM `fc`"

 

now i need to calcule the number of units done in a year and by months?

 

i tryed this

SELECT COUNT( `units` ) AS TotalNumber, SUM( `units` ) AS Totalunits
FROM `fc` 
WHERE 'date_issued' >01 /01 /2009(afther this date) gives me NULL

Link to comment
Share on other sites

Why do you think MySQL has a DATE, DATETIME and TIMESTAMP field types? So that people would use them and didn't run into problems like yours.

Storing a date as a string is probably the worst thing you can do to yourself. If you can switch to DATE, do it quickly. If not, you're going to do a lot of tedious string manipulation.

Link to comment
Share on other sites

CREATE TABLE `fc` (

`id` int(4) NOT NULL auto_increment,

`company` varchar(200) NOT NULL,

`contact` varchar(20) NOT NULL,

`number` varchar(20) NOT NULL,

`units` varchar(20) NOT NULL,

`PTO` varchar(20) NOT NULL,

`store` varchar(20) NOT NULL,

`date_issued` varchar(20) NOT NULL,

`date_done` varchar(20) NOT NULL,

`times` varchar(20) NOT NULL,

`custom_rate` varchar(20) NOT NULL,

`notes` varchar(200) NOT NULL,

`travel_times` varchar(20) NOT NULL,

`travel_charge` varchar(20) NOT NULL,

`paid` varchar(30) NOT NULL,

`month` varchar(25) NOT NULL,

`year` varchar(20) NOT NULL,

`client_id` varchar(20) NOT NULL,

`service_call` varchar(100) NOT NULL,

`done` varchar(50) NOT NULL,

PRIMARY KEY  (`id`)

) ENGINE=MyISAM AUTO_INCREMENT=164 DEFAULT CHARSET=latin1

Link to comment
Share on other sites

`date_issued` varchar(20) NOT NULL,

 

As you can see the column type for date_issued is VARCHAR. This means MySQL does not know it's a date. It thinks it's just a string. Same for date_done.

Also VARCHAR is not some kind of magic datatype that's applicable for each and every kind of data... yet almost all column in this table use it... Maybe you should rethink it?

Link to comment
Share on other sites

ok so its to late to think it over.already lots of data in there.

 

now i have notice that when i did this i added a line month.

 

so is there a way to select  all from fc were month =january and date is befor 01/01/2009

 

 

Link to comment
Share on other sites

164 rows is not much, and you can do the change in four queries.

 

1. Create a column date_issued_temp (DATE)

2. Copy the content of date_issued to date_issued_temp using STR_TO_DATE() function to convert your strings to date.

3. Drop the date_issued column

4. Rename date_issued_temp to date_issued

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.