Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


IF/ELSIF program in SQL

Posted on 2003-02-19
Medium Priority
Last Modified: 2006-11-17
Chinese Zodiac question:

I have to write an SQL program which I have to find what animal is on my birthyear.  I need to declare a date variable birth_date and assign it to my birthdate.  Use an IF/ELSIF structure to test every year and determine the animal associated with my birth year.  
1924. - Rat
1925. - Cow
1926. - Tiger
1927. - Rabbit
1928. - Dragon
1929. - Snake
...................... - Horse
...................... - Sheep
...................... - Monkey
...................... - Chicken
...................... - Dog
1935. - Pig

output should be this
dbms_output.put_line('I was born in ' || year || ', which is the year of the ???') ??? = whatever animal is in that year.
Question by:leegarson
  • 2
LVL 70

Accepted Solution

Scott Pletcher earned 200 total points
ID: 7983828
Are you using a SQL Server database?  If not, the statement below will almost certainly not work.  However, the LOGIC of how to determine the animal should work in any database/language, if you convert the SYNTAX.

SET @birth_date = 'Aug 17, 1975' --or whatever
SELECT 'I was born in ' + CAST(YEAR(@birth_date) AS CHAR(4)) + ' which is the year of the ' +
CASE (YEAR(@birth_date) - 1924) % 12
WHEN 2 THEN 'Tiger'
WHEN 11 THEN 'Pig'
LVL 70

Expert Comment

by:Scott Pletcher
ID: 7983859
Note that you could of course easily go back to dates prior to 1924 if you want to: just back up 1924 by even multiples of 12 years, the rest of the logic should function perfectly.

By the way, % is the modulo (remainder) operator in SQL Server; on other systems it is MOD or \ or whatever else.

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
This shares a stored procedure to retrieve permissions for a given user on the current database or across all databases on a server.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question