Solved

sql server query?

Posted on 2016-11-15
6
41 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 50

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 28

Accepted Solution

by:
Pawan Kumar 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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 47

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

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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…
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

785 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