开发者

Excel ODBC Import from MySQL

I have an excel import from ODBC setup using MSQuery and all of the data and rows I want are being returned fine there however when I send the data to my excel workbook I am missing 3 fields day, month & year Can anyone help find out why?

SELECT  contract_0.create_date AS 'Submitted Date', 
        -- abbreviating
        contract_0.install_date AS 'installed_US', 
        RIGHT(contract_0.install_date,2) AS 'day', 
        MID(contract_0.install_date,6,2) AS 'month',
        LEFT(contract_0.install_date,4) As 'year'
            -- abbreviating
FROM `web54-reiga-gms`.contact contact_0, `web54-reiga-gms`.contract contract_0, `web54-reiga-gms`.user user_0
WHERE contract_0.contact_id = contact_0.id AND contract_0.user_id = user_0.id

My MSQuery Code above

*EDIT **

I am using Excel 2010 on Windows 7 and MySQL ODBC 3.5

UPDATE **

CREATE TABLE `contract` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `installer` varchar(50) DEFAULT NULL,
  `fitter` varchar(50) DEFAULT NULL,
  `guarantee_no` varchar(50) DEFAULT NULL,
  `contact_id` int(11) DEFAULT NULL开发者_运维知识库,
  `equipment` varchar(32) DEFAULT NULL,
  `certificate_no` varchar(50) DEFAULT NULL,
  `install_date` date DEFAULT '0000-00-00',
  `create_date` date DEFAULT '0000-00-00',
  `edit_date` date DEFAULT '0000-00-00',
  `edited_by` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1;


You are treating the install_date field as a string. It's probably a Date or DateTime field and to get the individual year, month and day values you should use the extract function.

OK. It is as I suspected and you are dealing with a Date. Replace these lines:

    RIGHT(contract_0.install_date,2) AS 'day', 
    MID(contract_0.install_date,6,2) AS 'month',
    LEFT(contract_0.install_date,4) As 'year',

with these:

EXTRACT(YEAR FROM contract_0.install_date) AS 'day', 
EXTRACT(MONTH FROM contract_0.install_date) AS 'month',
EXTRACT(DAY FROM contract_0.install_date) As 'year',
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