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 2005 Forums
 Transact-SQL (2005)
 End of month date for 200909

Author  Topic 

jbphoenix
Yak Posting Veteran

68 Posts

Posted - 2009-10-15 : 12:02:08
I'm trying to find the end of the month date. My date field looks like this 200909, it only has the year and month. However my requirements are to have the end of the month date in the report. How would I get the end of the month date for any month? 20090930

Thanks

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-10-15 : 12:04:30
what is the data type of your date field ?


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

JCirocco
Constraint Violating Yak Guru

392 Posts

Posted - 2009-10-15 : 12:38:05
Add one month then subtract 1 day?

John
Go to Top of Page

scott_leseman
Starting Member

5 Posts

Posted - 2009-10-15 : 12:42:17
Assuming your date field is text with fixed width of 6 characters, something like this should work:

SELECT '200909' + CONVERT(CHAR(2), DAY(DATEADD(d,-1,DATEADD(m,1,CONVERT(DATETIME,RIGHT('200909',2) + '/1/' + LEFT('200909',4))))))

Replace the string '200909' with the name of your date field.

It takes your date and converts it to a datetime of 9/1/2009. Next it adds 1 month to that date and subtracts 1 day from that giving you the last day of the month. Then it takes the day portion of that date and adds it to the end of your current date field.

You could also simplify this by creating a user defined function and using variables.
Go to Top of Page

jbphoenix
Yak Posting Veteran

68 Posts

Posted - 2009-10-15 : 12:48:30
Thanks everyone for your help - this is what I came up with and it is working. I ended up going with this format mm/dd/yyyy

Convert(VARCHAR(10), DateAdd(dd,-1,DateAdd(m,1,convert(varchar(10),mnth)+'01')),101) as DateProcessed
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-10-16 : 04:06:17
quote:
Originally posted by jbphoenix

Thanks everyone for your help - this is what I came up with and it is working. I ended up going with this format mm/dd/yyyy

Convert(VARCHAR(10), DateAdd(dd,-1,DateAdd(m,1,convert(varchar(10),mnth)+'01')),101) as DateProcessed


You haven't answered to khtan's question

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2009-10-16 : 12:13:46
I posted a solution for this in the Script Library forum on the following topic:

Convert YYYYMM Integer Date to Datetime
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=134538



CODO ERGO SUM
Go to Top of Page
   

- Advertisement -