我试图显示数据库中两个日期(列)之间的天数。输出显示为varchar数据类型。我应该如何将其转换为int数据类型?以下是弹出的错误消息:
第245层状态1状态42行
转换varchar值“过期日”时转换失败 数据类型为int。
我尝试同时使用CAST和CONVERT语法,但是它们都不起作用。
SELECT DVD_mem_id AS 'Member ID',
DVD_mem_name AS 'Member Name',
DVD_movie_id AS 'Movie ID',
DVD_movie_title AS 'Movie Title',
DVD_borrowing_date AS 'Date of Borrow',
DVD_date_due AS 'Date Due',
CAST('Overdue Day(s)' AS int),
DATEDIFF(DAY, DVD_borrowing_date, DVD_date_due) -3 AS 'Overdue Day(s)',
'Overdue Day(s)' * 2 AS 'Fine Incurred'
FROM DVD_T
WHERE (DVD_mem_id IS NOT NULL) AND
(DVD_mem_name IS NOT NULL) AND
(DVD_borrowing_date IS NOT NULL) AND
(DVD_date_due) IS NOT NULL AND
('Overdue Day(s)' > 0)
答案 0 :(得分:1)
我认为您需要的是这个,因为@jarlh说'Overdue Day(s)'
是一个字符串,您无法进行任何数学运算。
SELECT DVD_mem_id AS 'Member ID',
DVD_mem_name AS 'Member Name',
DVD_movie_id AS 'Movie ID',
DVD_movie_title AS 'Movie Title',
DVD_borrowing_date AS 'Date of Borrow',
DVD_date_due AS 'Date Due',
CAST((DATEDIFF(DAY, DVD_borrowing_date, DVD_date_due) -3) AS int),
DATEDIFF(DAY, DVD_borrowing_date, DVD_date_due) -3 AS 'Overdue Day(s)',
(DATEDIFF(DAY, DVD_borrowing_date, DVD_date_due) -3) * 2 AS 'Fine Incurred'
FROM DVD_T
WHERE (DVD_mem_id IS NOT NULL) AND
(DVD_mem_name IS NOT NULL) AND
(DVD_borrowing_date IS NOT NULL) AND
(DVD_date_due) IS NOT NULL AND
(DATEDIFF(DAY, DVD_borrowing_date, DVD_date_due) -3 > 0)