473,411 Members | 1,937 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

inserting a null value to Time type

Hello,

I have a Time type column on a table. I have set it to ¨no null¨. When I enter a null value the column stores a 00:00:00 value.

How can I do to store just an empty value, so that the user have the choice to don't enter any time and display no time when the record is displayed?
Actually, the important part for me is displaying no value, I really don't mind what it is stored, but I dont want to loose the option of storing a ¨midnight value.


thanks,
Mar 14 '08 #1
17 8458
amitpatel66
2,367 Expert 2GB
Hello,

I have a Time type column on a table. I have set it to ¨no null¨. When I enter a null value the column stores a 00:00:00 value.

How can I do to store just an empty value, so that the user have the choice to don't enter any time and display no time when the record is displayed?
Actually, the important part for me is displaying no value, I really don't mind what it is stored, but I dont want to loose the option of storing a ¨midnight value.


thanks,
You cannot store NULL in to NOT NULL column. And Since its TIME datatype, you cannot store something like White Spaces as well. You need to make the column to accept NULL values as well.
Mar 14 '08 #2
ronverdonk
4,258 Expert 4TB
Be careful: in a db a null value is not identical to a 0 (zero) value!

Null indicates the absence of a value, where 0 is a real value. Midnight is a zero value 00:00:00 and not a null value. Allowing a null in your time field indicates that you want the option of no value in your time field.

Ronald
Mar 14 '08 #3
You cannot store NULL in to NOT NULL column. And Since its TIME datatype, you cannot store something like White Spaces as well. You need to make the column to accept NULL values as well.
Ok, I have make the colum accept NULL values, and then?
There must be some way for achieving this(not storing, but dislaying a null value for a Time datatype when the user has fill in a time form with ¨no time fot this trip¨ for example)
Mar 14 '08 #4
Be careful: in a db a null value is not identical to a 0 (zero) value!

Null indicates the absence of a value, where 0 is a real value. Midnight is a zero value 00:00:00 and not a null value. Allowing a null in your time field indicates that you want the option of no value in your time field.

Ronald

Correct, but when inserting a null value I get 00:00:00 stored.
Mar 14 '08 #5
amitpatel66
2,367 Expert 2GB
Correct, but when inserting a null value I get 00:00:00 stored.
Thats becuase it cannot accept NULL value. Remove the CONSTRAINT NOT NULL from the column then it will be able to store NULL values
Mar 14 '08 #6
Thats becuase it cannot accept NULL value. Remove the CONSTRAINT NOT NULL from the column then it will be able to store NULL values
Sure, all ready done that and still getting 00:00:00. Thats the problem
Mar 14 '08 #7
ronverdonk
4,258 Expert 4TB
Ok, I have make the colum accept NULL values, and then?
There must be some way for achieving this(not storing, but dislaying a null value for a Time datatype when the user has fill in a time form with ¨no time for this trip¨ for example)
When you allow null in your column and select it, the result of the select will return the literal NULL. If you do not want that literal but e.g. the literal 'empty', then replace it in your query like
Expand|Select|Wrap|Line Numbers
  1. select field1, IF(field2 IS NULL, 'empty ', field2) as field2 FROM table_name;
Ronald
Mar 14 '08 #8
ronverdonk
4,258 Expert 4TB
Sure, all ready done that and still getting 00:00:00. Thats the problem
Set your default to NULL and all new records will have this when no time is filled in. The existing records however, keep the value they have.
So you wilkl have to UPDATE these separately.

Ronald
Mar 14 '08 #9
When you allow null in your column and select it, the result of the select will return the literal NULL. If you do not want that literal but e.g. the literal 'empty', then replace it in your query like
Expand|Select|Wrap|Line Numbers
  1. select field1, IF(field2 IS NULL, 'empty ', field2) as field2 FROM table_name;
