Use SQL to calculate the birthday Constellation

Source: Internet
Author: User

Just after lunch, I took a break. I saw a problem in the QQ group. I told you a date without a year and figured out the constellation of the date; the first idea at the beginning was to compare the start time and start time of the constellation to the start time. Then we can see the constellation in the distance! But then I thought, is there any other method besides the comparison of time intervals?

If you want to think about it, try to compare it with numbers (the comparison between the time and the date is basically the same after writing). Because the time range of the constellation is fixed, there will be no repetition, then I can convert the time into numbers. For example, if Aquarius is 1/20 to 2/18, then I can convert it to 120 to 218, but then there is a problem, such as today's date, in March 4, after being converted to a number, it will be changed to 34 (which should be 304). If there is no 0 in the middle, we cannot find the interval. This is always incorrect, the last step is to change the time to a number!

 
 
  1. SELECT @NowDate = (CAST(MONTH(@Birthday) AS VARCHAR(10)) + CAST((CASE WHEN DAY(@Bir  
  2. thday) < 10  THEN '0' + CAST(DAY(@Birthday) AS VARCHAR(10)) ELSE CAST(DAY(@Birthday)   
  3. AS VARCHAR(10)) END) AS VARCHAR(10))); 

The most troublesome way to calculate the constellation is to convert the date in the early stage. The time has been changed, and the rest is easy to do. Let's make a comparison of the number intervals, you can get the constellation you want!
 

 
 
  1. /*
  2. Constellation date (Gregorian calendar) English name
  3. Capricorn (12/22-01/19) Capricorn
  4. Aquarius (01/20-02/18)
  5. Pisces (02/19-03/20) Pisces
  6. Aries (03/21-04/20) Aries
  7. Taurus (04/21-05/20) Taurus
  8. Gemini (05/21-06/21)
  9. Cancer (06/22-07/22) Cancer
  10. Leo (07/23-08/22) Leo
  11. Virgo (08/23-09/22)
  12. Libra (09/23-10/22) Libra
  13. Scorpio (10/23-11/21) Scorpio
  14. Sagittarius (11/22-12/21) Sagittarius
  15. */
  16. DECLARE @ TABLE
  17. (
  18. Name_zh VARCHAR (128 ),
  19. Name_en VARCHAR (128 ),
  20. S_time INT,
  21. O_time INT
  22. );
  23. DECLARE @ NowDate INT;
  24. DECLARE @ Birthday DATETIME;
  25. SET @ Birthday = GETDATE ();
  26. SELECT @ NowDate = (CAST (MONTH (@ Birthday) as varchar (10) + CAST (case when day (@ Birthday) <10 THEN '0' + CAST (DAY (@ Birthday) as varchar (10) else cast (DAY (@ Birthday) as varchar (10) END) as varchar (10 )));
  27. INSERT
  28. @ TABLE
  29. VALUES
  30. ('Capricorn ', 'capricorn', 1222,119 ),
  31. ('Aquarius ', 'aquarius', 120,218 ),
  32. ('Pisces ', 'pisces', 219,320 ),
  33. ('Aries ', 'aries', 321,420 ),
  34. ('Taurus ', 'taurus', 421,520 ),
  35. ('Gemini', 'gemini ', 521,621 ),
  36. ('Cancer ', 'cancer', 622,722 ),
  37. ('Leo, 'Leo, 723,822 ),
  38. ('Virgo', 'virgo', 823,922 ),
  39. ('Libra ', 'libra', 923,102 2 ),
  40. ('Scorpio ', 'scorpio', 1023,112 1 ),
  41. ('Sagittarius ', 'sagittarius', 1122,122 1 );
  42. SELECT
  43. Name_zh,
  44. Name_en,
  45. S_time,
  46. O_time
  47. FROM
  48. @ TABLE
  49. WHERE
  50. @ NowDate BETWEEN s_time AND o_time; <BR>

This method has not been carefully verified, and may cause bugs or performance losses. It will be rewritten in the future!

Edit recommendations]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.