Answer the question
In order to leave comments, you need to log in
What is the correct way to use a WHILE loop in SQL?
Good afternoon. The problem is the following.
It is necessary to determine:
1) The serial number of the day in the calendar year
2) The name of the day
3) The serial number of the week in the calendar month
4) The serial number of the month in the calendar year
5) The serial number of the quarter in the calendar year
For several years. For example, from 1600-1605
I did this:
DECLARE @Year VARCHAR (50)
DECLARE @EndYear VARCHAR (50)
SET @Year = '02/02/2002'
SET @EndYear = '01/31/2014'
SET LANGUAGE Russian
while (@Year <= @EndYear)
BEGIN
SELECT CAST(convert(char(8),@Year,112) as datetime) AS 'CalendarYear',
DATENAME(month, @Year) AS 'Month Name',
DATEPART(DAYOFYEAR,@Year) AS 'NumberDayInCalendarYear',
DATEPART(QUARTER, @Year) AS 'NumverOfQuarter',
DATEPART(MONTH, @Year) AS 'NubmerMounthInCalendarYear',
DATEPART(WEEK, @Year) AS 'NumberOfWeekInCalendarYear',
CAST(convert(char(6),@Year,112)+'01' as datetime) AS 'CalendarMonthStartDate',
DATEADD(day,-1, CONVERT(char(6), DATEADD(MONTH,1,@Year),112)+'01') AS 'CalendarMonthEndDate'
END
Answer the question
In order to leave comments, you need to log in
I would do it better without a loop:
* I would make a table with years as a hierarchical query.
In oracle, it's like this:
select level from dual
connect by level < 2000
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question