Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 using a query to return the next Sunday

Author  Topic 

dougancil
Posting Yak Master

217 Posts

Posted - 2010-10-25 : 10:59:31
I have the following query:

select
a.MyDate,
Sunday = dateadd(dd,((datediff(dd,'17530107',a.MyDate)/7)*7)+7,'17530107')
from
( -- Test Data
select MyDate = convert(datetime,'20100930')
) a

and what I'd like to do is instead of offering a datetime, I have a SQL field that I need to pull from with the date. Can anyone offer me a suggestion on how to do that?

What I see changing is the

select MyDate = [exceptions].exceptiondate.

I tried that though and I get an error:

The column prefix 'exceptions' does not match with a table name or alias name used in the query.

Thank you,

Doug

pk_bohra
Master Smack Fu Yak Hacker

1182 Posts

Posted - 2010-10-25 : 12:38:06
quote:


select MyDate = [exceptions].exceptiondate.

I tried that though and I get an error:

The column prefix 'exceptions' does not match with a table name or alias name used in the query.

Thank you,

Doug



Can you show us the query you have tried. May be a minor mistake which can be corrected.

Regards,
Bohra

I am here to learn from Masters and help new bees in learning.
Go to Top of Page

dougancil
Posting Yak Master

217 Posts

Posted - 2010-10-25 : 12:56:11
Bohra,

Here's the query I just tried:

select
a.MyDate,
Sunday = dateadd(dd,((datediff(dd,'17530107',a.MyDate)/7)*7)+7,'17530107')
from
( -- Test Data
select MyDate = [payroll].payrolldate
) a

which returns an error back to me:

The column prefix 'payroll' does not match with a table name or alias name used in the query.

I've also tried this query:

select
a.MyDate,
Sunday = dateadd(dd,((datediff(dd,'17530107',a.MyDate)/7)*7)+7,'17530107')
from
( -- Test Data
select MyDate = payrolldate
) a

which returns this error back to me:

Invalid column name 'payrolldate'.
Go to Top of Page

dougancil
Posting Yak Master

217 Posts

Posted - 2010-10-25 : 12:57:51
I apologize this question should be in the T-SQL forum.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-10-25 : 13:03:58
I've moved your topic and nuked your duplicate.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

dougancil
Posting Yak Master

217 Posts

Posted - 2010-10-25 : 15:02:26
Thanks Tara.
Go to Top of Page

pk_bohra
Master Smack Fu Yak Hacker

1182 Posts

Posted - 2010-10-26 : 01:30:48
quote:
Originally posted by dougancil


select
a.MyDate,
Sunday = dateadd(dd,((datediff(dd,'17530107',a.MyDate)/7)*7)+7,'17530107')
from
( -- Test Data
select MyDate = [payroll].payrolldate from payroll
) a


Go to Top of Page

dougancil
Posting Yak Master

217 Posts

Posted - 2010-10-26 : 10:45:43
Bohra,

I tried your query:

select
a.MyDate,
Sunday = dateadd(dd,((datediff(dd,'17530107',a.MyDate)/7)*7)+7,'17530107')
from
( -- Test Data
select MyDate payrolldate from payroll
) a

and got this error:

Server: Msg 207, Level 16, State 3, Line 1
Invalid column name 'MyDate'.
Server: Msg 207, Level 16, State 1, Line 1
Invalid column name 'MyDate'.
Server: Msg 207, Level 16, State 1, Line 1
Invalid column name 'MyDate'.
Go to Top of Page

dougancil
Posting Yak Master

217 Posts

Posted - 2010-10-26 : 13:50:16
Ok so with some help, here is what I have now.

select payrolldate,
Sunday = dateadd(dd, ((datediff(dd, '17530107', payrolldate)/7)*7)+7, '17530107')
from payroll

But what I want to make sure of is that when my query is run, that it picks the latest date in payrolldate to run against and not return the values back for all the entries. Can anyone help me think of a way to do this?

Thank you

Doug
Go to Top of Page

pk_bohra
Master Smack Fu Yak Hacker

1182 Posts

Posted - 2010-10-26 : 13:54:13
quote:
Originally posted by dougancil

Bohra,

I tried your query:

select
a.MyDate,
Sunday = dateadd(dd,((datediff(dd,'17530107',a.MyDate)/7)*7)+7,'17530107')
from
( -- Test Data
select MyDate payrolldate from payroll
) a

and got this error:

Server: Msg 207, Level 16, State 3, Line 1
Invalid column name 'MyDate'.
Server: Msg 207, Level 16, State 1, Line 1
Invalid column name 'MyDate'.
Server: Msg 207, Level 16, State 1, Line 1
Invalid column name 'MyDate'.





You missed the = equal to symbol. I just striked Payroll. and not = payroll.

Anyhow try the below example

Create table Payroll
(
payrolldate datetime )


Insert into Payroll
Select getdate()


select
a.MyDate,
Sunday = dateadd(dd,((datediff(dd,'17530107',a.MyDate)/7)*7)+7,'17530107')
from
( -- Test Data
select MyDate = payrolldate from payroll
) a
Go to Top of Page

dougancil
Posting Yak Master

217 Posts

Posted - 2010-10-26 : 14:19:00
Ok so I have a solution for this in place that I had forgotten about. I have a value in another field I can use as a "qualifier" for this data. I figured this out.

Thank you

Doug
Go to Top of Page

pk_bohra
Master Smack Fu Yak Hacker

1182 Posts

Posted - 2010-10-26 : 23:05:16
quote:
Originally posted by dougancil

Ok so I have a solution for this in place that I had forgotten about. I have a value in another field I can use as a "qualifier" for this data. I figured this out.

Thank you

Doug



You are welcome
Go to Top of Page
   

- Advertisement -