Ver Mensaje Individual
  #7 (permalink)  
Antiguo 09/06/2009, 06:55
Avatar de huesos52
huesos52
Colaborador
 
Fecha de Ingreso: febrero-2009
Ubicación: Manizales - Colombia
Mensajes: 5.980
Antigüedad: 15 años, 2 meses
Puntos: 360
Respuesta: UPDATE usando DateDiff

Anarquique... no sabria que decirte.

Mira lo que yo he hecho y nunca me da error.

Código mysql:
Ver original
  1. mysql> create table anarquique(fecha_fin date, fecha_inicio date, numero_dias float);
  2. Query OK, 0 rows affected (0.16 sec)
  3.  
  4. mysql> insert into anarquique (fecha_fin,fecha_inicio) values(now(),'2009-01-01');
  5. Query OK, 1 row affected (0.05 sec)
  6.  
  7. mysql> insert into anarquique (fecha_fin,fecha_inicio) values('2009-12-31',now());
  8. Query OK, 1 row affected (0.00 sec)
  9.  
  10. mysql> select *from anarquique;
  11. +------------+--------------+-------------+
  12. | fecha_fin  | fecha_inicio | numero_dias |
  13. +------------+--------------+-------------+
  14. | 2009-06-09 | 2009-01-01   |        NULL |
  15. | 2009-12-31 | 2009-06-09   |        NULL |
  16. +------------+--------------+-------------+
  17. 2 rows in set (0.00 sec)
  18.  
  19. mysql> update anarquique set numero_dias=datediff(fecha_fin,fecha_inicio);
  20. Query OK, 2 rows affected (0.02 sec)
  21. Rows matched: 2  Changed: 2  Warnings: 0
  22.  
  23. mysql> select *from anarquique;
  24. +------------+--------------+-------------+
  25. | fecha_fin  | fecha_inicio | numero_dias |
  26. +------------+--------------+-------------+
  27. | 2009-06-09 | 2009-01-01   |         159 |
  28. | 2009-12-31 | 2009-06-09   |         205 |
  29. +------------+--------------+-------------+
  30. 2 rows in set (0.00 sec)
  31.  
  32. mysql> alter table anarquique fecha_inicio datetime;
  33. ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQ
  34. mysql> alter table anarquique modify fecha_inicio datetime;
  35. Query OK, 2 rows affected (0.16 sec)
  36. Records: 2  Duplicates: 0  Warnings: 0
  37.  
  38. mysql> alter table anarquique modify fecha_fin datetime;
  39. Query OK, 2 rows affected (0.02 sec)
  40. Records: 2  Duplicates: 0  Warnings: 0
  41.  
  42. mysql> update anarquique set numero_dias=datediff(fecha_fin,fecha_inicio);
  43. Query OK, 0 rows affected (0.00 sec)
  44. Rows matched: 2  Changed: 0  Warnings: 0
  45.  
  46. mysql> insert into anarquique (fecha_fin,fecha_inicio) values('0000-00-00 00:00:00','0000-00-00 00:00:00
  47. Query OK, 1 row affected (0.00 sec)
  48.  
  49. mysql> update anarquique set numero_dias=datediff(fecha_fin,fecha_inicio);
  50. Query OK, 0 rows affected (0.00 sec)
  51. Rows matched: 3  Changed: 0  Warnings: 0
  52.  
  53. mysql> select *from anarquique;
  54. +---------------------+---------------------+-------------+
  55. | fecha_fin           | fecha_inicio        | numero_dias |
  56. +---------------------+---------------------+-------------+
  57. | 2009-06-09 00:00:00 | 2009-01-01 00:00:00 |         159 |
  58. | 2009-12-31 00:00:00 | 2009-06-09 00:00:00 |         205 |
  59. | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |        NULL |
  60. +---------------------+---------------------+-------------+
  61. 3 rows in set (0.00 sec)
  62.  
  63. mysql> insert into anarquique (fecha_fin,fecha_inicio) values('0000-00-00 00:00:00',now());
  64. Query OK, 1 row affected (0.00 sec)
  65.  
  66. mysql> update anarquique set numero_dias=datediff(fecha_fin,fecha_inicio);
  67. Query OK, 0 rows affected (0.00 sec)
  68. Rows matched: 4  Changed: 0  Warnings: 0
  69.  
  70. mysql> select *from anarquique;
  71. +---------------------+---------------------+-------------+
  72. | fecha_fin           | fecha_inicio        | numero_dias |
  73. +---------------------+---------------------+-------------+
  74. | 2009-06-09 00:00:00 | 2009-01-01 00:00:00 |         159 |
  75. | 2009-12-31 00:00:00 | 2009-06-09 00:00:00 |         205 |
  76. | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |        NULL |
  77. | 0000-00-00 00:00:00 | 2009-06-09 07:50:11 |        NULL |
  78. +---------------------+---------------------+-------------+
  79. 4 rows in set (0.00 sec)
  80.  
  81. mysql> update anarquique set numero_dias=datediff(fecha_fin,fecha_inicio);
  82. Query OK, 0 rows affected (0.00 sec)
  83. Rows matched: 4  Changed: 0  Warnings: 0
  84.  
  85. mysql>

Asegurate que tanto fecha_fin como fecha_inicio sean tipo datetime, timestamp o date. Que numero_dias sea numerico (float, decimal) y no deberías tener problemas. Que versión de mysql usas?
__________________
Without data, You are another person with an opinion.
W. Edwads Deming