Home changing step in the loop T-SQL
Reply: 1

changing step in the loop T-SQL

Valeriy Voronkov
1#
Valeriy Voronkov Published in 2017-12-07 11:20:07Z

Hello. I have some trouble. I have a table with dates for 4 years. I need to run loop through it using a day value = 7, month value = 30 and years = 365. How can I change this value inside the loop?

WHILE @start_date <= @finish_date
    BEGIN
        -- SOME ALGORITHM
        SET @start_date = @start_date + 1
    END

I would like to get changing value @start1 = 7 + 1; @start1 = 30 + 1 and @start1 = 365 + 1. I think it's possible, but I haven't any idea (haven't any working idea at the moment)

I was trying to use CASE construction, but when I ran it - my DBeaver application was shut down immediately.

Leran2002
2#
Leran2002 Reply to 2017-12-08 05:01:33Z

I think you can use DATEADD with YEAR/MONTH/DAY

DECLARE @start_date date='20140101'
DECLARE @finish_date date='20160101'

WHILE @start_date <= @finish_date
BEGIN
    PRINT CONVERT(varchar(10),@start_date,120)
    SET @start_date = DATEADD(YEAR,1,@start_date)
    --SET @start_date = DATEADD(MONTH,1,@start_date)
    --SET @start_date = DATEADD(DAY,1,@start_date)
END
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.298397 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO