Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MYSQL how to declare a datetime variable?

Tags:

sql

mysql

My code:

DECLARE report_date DATETIME;
set report_date='2013-01-17 00:00:00';

SELECT  *
  FROM `NMPP`.`capacitypersecond` 
  WHERE `StreamDT` >=report_date and `StreamDT` < '2013-01-18 00:00:00'  ;

SELECT  *
  FROM `NMPP`.`capacityperHr` 
  WHERE `StreamDT` >=report_date and `StreamDT` < '2013-01-18 00:00:00'  ;

SELECT  *
  FROM `NMPP`.`capacityperDay` 
  WHERE `TJLDate` >=report_date and `TJLDate` < '2013-01-18 00:00:00'  ;

-

DECLARE report_date DATETIME;
/* SQL Error (1064): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DECLARE report_date DATETIME' at line 1 */
/* Affected rows: 0  Found rows: 0  Warnings: 0  Duration for 0 of 5 queries: 0.000 sec. */
like image 348
Scott 混合理论 Avatar asked Jan 18 '13 09:01

Scott 混合理论


2 Answers

or with cast:

set @report_date = cast('2013-01-17 00:00:00' as datetime);
like image 158
spacepille Avatar answered Sep 20 '22 13:09

spacepille


get rid of declare:

set @report_date = '2013-01-17 00:00:00';

SELECT  *
  FROM `NMPP`.`capacitypersecond` 
  WHERE `StreamDT` >= @report_date and `StreamDT` < '2013-01-18 00:00:00'  ;
like image 33
palindrom Avatar answered Sep 22 '22 13:09

palindrom