|
-
May 25th, 2016, 11:21 AM
#1
Thread Starter
Don't Panic!
[RESOLVED] Explain what is happening here... Please
Hi
Finally, some (possibly silly) people have let me get my mits on MS Sql Server databases.
Where, with only the experience of MS Access and examples (previously) from here and online, I get to wade in with big boots.
Ok so the basics are the same for querying, table structures are a little different and sub queries tend not to do as I thought. However ...
Yesterday I wanted to get a list of dates thirty (30) days in the past to now. But I don't want to create a table, at least not in the sense of writing; storing and populating it. So I searched online and found a chunk of code. After discussing here with a colleague I ended up with the SQL below.
But how does it work.?
I get the gist, start date, loop each day, it's just how it does it without an explicit loop...
Could some one please answer this really easy question??
Code:
with T(day) as
(
select DATEADD(dd,-30,DATEADD(dd, DATEDIFF(dd, 0, getdate()), 0)) as day
union all
select day + 1
from T
where day < DATEADD(dd, DATEDIFF(dd, 0, getdate()), 0)
)
select * from T
Feeling like a fly on the inside of a closed window (Thunk!)
If I post a lot, it is because I am bored at work! ;D Or stuck...
* Anything I post can be only my opinion. Advice etc is up to you to persue...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|