Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Function to get last Monday of current week

 
   Database Help (Home) -> Programming RSS
Next:  Can't save Procedure containing EXEC  
Author Message
gv

External


Since: Sep 13, 2006
Posts: 91



(Msg. 1) Posted: Wed Oct 29, 2008 4:16 pm
Post subject: Function to get last Monday of current week
Archived from groups: microsoft>public>sqlserver>programming (more info?)

Hi guys,

trying to create a function to return the last Monday of a current week?

any help would be great.


CREATE FUNCTION [cvt_LastMonday]()

RETURNS datetime

AS

BEGIN

DECLARE @CURRENTDATE DATETIME
SET @CURRENTDATE = CURRENT_TIMESTAMP

RETURN (DATEADD(wk, DATEDIFF(wk, 0,@CURRENTDATE),0))

END

thanks,
gv

 >> Stay informed about: Function to get last Monday of current week 
Back to top
Login to vote
Aaron Bertrand [SQL Serve

External


Since: Jan 10, 2008
Posts: 2166



(Msg. 2) Posted: Wed Oct 29, 2008 4:19 pm
Post subject: Re: Function to get last Monday of current week [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

> trying to create a function to return the last Monday of a current week?

This word problem is confusing to me. If it is Sunday, what do you want?
Tomorrow, or last Monday? If it is Monday, do you want today, or last
Monday? I think from Tuesday it is straight forward but you need better
specs.

 >> Stay informed about: Function to get last Monday of current week 
Back to top
Login to vote
gv

External


Since: Sep 13, 2006
Posts: 91



(Msg. 3) Posted: Wed Oct 29, 2008 4:26 pm
Post subject: Re: Function to get last Monday of current week [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

My apologies,

If Sunday last Monday
If Monday then that day

I think this works which I included:
select (DATEADD(wk, DATEDIFF(wk, 0,current_timestamp),0))

my problem is the function itself?

Thanks
gv

"Aaron Bertrand [SQL Server MVP]" wrote in message

>> trying to create a function to return the last Monday of a current week?
>
> This word problem is confusing to me. If it is Sunday, what do you want?
> Tomorrow, or last Monday? If it is Monday, do you want today, or last
> Monday? I think from Tuesday it is straight forward but you need better
> specs.
>
 >> Stay informed about: Function to get last Monday of current week 
Back to top
Login to vote
KH

External


Since: Feb 21, 2008
Posts: 12



(Msg. 4) Posted: Wed Oct 29, 2008 4:26 pm
Post subject: Re: Function to get last Monday of current week [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

You should look at SET DATEFIRST and @@DATEFIRST before doing date
calculations -- SET DATEFIRST sets the day that is considered the first day
of the week, and your calculations could be wrong if the setting is changed
or you use the function on a server w/a different setting.

Another option is using a calendar table -- google "calendar table" and you
should find an example. Then you could do a query like:

SELECT MAX([dt])
FROM calendar
WHERE [dt] <= GETDATE() AND [dw] = 'MON';

HTH


"gv" wrote:

> My apologies,
>
> If Sunday last Monday
> If Monday then that day
>
> I think this works which I included:
> select (DATEADD(wk, DATEDIFF(wk, 0,current_timestamp),0))
>
> my problem is the function itself?
>
> Thanks
> gv
>
> "Aaron Bertrand [SQL Server MVP]" wrote in message
>
> >> trying to create a function to return the last Monday of a current week?
> >
> > This word problem is confusing to me. If it is Sunday, what do you want?
> > Tomorrow, or last Monday? If it is Monday, do you want today, or last
> > Monday? I think from Tuesday it is straight forward but you need better
> > specs.
> >
>
>
>
 >> Stay informed about: Function to get last Monday of current week 
Back to top
Login to vote
Aaron Bertrand [SQL Serve

External


Since: Jan 10, 2008
Posts: 2166



(Msg. 5) Posted: Wed Oct 29, 2008 4:35 pm
Post subject: Re: Function to get last Monday of current week [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

> my problem is the function itself?

Can you elaborate? What is the "problem"?

I ran this and it seems to work (though I didn't change my clock to test
what you think should happen on a Sunday or Monday):

USE tempdb;
GO

CREATE FUNCTION dbo.cvt_LastMonday() -- always use schema prefix!!
RETURNS DATETIME
AS
BEGIN
-- why bother with a DECLARE for one use?
-- why use WK when WEEK is much easier to read?
RETURN (DATEADD(WEEK, DATEDIFF(WEEK, 0, CURRENT_TIMESTAMP),0));
END
GO

SELECT dbo.cvt_LastMonday();
GO
 >> Stay informed about: Function to get last Monday of current week 
Back to top
Login to vote
gv

External


Since: Sep 13, 2006
Posts: 91



(Msg. 6) Posted: Mon Nov 03, 2008 8:09 am
Post subject: Re: Function to get last Monday of current week [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Thanks for your help.

I get this error?

Server: Msg 443, Level 16, State 1, Procedure cvt_LastMonday, Line 8
Invalid use of 'getdate' within a function.

Thanks
gv



"Aaron Bertrand [SQL Server MVP]" wrote in message

>> my problem is the function itself?
>
> Can you elaborate? What is the "problem"?
>
> I ran this and it seems to work (though I didn't change my clock to test
> what you think should happen on a Sunday or Monday):
>
> USE tempdb;
> GO
>
> CREATE FUNCTION dbo.cvt_LastMonday() -- always use schema prefix!!
> RETURNS DATETIME
> AS
> BEGIN
> -- why bother with a DECLARE for one use?
> -- why use WK when WEEK is much easier to read?
> RETURN (DATEADD(WEEK, DATEDIFF(WEEK, 0, CURRENT_TIMESTAMP),0));
> END
> GO
>
> SELECT dbo.cvt_LastMonday();
> GO
>
 >> Stay informed about: Function to get last Monday of current week 
Back to top
Login to vote
Plamen Ratchev

External


Since: Aug 20, 2008
Posts: 672



(Msg. 7) Posted: Mon Nov 03, 2008 8:19 am
Post subject: Re: Function to get last Monday of current week [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Back to top
Login to vote
DBA 100

External


Since: Mar 10, 2010
Posts: 4



(Msg. 8) Posted: Wed Mar 10, 2010 8:19 am
Post subject: Some comment. [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

RETURN (DATEADD(wk, DATEDIFF(wk, 0,@CURRENTDATE),0))

this one doesn't return the date of last monday, @currentdate is today but this one only return the date of this monday.

Not the last one.




gv wrote:

Function to get last Monday of current week
29-Oct-08

Hi guys,

trying to create a function to return the last Monday of a current week?

any help would be great.


CREATE FUNCTION [cvt_LastMonday]()

RETURNS datetime

AS

BEGIN

DECLARE @CURRENTDATE DATETIME
SET @CURRENTDATE = CURRENT_TIMESTAMP

RETURN (DATEADD(wk, DATEDIFF(wk, 0,@CURRENTDATE),0))

END

thanks,
gv

Previous Posts In This Thread:

On Wednesday, October 29, 2008 4:16 PM
gv wrote:

Function to get last Monday of current week
Hi guys,

trying to create a function to return the last Monday of a current week?

any help would be great.


CREATE FUNCTION [cvt_LastMonday]()

RETURNS datetime

AS

BEGIN

DECLARE @CURRENTDATE DATETIME
SET @CURRENTDATE = CURRENT_TIMESTAMP

RETURN (DATEADD(wk, DATEDIFF(wk, 0,@CURRENTDATE),0))

END

thanks,
gv

On Wednesday, October 29, 2008 4:19 PM
Aaron Bertrand [SQL Server MVP] wrote:

This word problem is confusing to me. If it is Sunday, what do you want?
This word problem is confusing to me. If it is Sunday, what do you want?
Tomorrow, or last Monday? If it is Monday, do you want today, or last
Monday? I think from Tuesday it is straight forward but you need better
specs.

On Wednesday, October 29, 2008 4:26 PM
gv wrote:

My apologies,If Sunday last MondayIf Monday then that dayI think this works
My apologies,

If Sunday last Monday
If Monday then that day

I think this works which I included:
select (DATEADD(wk, DATEDIFF(wk, 0,current_timestamp),0))

my problem is the function itself?

Thanks
gv

"Aaron Bertrand [SQL Server MVP]" wrote in message


On Wednesday, October 29, 2008 4:35 PM
Aaron Bertrand [SQL Server MVP] wrote:

Can you elaborate? What is the "problem"?
Can you elaborate? What is the "problem"?

I ran this and it seems to work (though I didn't change my clock to test
what you think should happen on a Sunday or Monday):

USE tempdb;
GO

CREATE FUNCTION dbo.cvt_LastMonday() -- always use schema prefix!!
RETURNS DATETIME
AS
BEGIN
-- why bother with a DECLARE for one use?
-- why use WK when WEEK is much easier to read?
RETURN (DATEADD(WEEK, DATEDIFF(WEEK, 0, CURRENT_TIMESTAMP),0));
END
GO

SELECT dbo.cvt_LastMonday();
GO

On Wednesday, October 29, 2008 5:27 PM
K wrote:

Re: Function to get last Monday of current week
You should look at SET DATEFIRST and @@DATEFIRST before doing date
calculations -- SET DATEFIRST sets the day that is considered the first day
of the week, and your calculations could be wrong if the setting is changed
or you use the function on a server w/a different setting.

Another option is using a calendar table -- google "calendar table" and you
should find an example. Then you could do a query like:

SELECT MAX([dt])
FROM calendar
WHERE [dt] <= GETDATE() AND [dw] = 'MON';

HTH


"gv" wrote:

On Monday, November 03, 2008 8:09 AM
gv wrote:

Thanks for your help.I get this error?
Thanks for your help.

I get this error?

Server: Msg 443, Level 16, State 1, Procedure cvt_LastMonday, Line 8
Invalid use of 'getdate' within a function.

Thanks
gv

On Monday, November 03, 2008 8:19 AM
Plamen Ratchev wrote:

You can pass the current date as parameter to the
You can pass the current date as parameter to the function:
http://sqlserver2000.databases.aspfaq.com/how-do-i-use-getdate-within-...ser-def

--
Plamen Ratchev
http://www.SQLStudio.com


Submitted via EggHeadCafe - Software Developer Portal of Choice
How to display a Gravatar Image with 100 Percent Client Script Code
http://www.eggheadcafe.com/tutorials/aspnet/3c8a04cd-471e-48b7-8dcc-b0...c10ecb4
 >> Stay informed about: Function to get last Monday of current week 
Back to top
Login to vote
DBA 100

External


Since: Mar 10, 2010
Posts: 4



(Msg. 9) Posted: Wed Mar 10, 2010 8:22 am
Post subject: date return [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

The statement RETURN (DATEADD(wk, DATEDIFF(wk, 0,@CURRENTDATE),0))
doesn't return the date of last monday.

I get the value for ,@CURRENTDATE = 11 03 2010, and the whole thing return 08 mar 2010, which is THIS monday, not the last one.

DBA100.



gv wrote:

Function to get last Monday of current week
29-Oct-08

Hi guys,

trying to create a function to return the last Monday of a current week?

any help would be great.


CREATE FUNCTION [cvt_LastMonday]()

RETURNS datetime

AS

BEGIN

DECLARE @CURRENTDATE DATETIME
SET @CURRENTDATE = CURRENT_TIMESTAMP

RETURN (DATEADD(wk, DATEDIFF(wk, 0,@CURRENTDATE),0))

END

thanks,
gv

Previous Posts In This Thread:

On Wednesday, October 29, 2008 4:16 PM
gv wrote:

Function to get last Monday of current week
Hi guys,

trying to create a function to return the last Monday of a current week?

any help would be great.


CREATE FUNCTION [cvt_LastMonday]()

RETURNS datetime

AS

BEGIN

DECLARE @CURRENTDATE DATETIME
SET @CURRENTDATE = CURRENT_TIMESTAMP

RETURN (DATEADD(wk, DATEDIFF(wk, 0,@CURRENTDATE),0))

END

thanks,
gv

On Wednesday, October 29, 2008 4:19 PM
Aaron Bertrand [SQL Server MVP] wrote:

This word problem is confusing to me. If it is Sunday, what do you want?
This word problem is confusing to me. If it is Sunday, what do you want?
Tomorrow, or last Monday? If it is Monday, do you want today, or last
Monday? I think from Tuesday it is straight forward but you need better
specs.

On Wednesday, October 29, 2008 4:26 PM
gv wrote:

My apologies,If Sunday last MondayIf Monday then that dayI think this works
My apologies,

If Sunday last Monday
If Monday then that day

I think this works which I included:
select (DATEADD(wk, DATEDIFF(wk, 0,current_timestamp),0))

my problem is the function itself?

Thanks
gv

"Aaron Bertrand [SQL Server MVP]" wrote in message


On Wednesday, October 29, 2008 4:35 PM
Aaron Bertrand [SQL Server MVP] wrote:

Can you elaborate? What is the "problem"?
Can you elaborate? What is the "problem"?

I ran this and it seems to work (though I didn't change my clock to test
what you think should happen on a Sunday or Monday):

USE tempdb;
GO

CREATE FUNCTION dbo.cvt_LastMonday() -- always use schema prefix!!
RETURNS DATETIME
AS
BEGIN
-- why bother with a DECLARE for one use?
-- why use WK when WEEK is much easier to read?
RETURN (DATEADD(WEEK, DATEDIFF(WEEK, 0, CURRENT_TIMESTAMP),0));
END
GO

SELECT dbo.cvt_LastMonday();
GO

On Wednesday, October 29, 2008 5:27 PM
K wrote:

Re: Function to get last Monday of current week
You should look at SET DATEFIRST and @@DATEFIRST before doing date
calculations -- SET DATEFIRST sets the day that is considered the first day
of the week, and your calculations could be wrong if the setting is changed
or you use the function on a server w/a different setting.

Another option is using a calendar table -- google "calendar table" and you
should find an example. Then you could do a query like:

SELECT MAX([dt])
FROM calendar
WHERE [dt] <= GETDATE() AND [dw] = 'MON';

HTH


"gv" wrote:

On Monday, November 03, 2008 8:09 AM
gv wrote:

Thanks for your help.I get this error?
Thanks for your help.

I get this error?

Server: Msg 443, Level 16, State 1, Procedure cvt_LastMonday, Line 8
Invalid use of 'getdate' within a function.

Thanks
gv

On Monday, November 03, 2008 8:19 AM
Plamen Ratchev wrote:

You can pass the current date as parameter to the
You can pass the current date as parameter to the function:
http://sqlserver2000.databases.aspfaq.com/how-do-i-use-getdate-within-...ser-def

--
Plamen Ratchev
http://www.SQLStudio.com

On Wednesday, March 10, 2010 11:19 AM
DBA 100 wrote:

Some comment.
RETURN (DATEADD(wk, DATEDIFF(wk, 0,@CURRENTDATE),0))

this one doesn't return the date of last monday, @currentdate is today but this one only return the date of this monday.

Not the last one.


Submitted via EggHeadCafe - Software Developer Portal of Choice
More Fun with Fluent NHibernate Automapping
http://www.eggheadcafe.com/tutorials/aspnet/50aa9259-6dbb-4d16-9639-81...2171b00
 >> Stay informed about: Function to get last Monday of current week 
Back to top
Login to vote
DBA 100

External


Since: Mar 10, 2010
Posts: 4



(Msg. 10) Posted: Wed Mar 10, 2010 8:22 am
Post subject: statement. [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

The statement RETURN (DATEADD(wk, DATEDIFF(wk, 0,@CURRENTDATE),0))
doesn't return the date of last monday.

I get the value for ,@CURRENTDATE = 11 03 2010, and the whole thing return 08 mar 2010, which is THIS monday, not the last one.

DBA100.



gv wrote:

Function to get last Monday of current week
29-Oct-08

Hi guys,

trying to create a function to return the last Monday of a current week?

any help would be great.


CREATE FUNCTION [cvt_LastMonday]()

RETURNS datetime

AS

BEGIN

DECLARE @CURRENTDATE DATETIME
SET @CURRENTDATE = CURRENT_TIMESTAMP

RETURN (DATEADD(wk, DATEDIFF(wk, 0,@CURRENTDATE),0))

END

thanks,
gv

Previous Posts In This Thread:

On Wednesday, October 29, 2008 4:16 PM
gv wrote:

Function to get last Monday of current week
Hi guys,

trying to create a function to return the last Monday of a current week?

any help would be great.


CREATE FUNCTION [cvt_LastMonday]()

RETURNS datetime

AS

BEGIN

DECLARE @CURRENTDATE DATETIME
SET @CURRENTDATE = CURRENT_TIMESTAMP

RETURN (DATEADD(wk, DATEDIFF(wk, 0,@CURRENTDATE),0))

END

thanks,
gv

On Wednesday, October 29, 2008 4:19 PM
Aaron Bertrand [SQL Server MVP] wrote:

This word problem is confusing to me. If it is Sunday, what do you want?
This word problem is confusing to me. If it is Sunday, what do you want?
Tomorrow, or last Monday? If it is Monday, do you want today, or last
Monday? I think from Tuesday it is straight forward but you need better
specs.

On Wednesday, October 29, 2008 4:26 PM
gv wrote:

My apologies,If Sunday last MondayIf Monday then that dayI think this works
My apologies,

If Sunday last Monday
If Monday then that day

I think this works which I included:
select (DATEADD(wk, DATEDIFF(wk, 0,current_timestamp),0))

my problem is the function itself?

Thanks
gv

"Aaron Bertrand [SQL Server MVP]" wrote in message


On Wednesday, October 29, 2008 4:35 PM
Aaron Bertrand [SQL Server MVP] wrote:

Can you elaborate? What is the "problem"?
Can you elaborate? What is the "problem"?

I ran this and it seems to work (though I didn't change my clock to test
what you think should happen on a Sunday or Monday):

USE tempdb;
GO

CREATE FUNCTION dbo.cvt_LastMonday() -- always use schema prefix!!
RETURNS DATETIME
AS
BEGIN
-- why bother with a DECLARE for one use?
-- why use WK when WEEK is much easier to read?
RETURN (DATEADD(WEEK, DATEDIFF(WEEK, 0, CURRENT_TIMESTAMP),0));
END
GO

SELECT dbo.cvt_LastMonday();
GO

On Wednesday, October 29, 2008 5:27 PM
K wrote:

Re: Function to get last Monday of current week
You should look at SET DATEFIRST and @@DATEFIRST before doing date
calculations -- SET DATEFIRST sets the day that is considered the first day
of the week, and your calculations could be wrong if the setting is changed
or you use the function on a server w/a different setting.

Another option is using a calendar table -- google "calendar table" and you
should find an example. Then you could do a query like:

SELECT MAX([dt])
FROM calendar
WHERE [dt] <= GETDATE() AND [dw] = 'MON';

HTH


"gv" wrote:

On Monday, November 03, 2008 8:09 AM
gv wrote:

Thanks for your help.I get this error?
Thanks for your help.

I get this error?

Server: Msg 443, Level 16, State 1, Procedure cvt_LastMonday, Line 8
Invalid use of 'getdate' within a function.

Thanks
gv

On Monday, November 03, 2008 8:19 AM
Plamen Ratchev wrote:

You can pass the current date as parameter to the
You can pass the current date as parameter to the function:
http://sqlserver2000.databases.aspfaq.com/how-do-i-use-getdate-within-...ser-def

--
Plamen Ratchev
http://www.SQLStudio.com

On Wednesday, March 10, 2010 11:19 AM
DBA 100 wrote:

Some comment.
RETURN (DATEADD(wk, DATEDIFF(wk, 0,@CURRENTDATE),0))

this one doesn't return the date of last monday, @currentdate is today but this one only return the date of this monday.

Not the last one.

On Wednesday, March 10, 2010 11:22 AM
DBA 100 wrote:

date return
The statement RETURN (DATEADD(wk, DATEDIFF(wk, 0,@CURRENTDATE),0))
doesn't return the date of last monday.

I get the value for ,@CURRENTDATE = 11 03 2010, and the whole thing return 08 mar 2010, which is THIS monday, not the last one.

DBA100.


Submitted via EggHeadCafe - Software Developer Portal of Choice
Adding WCF Service References
http://www.eggheadcafe.com/tutorials/aspnet/a1647f10-9aa4-4b0c-bbd9-df...a9fab8e
 >> Stay informed about: Function to get last Monday of current week 
Back to top
Login to vote
DBA 100

External


Since: Mar 10, 2010
Posts: 4



(Msg. 11) Posted: Wed Mar 10, 2010 8:23 am
Post subject: see here. [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

The statement RETURN (DATEADD(wk, DATEDIFF(wk, 0,@CURRENTDATE),0))
doesn't return the date of last monday.

I get the value for ,@CURRENTDATE = 11 03 2010, and the whole thing return 08 mar 2010, which is THIS monday, not the last one.

DBA100.



gv wrote:

Function to get last Monday of current week
29-Oct-08

Hi guys,

trying to create a function to return the last Monday of a current week?

any help would be great.


CREATE FUNCTION [cvt_LastMonday]()

RETURNS datetime

AS

BEGIN

DECLARE @CURRENTDATE DATETIME
SET @CURRENTDATE = CURRENT_TIMESTAMP

RETURN (DATEADD(wk, DATEDIFF(wk, 0,@CURRENTDATE),0))

END

thanks,
gv

Previous Posts In This Thread:

On Wednesday, October 29, 2008 4:16 PM
gv wrote:

Function to get last Monday of current week
Hi guys,

trying to create a function to return the last Monday of a current week?

any help would be great.


CREATE FUNCTION [cvt_LastMonday]()

RETURNS datetime

AS

BEGIN

DECLARE @CURRENTDATE DATETIME
SET @CURRENTDATE = CURRENT_TIMESTAMP

RETURN (DATEADD(wk, DATEDIFF(wk, 0,@CURRENTDATE),0))

END

thanks,
gv

On Wednesday, October 29, 2008 4:19 PM
Aaron Bertrand [SQL Server MVP] wrote:

This word problem is confusing to me. If it is Sunday, what do you want?
This word problem is confusing to me. If it is Sunday, what do you want?
Tomorrow, or last Monday? If it is Monday, do you want today, or last
Monday? I think from Tuesday it is straight forward but you need better
specs.

On Wednesday, October 29, 2008 4:26 PM
gv wrote:

My apologies,If Sunday last MondayIf Monday then that dayI think this works
My apologies,

If Sunday last Monday
If Monday then that day

I think this works which I included:
select (DATEADD(wk, DATEDIFF(wk, 0,current_timestamp),0))

my problem is the function itself?

Thanks
gv

"Aaron Bertrand [SQL Server MVP]" wrote in message


On Wednesday, October 29, 2008 4:35 PM
Aaron Bertrand [SQL Server MVP] wrote:

Can you elaborate? What is the "problem"?
Can you elaborate? What is the "problem"?

I ran this and it seems to work (though I didn't change my clock to test
what you think should happen on a Sunday or Monday):

USE tempdb;
GO

CREATE FUNCTION dbo.cvt_LastMonday() -- always use schema prefix!!
RETURNS DATETIME
AS
BEGIN
-- why bother with a DECLARE for one use?
-- why use WK when WEEK is much easier to read?
RETURN (DATEADD(WEEK, DATEDIFF(WEEK, 0, CURRENT_TIMESTAMP),0));
END
GO

SELECT dbo.cvt_LastMonday();
GO

On Wednesday, October 29, 2008 5:27 PM
K wrote:

Re: Function to get last Monday of current week
You should look at SET DATEFIRST and @@DATEFIRST before doing date
calculations -- SET DATEFIRST sets the day that is considered the first day
of the week, and your calculations could be wrong if the setting is changed
or you use the function on a server w/a different setting.

Another option is using a calendar table -- google "calendar table" and you
should find an example. Then you could do a query like:

SELECT MAX([dt])
FROM calendar
WHERE [dt] <= GETDATE() AND [dw] = 'MON';

HTH


"gv" wrote:

On Monday, November 03, 2008 8:09 AM
gv wrote:

Thanks for your help.I get this error?
Thanks for your help.

I get this error?

Server: Msg 443, Level 16, State 1, Procedure cvt_LastMonday, Line 8
Invalid use of 'getdate' within a function.

Thanks
gv

On Monday, November 03, 2008 8:19 AM
Plamen Ratchev wrote:

You can pass the current date as parameter to the
You can pass the current date as parameter to the function:
http://sqlserver2000.databases.aspfaq.com/how-do-i-use-getdate-within-...ser-def

--
Plamen Ratchev
http://www.SQLStudio.com

On Wednesday, March 10, 2010 11:19 AM
DBA 100 wrote:

Some comment.
RETURN (DATEADD(wk, DATEDIFF(wk, 0,@CURRENTDATE),0))

this one doesn't return the date of last monday, @currentdate is today but this one only return the date of this monday.

Not the last one.

On Wednesday, March 10, 2010 11:22 AM
DBA 100 wrote:

date return
The statement RETURN (DATEADD(wk, DATEDIFF(wk, 0,@CURRENTDATE),0))
doesn't return the date of last monday.

I get the value for ,@CURRENTDATE = 11 03 2010, and the whole thing return 08 mar 2010, which is THIS monday, not the last one.

DBA100.

On Wednesday, March 10, 2010 11:22 AM
DBA 100 wrote:

statement.
The statement RETURN (DATEADD(wk, DATEDIFF(wk, 0,@CURRENTDATE),0))
doesn't return the date of last monday.

I get the value for ,@CURRENTDATE = 11 03 2010, and the whole thing return 08 mar 2010, which is THIS monday, not the last one.

DBA100.


Submitted via EggHeadCafe - Software Developer Portal of Choice
Making Silverlight Emulate Synchronous Requests
http://www.eggheadcafe.com/tutorials/aspnet/91f69224-3da5-4959-9901-c5...7c9b184
 >> Stay informed about: Function to get last Monday of current week 
Back to top
Login to vote
John Bell

External


Since: Jan 11, 2008
Posts: 157



(Msg. 12) Posted: Wed Mar 10, 2010 4:25 pm
Post subject: Re: statement. [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

I'd say that is last Monday but if you want the Monday before that try

RETURN (DATEADD(wk, DATEDIFF(wk, 0,CURRENT_TIMESTAMP)-1,0))


John


On Wed, 10 Mar 2010 08:22:36 -0800, DBA 100 wrote:

>The statement RETURN (DATEADD(wk, DATEDIFF(wk, 0,@CURRENTDATE),0))
> doesn't return the date of last monday.
>
>I get the value for ,@CURRENTDATE = 11 03 2010, and the whole thing return 08 mar 2010, which is THIS monday, not the last one.
>
>DBA100.
>
>
>
>gv wrote:
>
>Function to get last Monday of current week
>29-Oct-08
>
>Hi guys,
>
>trying to create a function to return the last Monday of a current week?
>
>any help would be great.
>
>
>CREATE FUNCTION [cvt_LastMonday]()
>
> RETURNS datetime
>
> AS
>
>BEGIN
>
> DECLARE @CURRENTDATE DATETIME
> SET @CURRENTDATE = CURRENT_TIMESTAMP
>
> RETURN (DATEADD(wk, DATEDIFF(wk, 0,@CURRENTDATE),0))
>
>END
>
>thanks,
>gv
>
>Previous Posts In This Thread:
>
>On Wednesday, October 29, 2008 4:16 PM
>gv wrote:
>
>Function to get last Monday of current week
>Hi guys,
>
>trying to create a function to return the last Monday of a current week?
>
>any help would be great.
>
>
>CREATE FUNCTION [cvt_LastMonday]()
>
> RETURNS datetime
>
> AS
>
>BEGIN
>
> DECLARE @CURRENTDATE DATETIME
> SET @CURRENTDATE = CURRENT_TIMESTAMP
>
> RETURN (DATEADD(wk, DATEDIFF(wk, 0,@CURRENTDATE),0))
>
>END
>
>thanks,
>gv
>
>On Wednesday, October 29, 2008 4:19 PM
>Aaron Bertrand [SQL Server MVP] wrote:
>
>This word problem is confusing to me. If it is Sunday, what do you want?
>This word problem is confusing to me. If it is Sunday, what do you want?
>Tomorrow, or last Monday? If it is Monday, do you want today, or last
>Monday? I think from Tuesday it is straight forward but you need better
>specs.
>
>On Wednesday, October 29, 2008 4:26 PM
>gv wrote:
>
>My apologies,If Sunday last MondayIf Monday then that dayI think this works
>My apologies,
>
>If Sunday last Monday
>If Monday then that day
>
>I think this works which I included:
> select (DATEADD(wk, DATEDIFF(wk, 0,current_timestamp),0))
>
>my problem is the function itself?
>
>Thanks
>gv
>
>"Aaron Bertrand [SQL Server MVP]" wrote in message
>
>
>On Wednesday, October 29, 2008 4:35 PM
>Aaron Bertrand [SQL Server MVP] wrote:
>
>Can you elaborate? What is the "problem"?
>Can you elaborate? What is the "problem"?
>
>I ran this and it seems to work (though I didn't change my clock to test
>what you think should happen on a Sunday or Monday):
>
>USE tempdb;
>GO
>
>CREATE FUNCTION dbo.cvt_LastMonday() -- always use schema prefix!!
>RETURNS DATETIME
>AS
>BEGIN
> -- why bother with a DECLARE for one use?
> -- why use WK when WEEK is much easier to read?
> RETURN (DATEADD(WEEK, DATEDIFF(WEEK, 0, CURRENT_TIMESTAMP),0));
>END
>GO
>
>SELECT dbo.cvt_LastMonday();
>GO
>
>On Wednesday, October 29, 2008 5:27 PM
>K wrote:
>
>Re: Function to get last Monday of current week
>You should look at SET DATEFIRST and @@DATEFIRST before doing date
>calculations -- SET DATEFIRST sets the day that is considered the first day
>of the week, and your calculations could be wrong if the setting is changed
>or you use the function on a server w/a different setting.
>
>Another option is using a calendar table -- google "calendar table" and you
>should find an example. Then you could do a query like:
>
>SELECT MAX([dt])
>FROM calendar
>WHERE [dt] <= GETDATE() AND [dw] = 'MON';
>
>HTH
>
>
>"gv" wrote:
>
>On Monday, November 03, 2008 8:09 AM
>gv wrote:
>
>Thanks for your help.I get this error?
>Thanks for your help.
>
>I get this error?
>
>Server: Msg 443, Level 16, State 1, Procedure cvt_LastMonday, Line 8
>Invalid use of 'getdate' within a function.
>
>Thanks
>gv
>
>On Monday, November 03, 2008 8:19 AM
>Plamen Ratchev wrote:
>
>You can pass the current date as parameter to the
>You can pass the current date as parameter to the function:
>http://sqlserver2000.databases.aspfaq.com/how-do-i-use-getdate-within-a-user-defined-function-udf.html
 >> Stay informed about: Function to get last Monday of current week 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Set first day of week to monday - Hi. I try this code: SET DATEFIRST 1 UPDATE dbo.tbl_log SET iweek= Datepart(ww,logdate)-- Exctract weeknumer into field I want Monday to be the first day of the week, but the code above does not help me out. For example October 8. is still caculated a...

Current Week and Prior week data in same Query - I need to generate a report that shows detail for territory, product on hand etc for Last week and prior week in separate columns. My code currently allows me to get the current week data, but I don't know how to add the data from the prior week in a ne...

Get Next Monday - I have a schedule table containing 2 rows like so: workday, name 4/26/2008, joe 4/27/2008, terry .... .... This table contains thousands of rows, one row for each day in the next few years. I need to somehow always return the next Monday date..

Pass Current Login/Password to COM in Function? - I have a function that calls out to an external COM object for decrypting data. The COM object has an Init() call that is required, and takes three parameters - the database name, a username, and a password. The way I see it, I have three options: 1) ...

Table that lists purchases made this week and last week - So I have a table that has entries as such Purchase Data Item 2007-1-10 9:00 Cat 2007-1-17 10:00 Cat 2007-1-18 12:05 Dog 2007-1-18 14:20 Cat 2007-1-18 16:10 Dog 2007-1-18 16:20 Cat So my output for..
   Database Help (Home) -> Programming All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]