dbTalk Databases Forums  

DATE problem

comp.databases.mysql comp.databases.mysql


Discuss DATE problem in the comp.databases.mysql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Joe Hesse
 
Posts: n/a

Default DATE problem - 03-30-2011 , 12:39 PM






Hi,

I have a table with a DATE field that can be NULL and the default is NULL.
The table definition is shown below with all the fields not shown except
the one in question.

CREATE TABLE MyTable (
....
SepDate DATE NULL DEFAULT NULL,
....
) ENGINE = InnoDB;

I populate MyTable using a csv file that comes directly from
an Excel spreadsheet. The SQL command is:

LOAD DATA LOCAL INFILE '2011-03-28.csv'
INTO TABLE Brios.Brio2011_03_28
FIELDS TERMINATED BY '|'
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
.... more omitted

The terminator is a pipe symbol because I'm sure it does not appear as
data in the Excel file. It all works well except for the following minor
irritation.

Most, not all, of the DATE columns in the csv file are blank,
they appear as two consecutive pipe symbols with nothing in between,
i.e., ||.
After the LOAD these blank DATE values (||) appear in MyTable as
'0000-00-00'. I would like them to appear as NULL.

I realize I can do a query that would replace all '0000-00-00' with NULL
but I was hoping there would be another way.

Thank you,
Joe

Reply With Quote
  #2  
Old   
Álvaro G. Vicario
 
Posts: n/a

Default Re: DATE problem - 03-31-2011 , 02:34 AM






El 30/03/2011 19:39, Joe Hesse escribió/wrote:
Quote:
Hi,

I have a table with a DATE field that can be NULL and the default is NULL.
The table definition is shown below with all the fields not shown except
the one in question.

CREATE TABLE MyTable (
...
SepDate DATE NULL DEFAULT NULL,
...
) ENGINE = InnoDB;

I populate MyTable using a csv file that comes directly from
an Excel spreadsheet. The SQL command is:

LOAD DATA LOCAL INFILE '2011-03-28.csv'
INTO TABLE Brios.Brio2011_03_28
FIELDS TERMINATED BY '|'
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
... more omitted

The terminator is a pipe symbol because I'm sure it does not appear as
data in the Excel file. It all works well except for the following minor
irritation.

Most, not all, of the DATE columns in the csv file are blank,
they appear as two consecutive pipe symbols with nothing in between,
i.e., ||.
After the LOAD these blank DATE values (||) appear in MyTable as
'0000-00-00'. I would like them to appear as NULL.

I realize I can do a query that would replace all '0000-00-00' with NULL
but I was hoping there would be another way.
You can use a function to manipulate the input data if you store it in a
temporary variable. For instance, this is what I use to replace commas
with periods in numbers:

LOAD DATA LOCAL INFILE 'C:/path/to/mytable.txt' IGNORE
INTO TABLE mytable
FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\r\n'
(int_col, @float_col)
SET float_col = replace(@float_col, ',', '.');

MySQL casts the value into the column type when it reads it from the
file, *before* applying the transformations described in the SET clause.
If you instruct it to read it into a variable the value is handled as
string from the beginning.

In your case, you can do something like:

SET SepDate = CASE @SepDate WHEN '' THEN NULL ELSE @SepDate END

It's also interesting to disable the ALLOW_INVALID_DATES SQL mode:

http://dev.mysql.com/doc/refman/5.1/..._invalid_dates



--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--

Reply With Quote
  #3  
Old   
Álvaro G. Vicario
 
Posts: n/a

Default Re: DATE problem - 03-31-2011 , 02:45 AM



El 31/03/2011 9:34, "Álvaro G. Vicario" escribió/wrote:
Quote:
In your case, you can do something like:

SET SepDate = CASE @SepDate WHEN '' THEN NULL ELSE @SepDate END
Or maybe:

SET SepDate = CASE @SepDate WHEN '0000-00-00' THEN NULL ELSE @SepDate END

I haven't really tested it


--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--

Reply With Quote
  #4  
Old   
Tony Mountifield
 
Posts: n/a

Default Re: DATE problem - 03-31-2011 , 03:48 AM



In article <in1bf4$tbg$2 (AT) dont-email (DOT) me>,
lvaro G. Vicario <alvaro.NOSPAMTHANX (AT) demogracia (DOT) com.invalid> wrote:
Quote:
El 31/03/2011 9:34, "Álvaro G. Vicario" escribió/wrote:
In your case, you can do something like:

SET SepDate = CASE @SepDate WHEN '' THEN NULL ELSE @SepDate END
Or more concisely:

SET SepDate = NULLIF(@SepDate,'')

Quote:
Or maybe:

SET SepDate = CASE @SepDate WHEN '0000-00-00' THEN NULL ELSE @SepDate END
SET SepDate = NULLIF(@SepDate,'0000-00-00')

Quote:
I haven't really tested it
Neither have I! :-)

Tony
--
Tony Mountifield
Work: tony (AT) softins (DOT) co.uk - http://www.softins.co.uk
Play: tony (AT) mountifield (DOT) org - http://tony.mountifield.org

Reply With Quote
  #5  
Old   
Captain Paralytic
 
Posts: n/a

Default Re: DATE problem - 03-31-2011 , 04:21 AM



On Mar 31, 8:45*am, "lvaro G. Vicario"
<alvaro.NOSPAMTH... (AT) demogracia (DOT) com.invalid> wrote:
Quote:
El 31/03/2011 9:34, "lvaro G. Vicario" escribi/wrote:

In your case, you can do something like:

SET SepDate = CASE @SepDate WHEN '' THEN NULL ELSE @SepDate END

Or maybe:

SET SepDate = CASE @SepDate WHEN '0000-00-00' THEN NULL ELSE @SepDate END
CASE is complete overkill when you are only testing one thing for 2
alternative results. You should use IF() (except where as Tony pointed
out NULLIF() will do.

Reply With Quote
  #6  
Old   
lvaro G. Vicario
 
Posts: n/a

Default Re: DATE problem - 03-31-2011 , 05:52 AM



El 31/03/2011 10:48, Tony Mountifield escribi/wrote:
Quote:
In article<in1bf4$tbg$2 (AT) dont-email (DOT) me>,
lvaro G. Vicario<alvaro.NOSPAMTHANX (AT) demogracia (DOT) com.invalid> wrote:
El 31/03/2011 9:34, "Álvaro G. Vicario" escribió/wrote:
In your case, you can do something like:

SET SepDate = CASE @SepDate WHEN '' THEN NULL ELSE @SepDate END

Or more concisely:

SET SepDate = NULLIF(@SepDate,'')
Certainly. I tend to abuse CASE because it's the only construct that
doesn't force me to think what DBMS I'm currently using

That, and the fact that I can never remember the differences between
IF(), IFNULL and NULLIF()...

http://dev.mysql.com/doc/refman/5.1/...functions.html



--
-- http://alvaro.es - lvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programacin web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.