Question

I'm using SQL Server MS. I'm having trouble writing this script:

CREATE VIEW rental_view
AS SELECT 
  m.movie_id, movie_name, co.copy_id, f.format_id, format_name, cu.customer_id,
 (first_name + ' ' + surname) AS customer_name, rental_id, rental_date, 
 return_date, ISNULL(return_date, DATEDIFF(dd, rental_date, GETDATE())) 
 AS rental_duration 
FROM movie AS m INNER JOIN copy AS co
ON m.movie_id = co.movie_id
INNER JOIN format AS f
ON co.format_id = f.format_id
INNER JOIN rental 
ON co.copy_id = rental.copy_id
INNER JOIN customer AS cu
ON rental.customer_id = cu.customer_id

The goal for this is to create a view of the specified columns in the select. The last column specified is a computed column which is supposed to calculate the amount of days a rental has been out past it's rental date (only if return_date value is NULL) and display it as an INT. To my understanding I'm on the right track, but evidently not, as when executed the values that show up in that column are DATATIME datatype values, and make no sense.

Appreciate any help I can get.

Was it helpful?

Solution

Perhaps something like this will work for you. If the value is NULL then compare a dummy date 01-01-1900 and then either return the DATEDIFF or a zero value.

  CASE ISNULL(return_date,'19000101') 
  WHEN '19000101' THEN DATEDIFF(dd, rental_date, GETDATE()) 
  ELSE 0 END 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top