Hi,
I'm trying to use the code "DateAdd(day, datepart(dw, ReviewDate), ReviewDate )" to organize the ReviewDates by weeks. However, my code keeps grouping some items incorrectly. Since I don't know what is happening, I'll include all possible information.
-there is a date field that is stored by seconds since 1970, so I must do a convert
-I'm passing the query a start and end date to organize rows into weeks
question: In query results 4, why would the date selection cause some items that are less that minDate be selected? Why would "2005-02-05 11:34:35.000"
be classified as the week "2005-01-29"
here is the sql code:
===================
declare @startDate varchar(25)
declare @endDate varchar(25)
declare @ProductName varchar(250)
select @startDate = '1995/02/12'
select @endDate = '1995/02/19'
select @ProductName = 'myProduct'
declare @minDate varchar(25)
declare @maxDate varchar(25)
SELECT @minDate = convert(datetime,DateAdd(day, ( datepart(dw, @startDate)) , @startDate ),113) -- get the earliest monday in the date range
SELECT @maxDate = convert(datetime,DateAdd(day, ( datepart(dw, @endDate)) , @endDate ),113) -- get the latest monday in the date range
SELECT @minDate
SELECT @maxDate
SELECT count(DISTINCT LTRIM(RTRIM(myTable.myItem))) as [the_count],
convert(char(11),DateAdd(day,
datepart(dw, CONVERT(datetime, DATEADD(s, myTable.timeInSecs, '1970-01-01 00:00:00.000') , 121)),
CONVERT(datetime, DATEADD(s, myTable.timeInSecs, '1970-01-01 00:00:00.000') , 121)
),121) as [the_date]
FROM myTable, myOtherTable
WHERE
myOtherTable.ProductName= @ProductName
and CONVERT(datetime, DATEADD(s, myTable.timeInSecs, '1970-01-01 00:00:00.000') , 121) >= @minDate
and CONVERT(datetime, DATEADD(s, myTable.timeInSecs, '1970-01-01 00:00:00.000') , 121) <= @maxDate
GROUP BY convert(char(11),DateAdd(day,
datepart(dw, CONVERT(datetime, DATEADD(s, myTable.timeInSecs, '1970-01-01 00:00:00.000') , 121)),
CONVERT(datetime, DATEADD(s, myTable.timeInSecs, '1970-01-01 00:00:00.000') , 121)
),121)
select '======='
SELECT DISTINCT LTRIM(RTRIM(myTable.myItem)) as [the_count],
convert(char(11),DateAdd(day,
datepart(dw, CONVERT(datetime, DATEADD(s, myTable.timeInSecs, '1970-01-01 00:00:00.000') , 121)),
CONVERT(datetime, DATEADD(s, myTable.timeInSecs, '1970-01-01 00:00:00.000') , 121)
),121) as [the_week]
, CONVERT(datetime, DATEADD(s, myTable.timeInSecs, '1970-01-01 00:00:00.000') , 121) as [the_day]
FROM myTable, myOtherTable
WHERE
myOtherTable.ProductName = @ProductName
and CONVERT(datetime, DATEADD(s, myTable.timeInSecs, '1970-01-01 00:00:00.000') , 121) >= @minDate
and CONVERT(datetime, DATEADD(s, myTable.timeInSecs, '1970-01-01 00:00:00.000') , 121) <= @maxDate
===================
HERE IS THE RESULT:
query 1 ruturns:
Feb 5 2005 12:00AM
query 2 returns:
Feb 12 2005 12:00AM
query 3 returns:
[the_count] [the_date]
3 2005-01-29
318 2005-02-05
query 4 returns:
myprodA 2005-01-29 2005-02-05 11:34:35.000
myprodB 2005-01-29 2005-02-05 14:16:28.000
myprodC 2005-02-05 2005-02-06 23:06:26.000
myprodD 2005-02-05 2005-02-07 08:30:44.000
myprodE 2005-02-05 2005-02-07 08:39:54.000
I'm trying to use the code "DateAdd(day, datepart(dw, ReviewDate), ReviewDate )" to organize the ReviewDates by weeks. However, my code keeps grouping some items incorrectly. Since I don't know what is happening, I'll include all possible information.
-there is a date field that is stored by seconds since 1970, so I must do a convert
-I'm passing the query a start and end date to organize rows into weeks
question: In query results 4, why would the date selection cause some items that are less that minDate be selected? Why would "2005-02-05 11:34:35.000"
be classified as the week "2005-01-29"
here is the sql code:
===================
declare @startDate varchar(25)
declare @endDate varchar(25)
declare @ProductName varchar(250)
select @startDate = '1995/02/12'
select @endDate = '1995/02/19'
select @ProductName = 'myProduct'
declare @minDate varchar(25)
declare @maxDate varchar(25)
SELECT @minDate = convert(datetime,DateAdd(day, ( datepart(dw, @startDate)) , @startDate ),113) -- get the earliest monday in the date range
SELECT @maxDate = convert(datetime,DateAdd(day, ( datepart(dw, @endDate)) , @endDate ),113) -- get the latest monday in the date range
SELECT @minDate
SELECT @maxDate
SELECT count(DISTINCT LTRIM(RTRIM(myTable.myItem))) as [the_count],
convert(char(11),DateAdd(day,
datepart(dw, CONVERT(datetime, DATEADD(s, myTable.timeInSecs, '1970-01-01 00:00:00.000') , 121)),
CONVERT(datetime, DATEADD(s, myTable.timeInSecs, '1970-01-01 00:00:00.000') , 121)
),121) as [the_date]
FROM myTable, myOtherTable
WHERE
myOtherTable.ProductName= @ProductName
and CONVERT(datetime, DATEADD(s, myTable.timeInSecs, '1970-01-01 00:00:00.000') , 121) >= @minDate
and CONVERT(datetime, DATEADD(s, myTable.timeInSecs, '1970-01-01 00:00:00.000') , 121) <= @maxDate
GROUP BY convert(char(11),DateAdd(day,
datepart(dw, CONVERT(datetime, DATEADD(s, myTable.timeInSecs, '1970-01-01 00:00:00.000') , 121)),
CONVERT(datetime, DATEADD(s, myTable.timeInSecs, '1970-01-01 00:00:00.000') , 121)
),121)
select '======='
SELECT DISTINCT LTRIM(RTRIM(myTable.myItem)) as [the_count],
convert(char(11),DateAdd(day,
datepart(dw, CONVERT(datetime, DATEADD(s, myTable.timeInSecs, '1970-01-01 00:00:00.000') , 121)),
CONVERT(datetime, DATEADD(s, myTable.timeInSecs, '1970-01-01 00:00:00.000') , 121)
),121) as [the_week]
, CONVERT(datetime, DATEADD(s, myTable.timeInSecs, '1970-01-01 00:00:00.000') , 121) as [the_day]
FROM myTable, myOtherTable
WHERE
myOtherTable.ProductName = @ProductName
and CONVERT(datetime, DATEADD(s, myTable.timeInSecs, '1970-01-01 00:00:00.000') , 121) >= @minDate
and CONVERT(datetime, DATEADD(s, myTable.timeInSecs, '1970-01-01 00:00:00.000') , 121) <= @maxDate
===================
HERE IS THE RESULT:
query 1 ruturns:
Feb 5 2005 12:00AM
query 2 returns:
Feb 12 2005 12:00AM
query 3 returns:
[the_count] [the_date]
3 2005-01-29
318 2005-02-05
query 4 returns:
myprodA 2005-01-29 2005-02-05 11:34:35.000
myprodB 2005-01-29 2005-02-05 14:16:28.000
myprodC 2005-02-05 2005-02-06 23:06:26.000
myprodD 2005-02-05 2005-02-07 08:30:44.000
myprodE 2005-02-05 2005-02-07 08:39:54.000