Ronald
I have set the default to NULL and the column to accept null values, still storing 00:00:00. As the Mysql guide says a Time field cant store a null value, but their must be some coding to be done for solving this problem.
Mar 14 '08 #10
ronverdonk
4,258 Expert 4TB
But you do not want an empty field to show when you mean 00:00:00, i.e. 12 midnight, do you? If not, you just format your time field like this
Expand|Select|Wrap|Line Numbers
  1. SELECT field1, IF(field2 IS NULL, 'empty ', TIME_FORMAT(field2,'%h:%m:%s')) as field2 FROM table_name;
and you see the difference in your output: '00:00:00' for midnight, 'empty' for null..

Ronald
Mar 14 '08 #11
But you do not want an empty field to show when you mean 00:00:00, i.e. 12 midnight, do you? If not, you just format your time field like this
Expand|Select|Wrap|Line Numbers
  1. SELECT field1, IF(field2 IS NULL, 'empty ', TIME_FORMAT(field2,'%h:%m:%s')) as field2 FROM table_name;
and you see the difference in your output: '00:00:00' for midnight, 'empty' for null..

Ronald
sorry if this questions are to basic:

I have just talk about one time field, so what would be field1 and field2 in your code?

How do I insert that code in phpMyadmin for example, I bet it is not a query

I haven't been able to store a null value on the field to make true the if condition, but I want to try it anyway if you could answer this maybe dumb questions.


Thanks in advance,
Mar 14 '08 #12
ronverdonk
4,258 Expert 4TB
sorry if this questions are to basic:

I have just talk about one time field, so what would be field1 and field2 in your code?

How do I insert that code in phpMyadmin for example, I bet it is not a query

I haven't been able to store a null value on the field to make true the if condition, but I want to try it anyway if you could answer this maybe dumb questions.

Thanks in advance,
It is just an example. You have to rework it for use with your own table. In teh example field2 is the field that holds the time (or null).

Lucky you did not bet, of course it is an MySQL query. Just enter that in the SQL command popup of phpMyAdmin and hit 'start'.

To change a field value in a row you just enter the UPDATE query (mind: it is an example, you must change it to your own tablename and fields) like[
Expand|Select|Wrap|Line Numbers
  1. update table_name set time_field=null where other_field='xxx';
Ronald
Mar 15 '08 #13
I will try to order things up a little to make things clear about this post an its progress

1)Time type field cant store a null value, instead it stores 00:00:00 value.

2)I have a time form that gives the user the choice of "I am not sure of the time yet".

3)A code on the query could display an empty value when it found a 00:00:00 value stored in the database

4)By doing this, if the user enters in the form a midnight value or 00:00:00, the query would also display an empty value, and that is not wanted.

Hope to make things clearer and start suggesting solutions from now on.

Thanks again
Mar 15 '08 #14
docdiesel
297 Expert 100+
Hi,

to start with, I'm not a MySQL expert but I'm used to work with DB2. So it sounds a bit unusual to me that a database should not be able to store a NULL in a date field. But after reading the thread I'm suspecting somebody/something else to be betraying you.

As fas as I understand you're working with phpMyAdmin as frontend to MySQL. Did you check your problem with the command line mysql tool? For it could be that phpMyAdmin is converting the NULL into '00:00:00' when inserting without telling you.

I'd suggest to use the command line for a test. Try the following sql :
Expand|Select|Wrap|Line Numbers
  1. create table testtable (when time);
  2. insert into testtable (when) values ('10:20:30');
  3. insert into testtable (when) values (0);
  4. insert into testtable (when) values (NULL);
  5. select * from testtable;
and see what output it's generating.

If this is still wrong, at least there could be a workaround. Store the time as integer value (which MySQL HAS to allow to be NULL) using the seconds since midnight.

Regards,

Bernd
Mar 15 '08 #15
ronverdonk
4,258 Expert 4TB
I will try to order things up a little to make things clear about this post an its progress
1)Time type field cant store a null value, instead it stores 00:00:00 value.
2)I have a time form that gives the user the choice of "I am not sure of the time yet".
3)A code on the query could display an empty value when it found a 00:00:00 value stored in the database
4)By doing this, if the user enters in the form a midnight value or 00:00:00, the query would also display an empty value, and that is not wanted.

