Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

sql server query?

Posted on 2016-11-15
6
Medium Priority
?
65 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 53

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 66

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 32

Accepted Solution

by:
Pawan Kumar earned 2000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 11

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 52

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

610 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