Share via

how to Flag

Ambro duh 21 Reputation points
2022-03-27T20:55:55.83+00:00

Hi everyone,

Movie table have the following attributes:
• Title
• DirectorId
• YearOfRelease

I want know how can I flag movies that are no longer available to view.
I do not want the movie removed from the table but rather that it is flagged as being unavailable.

Many thanks

Developer technologies | Transact-SQL
Developer technologies | Transact-SQL

A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.

SQL Server | Other
SQL Server | Other

Additional SQL Server features and topics not covered by specific categories

0 comments No comments

Answer accepted by question author

LiHong-MSFT 10,061 Reputation points
2022-03-28T06:39:07.36+00:00

Hi @Ambro duh
Since you don't want to add an extra column to the table,maybe you could have a try on CASE WHEN sentence,like this:

SELECT Title,DirectorId,YearOfRelease,  
       CASE WHEN YEAR(GETDATE())-YearOfRelease <= 2  --Modify the boolean expression here to suit your needs  
	        THEN 'Yes'  
			ELSE 'No'   
			END AS [If Available]  
FROM Movie_Table  

Best regards,
LiHong


If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

Was this answer helpful?

0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Erland Sommarskog 134.7K Reputation points MVP Volunteer Moderator
    2022-03-27T22:05:47.613+00:00

    As Naomi says, you will need to add a column to hold the information on whether the movie is available or not.

    A simple way is to just add a bit column:

       ALTER TABLE movies ADD available bit NOT NULL DEFAULT 1  
    

    And then you need to implement a process to mark movies as unavailable:

       UPDATE movies  
       SET     available = 0  
       WHERE Titlle = [@](/users/na/?userId=17da8bdc-bffd-0003-0000-000000000000)  
            AND YearOfRelease = @year  
    

    I'm here assuming that Title and YearOfRelease is your primary key.

    In a system I worked, we did not use bit columns, but rather we had a datetime2(3) column called deregdate, so we could track when a row was inactivated, but this is not always needed.

    Was this answer helpful?

    0 comments No comments

  2. Naomi Nosonovsky 8,906 Reputation points
    2022-03-27T21:03:29.913+00:00

    Your question is unclear. Are you asking of adding an extra column to the table (say, bit type) with the notAvailableForView name? Or do you want to select rows and certain criteria will tell you if the movie is avaiable or not?

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.