Hope to make things clearer and start suggesting solutions from now on.
Thanks again
Are you pulling my leg or what? Do I sense some dissatisfaction here? Then let it be clear: IN MYSQL YOU CAN ENTER A NULL VALUE IN A TIME FIELD WHEN YOUR FIELD ALLOWS NULLS!.

So don't waste time, but look at your own definiton of your own time field, or the way you (try to) fill that field. But don't give us nonsense about not being able to make a time field null when the definition of that time field allows nulls in its value!

To check your own MySQL, just run the following scenario and you'll see that it does exactly what is is supposed to do.
Expand|Select|Wrap|Line Numbers
  1. create table x (id int, time time);
  2. insert into x values(1,'00:00:00'),(2,null),(3,'12:00:00');
  3. select * from x;
  4. +------+----------+
  5. | id   | time     |
  6. +------+----------+
  7. |    1 | 00:00:00 |
  8. |    2 | NULL     |
  9. |    3 | 12:00:00 |
  10. +------+----------+
  11. update x set time=null where id=3;
  12. select * from x;
  13. +------+----------+
  14. | id   | time     |
  15. +------+----------+
  16. |    1 | 00:00:00 |
  17. |    2 | NULL     |
  18. |    3 | NULL     |
  19. +------+----------+
  20. update x set time=0 where id=2;
  21. select * from x;
  22. +------+----------+
  23. | id   | time     |
  24. +------+----------+
  25. |    1 | 00:00:00 |
  26. |    2 | 00:00:00 |
  27. |    3 | NULL     |
  28. +------+----------+
Ronald
Mar 16 '08 #16
I am sorry, I apologize
Mar 29 '08 #17
ronverdonk
4,258 Expert 4TB
No need to. All I want to know if it is clear what I posted and if you can use that information to use in your MySQL.

Ronald
Mar 29 '08 #18

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Smitty | last post by:
From the psql command line, I am having absolutely no problems inserting a NULL value into a date datatype field. When attempting to insert a NULL value via my Web app using SQL Relay, I am...
1
by: suslikovich | last post by:
Hi all, I am getting this error when insert values from one table to another in the first table the values are varchar (10). In the second they are datetime. The format of the data is mm/dd/yyyy...
0
by: tom c | last post by:
I am going through "Walkthrough: Editing and Inserting Data in Web Pages with the DetailsView Web Server Control" found at http://msdn2.microsoft.com/en-us/library/sdba1d59.aspx I am using...
6
by: fniles | last post by:
I am using VB.NET 2003 and SQL Server 2000. I have a table with a datetime column type. When inserting into the table for that column, I set it to Date.Now.ToString("T") , which is something like...
7
by: ebindia0041 | last post by:
This is like the bug from hell. It is kind of hard to explain, so please bear with me. Background Info: SQL Server 7.0, Asp.net 1.1 with c# I'm inserting simple records into a table. But one...
4
priyan
by: priyan | last post by:
hai everyone, I am having a doubt in inserting data in time field. I am having a table in which in column in timestamp without time zone datatype. I want to insert a row into the table but...
0
by: gp | last post by:
I am and have been using PDO for about a year now...and have finally gotten around to solving the "DB NULL value" issues I ran into early on... I am looking for suggestions and techniques to...
14
Parul Bagadia
by: Parul Bagadia | last post by:
Here is the code i hav written for inserting a no., after given no. in a link list; i guess the logic is ofcourse right. there is no error in it, but at the time of display its not displaying the...
1
by: javediq143 | last post by:
Hi All, This is my first post in this forum. I'm developing a CMS for my latest website. This CMS is also in PhP & MySQL. I'm done with the ADD section where the Admin can INSERT new records in...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.