Solved

sql server query?

Posted on 2016-11-15
6
26 Views
Last Modified: 2016-11-16
Why am I not picking up '2016-10-29' in this query


Declare @StartDt date
Declare @EndDt date
Set @StartDt  = '2016-10-08'


--print @StartDt

Set @EndDt = '2016-11-02'

WHILE @StartDt <= @EndDt
BEGIN

SELECT
CONVERT(INTEGER, CONVERT(CHAR(10),  (DATEADD(wk, 0, DATEADD(DAY, 0-DATEPART(WEEKDAY, @StartDt), DATEDIFF(dd, 0, @StartDt)))), 112)) AS YTDDateKey,
YEAR((DATEADD(wk, 0, DATEADD(DAY, 0-DATEPART(WEEKDAY, @StartDt), DATEDIFF(dd, 0, @StartDt))))) AS [Year],
(DATEADD(wk, 0, DATEADD(DAY, 0-DATEPART(WEEKDAY, @StartDt), DATEDIFF(dd, 0, @StartDt)))) as SatYTDDt, LoadDttm = getdate()



SET @StartDt = DATEADD(dd, 7, @StartDt)
END
0
Comment
Question by:elucero
6 Comments
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 41888955
Why am I not picking up '2016-10-29' in this query
you are...

Declare @StartDt date
Declare @EndDt date
Set @StartDt  = '2016-10-08'
Set @EndDt = '2016-11-02'

WHILE @StartDt <= @EndDt
BEGIN

print @StartDt 

SET @StartDt = DATEADD(dd, 7, @StartDt)
END 

Open in new window


you are getting the output such as:
2016-10-08
2016-10-15
2016-10-22
2016-10-29

Open in new window


For the query:
SELECT
CONVERT(INTEGER, CONVERT(CHAR(10),  (DATEADD(wk, 0, DATEADD(DAY, 0-DATEPART(WEEKDAY, @StartDt), DATEDIFF(dd, 0, @StartDt)))), 112)) AS YTDDateKey,
YEAR((DATEADD(wk, 0, DATEADD(DAY, 0-DATEPART(WEEKDAY, @StartDt), DATEDIFF(dd, 0, @StartDt))))) AS [Year],
(DATEADD(wk, 0, DATEADD(DAY, 0-DATEPART(WEEKDAY, @StartDt), DATEDIFF(dd, 0, @StartDt)))) as SatYTDDt, LoadDttm = getdate()

Open in new window

can you tell us what you intend to get here?
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 41888957
When I run your code on my SQL 2012 box @startdt='2016-10-08' is the first @startdt processed, but YTDDateKey=20161001.  

It would probably help if you could add some details as to what you're trying to accomplish here.
20161008.png
0
 
LVL 17

Accepted Solution

by:
Pawan Kumar Khowal earned 500 total points
ID: 41889005
try..Can you tell me the purpose of (DATEADD(wk, 0, DATEADD(DAY, 0-DATEPART(WEEKDAY, @StartDt), DATEDIFF(dd, 0, @StartDt)))) as SatYTDDt

I have modified all your columns. Now it is picking  '2016-10-29'  

Other than this I think you need below.. please check?

Declare @StartDt date 
Declare @EndDt date
Set @StartDt  = '2016-10-08'
Set @EndDt = '2016-11-02'

WHILE @StartDt <= @EndDt 
BEGIN

PRINT @StartDt

SELECT
CONCAT(YEAR(@StartDt), RIGHT(CONCAT('0', MONTH(@StartDt)),2), RIGHT(CONCAT('0', DAY(@StartDt)),2))AS YTDDateKey,
CAST(@StartDt AS DATETIME) AS [Year],
(DATEADD(wk, 0, DATEADD(DAY, 0-DATEPART(WEEKDAY, @StartDt), DATEDIFF(dd, 0, @StartDt)))) as SatYTDDt
, LoadDttm = getdate()

SET @StartDt = DATEADD(dd, 7, @StartDt)

END

Open in new window


Output


2016-10-08
YTDDateKey       Year                    SatYTDDt                LoadDttm
---------------- ----------------------- ----------------------- -----------------------
20161008         2016-10-08 00:00:00.000 2016-10-01 00:00:00.000 2016-11-16 07:59:01.773

(1 row(s) affected)

2016-10-15
YTDDateKey       Year                    SatYTDDt                LoadDttm
---------------- ----------------------- ----------------------- -----------------------
20161015         2016-10-15 00:00:00.000 2016-10-08 00:00:00.000 2016-11-16 07:59:01.773

(1 row(s) affected)

2016-10-22
YTDDateKey       Year                    SatYTDDt                LoadDttm
---------------- ----------------------- ----------------------- -----------------------
20161022         2016-10-22 00:00:00.000 2016-10-15 00:00:00.000 2016-11-16 07:59:01.773

(1 row(s) affected)

2016-10-29
YTDDateKey       Year                    SatYTDDt                LoadDttm
---------------- ----------------------- ----------------------- -----------------------
20161029         2016-10-29 00:00:00.000 2016-10-22 00:00:00.000 2016-11-16 07:59:01.773

(1 row(s) affected)
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 10

Expert Comment

by:HuaMinChen
ID: 41889195
Try
Declare @StartDt date 
Declare @EndDt date
Set @StartDt  = '2016-10-01'


--print @StartDt

Set @EndDt = '2016-11-02'

WHILE @StartDt <= @EndDt 
BEGIN
SET @StartDt = DATEADD(dd, 7, @StartDt)

SELECT
CONVERT(INTEGER, CONVERT(CHAR(10),  (DATEADD(wk, 0, DATEADD(DAY, 0-DATEPART(WEEKDAY, @StartDt), DATEDIFF(dd, 0, @StartDt)))), 112)) AS YTDDateKey,
YEAR((DATEADD(wk, 0, DATEADD(DAY, 0-DATEPART(WEEKDAY, @StartDt), DATEDIFF(dd, 0, @StartDt))))) AS [Year],
(DATEADD(wk, 0, DATEADD(DAY, 0-DATEPART(WEEKDAY, @StartDt), DATEDIFF(dd, 0, @StartDt)))) as SatYTDDt, LoadDttm = getdate()



END

Open in new window

0
 
LVL 45

Expert Comment

by:Vitor Montalvão
ID: 41889294
Why am I not picking up '2016-10-29' in this query
Tell us what you want to do with the query.

What's actually doing is processing the last week based in the current day and that's why '2016-10-29' isn't returned. When '2016-10-29' it's processing '2016-10-22' (one week before).
0
 

Author Closing Comment

by:elucero
ID: 41890095
Thanks!
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now