[Lunar calendar] calculation method
1. Built a table, put the current information, because the date of the lunar calendar is, and now, now only 2049, after the future, you can join! CREATE TABLE SolarData (yearId int not null, data char (7) not null, dataInt int not null) - insert data INSERT INTO SolarDataa SELECT 1900, '0x04bd8', 19416 UNION ALL SELECT 1901, '0x04ae0', 19168 UNION ALL SELECT 1902, '0x0a570', 42352 UNION ALL SELECT 1903, '0x054d5', 21717 UNION ALL SELECT 1904, '0x0d260', 53856 UNION ALL SELECT 1905, '0x0d950', 55632 UNION ALL SELECT 1906, '0x16554', 91476 UNION ALL SELECT 1907, '0x056a0', 22176 UNION ALL SELECT 1908, '0x09ad0', 39632 UNION ALL SELECT 1909, '0x055d2', 21970 UNION ALL SELECT 1910, '0x04ae0', 19168 UNION ALL SELECT 1911, '0x0a5b6', 42422 UNION ALL SELECT 1912, '0x0a4d0', 42192 UNION ALL SELECT 1913, '0x0d250', 53840 UNION ALL SELECT 1914, '0x1d255', 119381 UNION ALL SELECT 1915, '0x0b540', 46400 UNION ALL SELECT 1916, '0x0d6a0', 54944 UNION ALL SELECT 1917, '0x0ada2', 44450 Union All Select 1918, '0x095B0', 38320 Union All Select 1919, '0x14977', 84343 UN ION ALL SELECT 1920, '0x04970', 18800 UNION ALL SELECT 1921, '0x0a4b0', 42160 UNION ALL SELECT 1922, '0x0b4b5', 46261 UNION ALL SELECT 1923, '0x06a50', 27216 UNION ALL SELECT 1924, '0x06d40', 27968 UNION ALL SELECT 1925, '0x1ab54', 109396 UNION ALL SELECT 1926, '0x02b60', 11104 UNION ALL SELECT 1927, '0x09570', 38256 UNION ALL SELECT 1928, '0x052f2', 21234 UNION ALL SELECT 1929, '0x04970'
, 18800 UNION ALL SELECT 1930, '0x06566', 25958 UNION ALL SELECT 1931, '0x0d4a0', 54432 UNION ALL SELECT 1932, '0x0ea50', 59984 UNION ALL SELECT 1933, '0x06e95', 28309 UNION ALL SELECT 1934, '0x05ad0' , 23248 UNION ALL SELECT 1935, '0x02b60', 11104 UNION ALL SELECT 1936, '0x186e3', 100067 UNION ALL SELECT 1937, '0x092e0', 37600 UNION ALL SELECT 1938, '0x1c8d7', 116951 UNION ALL SELECT 1939, '0x0c950' , 51536 UNION ALL SELECT 1940, '0x0d4a0', 54432 UNION ALL SELECT 1941, '0x1d8a6', 120998 UNION ALL SELECT 1942, '0x0b550', 46416 UNION ALL SELECT 1943, '0x056a0', 22176 UNION ALL SELECT 1944, '0x1a5b4' , 107956 UNION ALL SELECT 1945, '0x025d0', 9680 UNION ALL SELECT 1946, '0x092d0', 37584 UNION ALL SELECT 1947, '0x0d2b2', 53938 UNION ALL SELECT 1948, '0x0a950', 43344 UNION ALL SELECT 1949, '0x0b557' , 46423 Union All Select 1950, '0x06ca0', 27808 Union All Select 1951, '0x0B550', 46416 Union All SELECT 1952, '0x15355', 86869 Union All Select 195 3, '0x04da0', 19872 UNION ALL SELECT 1954, '0x0a5d0', 42448 UNION ALL SELECT 1955, '0x14573', 83315 UNION ALL SELECT 1956, '0x052d0', 21200 UNION ALL SELECT 1957, '0x0a9a8', 43432 UNION ALL SELECT 1958, '0x0e950', 59728 UNION ALL SELECT 1959, '0x06aa0', 27296 UNION ALL SELECT 1960, '0x0aea6', 44710 UNION ALL SELECT 1961, '0x0ab50', 43856 UNION ALL SELECT 1962, '0x04b60', 19296 UNION ALL SELECT 1963, '0x0ae4', 43748 Union All SELECT 1964, '
0x0a570 ', 42352 UNION ALL SELECT 1965,' 0x05260 ', 21088 UNION ALL SELECT 1966,' 0x0f263 ', 62051 UNION ALL SELECT 1967,' 0x0d950 ', 55632 UNION ALL SELECT 1968,' 0x05b57 ', 23383 UNION ALL SELECT 1969,' 0x056a0 ', 22176 UNION ALL SELECT 1970,' 0x096d0 ', 38608 UNION ALL SELECT 1971,' 0x04dd5 ', 19925 UNION ALL SELECT 1972,' 0x04ad0 ', 19152 UNION ALL SELECT 1973,' 0x0a4d0 ', 42192 UNION ALL SELECT 1974,' 0x0d4d4 ', 54484 UNION ALL SELECT 1975,' 0x0d250 ', 53840 UNION ALL SELECT 1976,' 0x0d558 ', 54616 UNION ALL SELECT 1977,' 0x0b540 ', 46400 UNION ALL SELECT 1978,' 0x0b5a0 ', 46496 UNION ALL SELECT 1979,' 0x195a6 ', 103846 UNION ALL SELECT 1980,' 0x095b0 ', 38320 UNION ALL SELECT 1981,' 0x049b0 ', 18864 UNION ALL SELECT 1982,' 0x0a974 ', 43380 UNION ALL SELECT 1983,' 0x0a4b0 ', 42160 UNION ALL SELECT 1984,' 0x0B27A ', 45690 Union All Select 1985,' 0x06a50 ', 27216 Union All Select 1986,' 0x06D40 ', 27968 Union All Select 1987,' 0x0af46 ', 44870 Union All SELEC T 1988, '0x0ab60', 43872 UNION ALL SELECT 1989, '0x09570', 38256 UNION ALL SELECT 1990, '0x04af5', 19189 UNION ALL SELECT 1991, '0x04970', 18800 UNION ALL SELECT 1992, '0x064b0', 25776 UNION ALL SELECT 1993, '0x074a3', 29859 UNION ALL SELECT 1994, '0x0ea50', 59984 UNION ALL SELECT 1995, '0x06b58', 27480 UNION ALL SELECT 1996, '0x055c0', 21952 UNION ALL SELECT 1997, '0x0ab60', 43872 UNION ALL SELECT 1998, '0x096d5', 38613 Union All Select 1999, '
0x092e0 ', 37600 UNION ALL SELECT 2000,' 0x0c960 ', 51552 UNION ALL SELECT 2001,' 0x0d954 ', 55636 UNION ALL SELECT 2002,' 0x0d4a0 ', 54432 UNION ALL SELECT 2003,' 0x0da50 ', 55888 UNION ALL SELECT 2004,' 0x07552 ', 30034 UNION ALL SELECT 2005,' 0x056a0 ', 22176 UNION ALL SELECT 2006,' 0x0abb7 ', 43959 UNION ALL SELECT 2007,' 0x025d0 ', 9680 UNION ALL SELECT 2008,' 0x092d0 ', 37584 UNION ALL SELECT 2009,' 0x0cab5 ', 51893 UNION ALL SELECT 2010,' 0x0a950 ', 43344 UNION ALL SELECT 2011,' 0x0b4a0 ', 46240 UNION ALL SELECT 2012,' 0x0baa4 ', 47780 UNION ALL SELECT 2013,' 0x0ad50 ', 44368 UNION ALL SELECT 2014,' 0x055d9 ', 21977 UNION ALL SELECT 2015,' 0x04ba0 ', 19360 UNION ALL SELECT 2016,' 0x0a5b0 ', 42416 UNION ALL SELECT 2017,' 0x15176 ', 86390 UNION ALL SELECT 2018,' 0x052b0 ', 21168 UNION ALL SELECT 2019,' 0x0A930 ', 43312 Union All Select 2020,' 0x07954 ', 31060 Union All Select 2021,' 0x06aa0 ', 27296 Union All SELECT 2022,' 0x0AD50 ', 44368 Union All SELECT 2023, '0x05b52', 23378 UNION ALL SELECT 2024, '0x04b60', 19296 UNION ALL SELECT 2025, '0x0a6e6', 42726 UNION ALL SELECT 2026, '0x0a4e0', 42208 UNION ALL SELECT 2027, '0x0d260', 53856 UNION ALL SELECT 2028, '0x0ea65', 60005 UNION ALL SELECT 2029, '0x0d530', 54576 UNION ALL SELECT 2030, '0x05aa0', 23200 UNION ALL SELECT 2031, '0x076a3', 30371 UNION ALL SELECT 2032, '0x096d0', 38608 UNION ALL SELECT 2033, '0x04bd7', 19415 Union All SELECT 2034, '
0x04ad0 ', 19152 UNION ALL SELECT 2035,' 0x0a4d0 ', 42192 UNION ALL SELECT 2036,' 0x1d0b6 ', 118966 UNION ALL SELECT 2037,' 0x0d250 ', 53840 UNION ALL SELECT 2038,' 0x0d520 ', 54560 UNION ALL SELECT 2039,' 0x0dd45 ', 56645 UNION ALL SELECT 2040,' 0x0b5a0 ', 46496 UNION ALL SELECT 2041,' 0x056d0 ', 22224 UNION ALL SELECT 2042,' 0x055b2 ', 21938 UNION ALL SELECT 2043,' 0x049b0 ', 18864 UNION ALL SELECT 2044,' 0x0a577 ', 42359 UNION ALL SELECT 2045,' 0x0a4b0 ', 42160 UNION ALL SELECT 2046,' 0x0aa50 ', 43600 UNION ALL SELECT 2047,' 0x1b255 ', 111189 UNION ALL SELECT 2048,' 0x06d20 ', 27936 UNION ALL SELECT 2049,' 0x0ada0 ', 44448 -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------ - then you can use this function I wrote to take the Lunar New Function FN_GETLUNAR (@solarday datetime) Returns DateTime As begin Declare @soldata int declare @offset int declare @ilunar int declare @i int decl ARE @j INT DECLARE @yDays int DECLARE @mDays int DECLARE @mLeap int DECLARE @mLeapNum int DECLARE @bLeap smallint DECLARE @temp int DECLARE @YEAR INT DECLARE @MONTH INT DECLARE @DAY INT DECLARE @OUTPUTDATE DATETIME - guarantee passed in Date is free of time set @ Solarday = Cast (@solarday as char (10)) set @ Offset = Cast (@ SolarDay-'1900-01-30 '
AS INT - Determine the Lunar Year Start Set @ i = 1900 --set @ Offset = @ Soldata While @i <2050 and @offset> 0 Begin Set @ YDAYS = 348 Set @ mleapnum = 0 Select @ ilunar = DatAint from Solardata WHERE YEARID = @ i - Total number of days of returning the Lunar New Year Set @ J = 32768 While @ J> 8 Begin if @ilunar & @j> 0 set @ YDAYS = @ YDAYS 1 set @ J = @ J / 2 End - Choosing the Lunar Year of the Lunar Year 1-12, no 闰 0 0 0 set @mleap = @ilunar & 15 - Remove the number of days of the lunar month, add the number of days in the year if @mleap> 0 Begin IF @ilunar & 65536> 0 set @ mleapnum = 30 else set @ mleapnum = 29 set @ ydays = @ YDAYS @ mleapnum End set @ Offset = @ Offset- @ YDAYS SET @ i = @ i 1 end if @offset <= 0 begin set @ Offset = @ @ @ @ i-1 End - Determine the Lunar Year end set @ Year = @ i - Determine the Chinese Moon Start Set @i = 1 Select @ ilunar = DatAint from Solardata WHERE YEEARID = @
Year - Judging that month is Run Yue set @mleap = @ilunar & 15 set @bleap = 0 while @i <13 and @offset> 0 Begin - Judgment Lunun set @ mdays = 0 if (@mleap> 0 and @i = (@ mleap 1) and @ bleap = 0) Begin - is the Lun Moon set @ i = @ i-1 set @ bleap = 1 - Remove the number of days of the day of the Lunar Year of the Lunar, IF @ilunar & 65536> 0 Set @MDAYS = 30 else set @MDAYS = 29 END ELSE - not Lunun Begin Set @ J = 1 set @temp = 65536 While @J <= @ i begin set @ j = @ @ J 1 End if @ilunar & @temp> 0 set @mda YS = 30 else set @MDAYS = 29 End - Unnight month if @ bleap = 1 and @ = (@ mleap 1) set @ bleap = 0 set @ Offset = @ @ @ @ mdays set @ i = @ i 1 end if @offset <= 0 begin set @ offset = @ Offset @ mdays set @ i = @
I-1 end - Determine the End of the Lunar Moon Set @ Month = @ i - Determine the End of Lunar Day Set @ day = @ Offset Set @ OutputDate = Cast ((@Year As Varchar (4)) '-' Cast (@Month As Varchar (2)) '-' Cast (@day as varchar (2))) AS dateTime) Return @outputdate end ----------------- -------------------------------------------------- --- - calling method select dbo.fn_GetLunar (getdate ()) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ SQL Server Date Calculation
Typically, you need to get the current date and calculate some other dates, for example, your program may need to judge the first day or last day. Most of you know how to split the date (year, month, day, etc.), and then use only the annual, month, day, and the date you need! In this article, I will tell you how to use the Dateadd and Datediff functions to calculate some different dates that may be used in your program. You must pay attention to the following questions before using the examples in this article. Most of these may not be all the results performed on different machines may not be the same, which is the first day of a week. This setting decision is determined. The first day (DateFirst) setting determines which day of your system uses the first day of the week. All of the following examples are established as a week on Sunday, which is set to 7 on the first day. If your first day setting is different, you may need to adjust these examples, making it in line with different first days. You can check the first day by @@ DateFirst function. To understand these examples, let's review the Datediff and DateAdd functions first. The datediff function calculates the total number of hours, days, week, months, annual time intervals between two dates. The dateAdd function calculates a date by adding a date by giving time interval to get a new date. To learn more of the dateDiff and dateadd functions and time intervals, you can read Microsoft online help. Use the DateDiff and DateAdd functions to calculate the date, and it is a bit different from the time you convert from the current date to the date you need. You have to consider this aspect from time interval. For example, how many time intervals from the date you have to get, or how many time intervals are there from today to a certain day (such as 1900-1-1), and so on. Understand how to focus on time interval helps you easily understand my different date calculations. The first day of the first day of a month, I will tell you how to go from the last day of this month from the current date. Note: This example and other examples in this article will only use the dateDiff and dateadd functions to calculate the date we want. Each example will be calculated before the time interval, and then the addition or subtraction is made to get the date you want to calculate. This is calculating the first day of the SQL script: select dateadd (mm, datediff (mm, 0), getDate ()), 0) Separate this statement to see how it works. The core function is getDate (), most people know this is a function that returns the current date and time. The next executed function datediff (mm, 0, getdate ()) is the number of months between the current date and "1900-01-01 00: 00.000". Remember: The period and time variables are calculated from "1900-01-01 00:00:00.000". That's why you can specify the first time expression to "0" in the dateDiff function. The next function is dateadd, increasing the number of times the current date to "1900-01-01". We can get this month's first day by increasing the month of the predefined date "1900-01-01" and the current date. In addition, the calculated date of the date will be "00: 00: 00.000". This calculation skill is to calculate the current date to "1900-01-01" number, then add it to "1900-01-01" to get a special date, this tip can be used to calculate many different dates . The next example is also a different date from the current date. This week's Monday here I use week (WK) time intervals which day is Monday this week.
Select DateAdd (WK, Datediff (WK, 0, getDate ()), 0) The first day of the year (YY) is now used in the first day of the year. Select Dateadd (YY, Datediff (YY, 0, getDate ()), 0) The first day of the quarter, if you want to calculate the first day of this quarter, this example tells you how to do it. Select Dateadd (QQ, Datediff (QQ, 0, Getdate ()), 0) The day of the day has needed to pass the getDate () function to cut off the time in order to return the time value, consider whether the current date is overnight. If this example, this example uses the dateDiff and dateadd functions to get the point in the middle of the night. Select DateAdd (DD, DD, 0, Getdate ()), 0) Deeper DateDiff and Dateadd Functions Calculate You can understand that by using simple dateDiff and dateadd functions, you can find a lot of different possible dates. All examples thereof only calculate only the current time and the number of time intervals between "1900-01-01", and then add it to the "1900-01-01" time interval to calculate the date. Assume that you modify the number of time intervals, or use a different time interval to call the DateAdd function, or subtract time intervals instead of increasing, you can discover and make differences through these small adjustments. Here are four examples of using another DATEADD function to calculate two time intervals before and after the dateadd function in the last day. The last day of last month This is an example of calculating the last day of last month. It is obtained by minus 3 milliseconds from the last day of one month. One thing to remember, the time in SQL Server is accurate to 3 milliseconds. That's why I need to lose 3 milliseconds to get the date and time I want. SELECT DATEADD (MS, -3, dateadd (mm, datediff (mm, 0) calculated date of the date of the date with a day of SQL Server can record ("23:59 : 59: 997 ") time. Last day, the last day of last year, in order to get the last day last year, you need to lose 3 milliseconds on the first day of this year. Select Dateadd (MS, -3, Dateadd (YY, Datediff (YY, 0)) The last day of this month is now, in order to get the last day of this month, I need to modify a lot of money. The last day of statement. Modification needs to be added to the time interval returned by Datediff and "1900-01-01". By adding 1 month, I calculated the first day of the next month, then minus 3 milliseconds, which calculated this month's last day. This is the SQL script that calculates the last day of this month. Select Dateadd (MS, -3, Dateadd (mm, datediff (m, 0, getdate ()) 1, 0)) The last day of this year You should now master this practice, this is the last day of this year SELECT Dateadd (MS, -3, dateAdd (YY, Datediff (YY, 0, getDate ()) 1, 0)). The first week of this month is good, now is the last example. Here I am going to calculate the first Monday of this month. This is the calculated script.
Select Dateadd (WK, Datediff (WK, DatePart (DD (DD, 6-datepart ()), getdate ())), 0) In this example, I used "Monday this week" Script and make a little modification. The modified part is to replace the "getDate ()" section in the original script to calculate this month, replacing the current date in the calculation of this month, make the calculation to get this month's first Monday . Summary I hope that these examples can be inspired when you calculate the date with the Dateadd and Datediff functions. By using this time interval of this calculation date, I found that useful calendar in order to display the intervals between the two dates. Note that this is just a method of calculating these dates. It is necessary to keep in mind, there are many ways to get the same calculation results. If you have other methods, it is very good, if you don't, I hope these examples can give you some inspiration, when you want to use the Dateadd and Datediff function to calculate the date you may use when you can use.
-------------------------------------------------- ------------- Appendix, Other Date Treatment Method 1) Remove the timest second declare @ DateTime set @ = getdate () - '2003-7-1 10:00:00' select @, DATEADD (Day, Datediff (0, @), 0) 2) Show the week Select Datename (Weekday, getDate ()) 3) How to get a certain number of days in a month declare @m Int set @ m = 2 - month SELECT dateDiff (DAY, '2003 -' CAST (@M as varchar) '- 15', '2003 -' CAST (@ m 1 as varchar) '- 15') In addition, the number of days this month Select Datediff (day, cast (Month (getdate ()) as varchar) '-' casc (Month (Getdate ()) as varchar) '- 15', Cast (Month (getdate ()) as varchar) ' - ' CAST (Month (Getdate ()) 1 as varchar) ' - 15 ') or uses the last day of this month, then use the Day function area Select Day (DateAdd (MS, -3, Dateadd (mm, datediff (m, 0, getdate ()) 1, 0))) 4) Judgment whether the leap year: select Case Day (MM, 2, Dateadd (MS, -3, DateAdd (Yy, Datediff) YY, 0, Getdate (), 0))))))))))) WHEN 28 TEN 'ELSE' leap year 'end or Select Case Datediff (Day, Datename (Year, getDate ()) ' - 02-01 ', DateAdd ( MM, 1, Datename (Year, getDate ()) '- 02-01')) when 28 Ten ' Else 'leap year' end 5) How many days in a quarter Declare @m tinyint, @ Time SmallDatetime Select @ m = month ()) Select @ m = case when @m Between 1 and 3 Ten 1 by @m between 4 and 6 THEN 4 WHEN @M Between 7 and 9 THEN 7 else 10 End Select @ Time = Datename (Year, getDate ()) '-' Convert (varchar (10), @ m) '- 01'