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

Finding the end of month.

 
   Database Forums (Home) -> MSEQ RSS
Next:  selecting null with an iif expression in criteria..  
Author Message
jmillerWV

External


Since: Sep 02, 2005
Posts: 15



(Msg. 1) Posted: Tue Oct 07, 2008 9:22 am
Post subject: Finding the end of month.
Archived from groups: microsoft>public>sqlserver>mseq (more info?)

I am just learning MS SQL and have run onto a problem I can't find an answer
to. Is there a function to get the last day of a month in MS SQL? I am using
a "Sam's Learn SQL in 21 days" book and the SQL the author is using is not MS
SQL. He uses a function "Last_Day(field name)" ,where the "field name" is a
date field in a table. I have checked to Knowledge Base and can find nothing
there. Thanks in advance for your assistance. If you know of a better book
please let me know.

 >> Stay informed about: Finding the end of month. 
Back to top
Login to vote
Hugo Kornelis

External


Since: Jan 31, 2006
Posts: 335



(Msg. 2) Posted: Tue Oct 07, 2008 6:25 pm
Post subject: Re: Finding the end of month. [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Tue, 7 Oct 2008 09:22:01 -0700, jmillerWV wrote:

>I am just learning MS SQL and have run onto a problem I can't find an answer
>to. Is there a function to get the last day of a month in MS SQL? I am using
>a "Sam's Learn SQL in 21 days" book and the SQL the author is using is not MS
>SQL. He uses a function "Last_Day(field name)" ,where the "field name" is a
>date field in a table. I have checked to Knowledge Base and can find nothing
>there. Thanks in advance for your assistance. If you know of a better book
>please let me know.
>

Hi jmillerWV,

There is no single standard function to return the last day of a month,
but it's easy to calculate. The code below finds the last day of the
current month; you can replace CURRENT_TIMESTAMP with a variable or a
column name (when used in a query) or even an expression that results in
a datetime (or date when you're on SQL Server 2008) data type.

SELECT DATEADD(month,
DATEDIFF(month, '20000101', CURRENT_TIMESTAMP),
'20000131');

The trick is to calculate the number of months since the first day of
some "baseline" month, then add that number of month to the last day of
the same baseline month. I used january 2000 as the baseline, but you
can substitute any 31-day month.

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis

 >> Stay informed about: Finding the end of month. 
Back to top
Login to vote
jmillerWV

External


Since: Sep 02, 2005
Posts: 15



(Msg. 3) Posted: Wed Oct 08, 2008 6:01 am
Post subject: Re: Finding the end of month. [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Hugo,
Thanks for the timely response. I do appreciate it.

Have a wonderful day.

jmillerWV

"Hugo Kornelis" wrote:

> On Tue, 7 Oct 2008 09:22:01 -0700, jmillerWV wrote:
>
> >I am just learning MS SQL and have run onto a problem I can't find an answer
> >to. Is there a function to get the last day of a month in MS SQL? I am using
> >a "Sam's Learn SQL in 21 days" book and the SQL the author is using is not MS
> >SQL. He uses a function "Last_Day(field name)" ,where the "field name" is a
> >date field in a table. I have checked to Knowledge Base and can find nothing
> >there. Thanks in advance for your assistance. If you know of a better book
> >please let me know.
> >
>
> Hi jmillerWV,
>
> There is no single standard function to return the last day of a month,
> but it's easy to calculate. The code below finds the last day of the
> current month; you can replace CURRENT_TIMESTAMP with a variable or a
> column name (when used in a query) or even an expression that results in
> a datetime (or date when you're on SQL Server 2008) data type.
>
> SELECT DATEADD(month,
> DATEDIFF(month, '20000101', CURRENT_TIMESTAMP),
> '20000131');
>
> The trick is to calculate the number of months since the first day of
> some "baseline" month, then add that number of month to the last day of
> the same baseline month. I used january 2000 as the baseline, but you
> can substitute any 31-day month.
>
> --
> Hugo Kornelis, SQL Server MVP
> My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
>
 >> Stay informed about: Finding the end of month. 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Finding the Shift for current hour - I have a table which stores the shift timing like this: ShiftId StartTime EndTime WeekDay Shift 1 6:00:00 AM 6:00:00 PM 7 Shift A 2 6:00:00 PM 6:00:00 AM 7 Shift B 4 6:00:00 AM 6:00:00 PM 112 Shift C 8 6:00:00 AM 6:00:00 PM 112 Shift D....

EXEC (select... ) problem Help! - How can I get this to work? declare @WkEmpID declare @sql varchar(1000) set @sql = 'select distinct @WkEmpID = EmpID from Employee' exec (@sql) Now this is a simplified version of a more comples query which is forcing me to use this method rather than...

Query returning multiple rows - Hi, I have a query that returns back rows that have multiple entries for a given UniqueID. I want the query to be expanded so that it then only returns 1 of the multiple rows based on the maximum date. Thefore, my return set looks like this: ..

Having a stored procedure copy tables & also preserve inde.. - Hello, I created a stored procedure that renames a table to OLD_xxxxx and replaces that table with another (copy) that resides on a different database. I pull the tablename names through the use of a cursor table and construct a SELECT INTO statement a...

GROUP BY's on 3 tables in one SELECT? - Hi there, I'd like to ask you for help with following: having 3 tables: T1 (Person_ID, Product_ID, Costs) T2 (Person_ID, Product_ID, Balancies) T3 (Product_ID, Product_Type) I have simple GROUP BY query: SELECT T1.Person_ID, T1.Product_ID, MAX(Costs)..
   Database Forums (Home) -> MSEQ 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 ]