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
 General SQL Server Forums
 New to SQL Server Programming
 Trim Date

Author  Topic 

ScottBot
Starting Member

16 Posts

Posted - 2011-03-03 : 09:40:56
Hi,

I was wondering if anyone knows a way to trim GetDate() value to show only the day of the month so for instance rather than having 31/03/2011
I get 31.

I would like to base a calculation on the days in a month but am having troubles. I can get it to display the last day of a month and convert it from a datetime value to varchar to use it in a calculation but I just cant get the days section only.

Thanks in advance

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-03-03 : 09:46:49
[code]
dateadd(day, datediff(day, 0, getdate()), 0)
[/code]


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

Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2011-03-03 : 09:48:07
From SQL Server 2008 Books Online:
Functions That Get Date and Time Parts:
http://msdn.microsoft.com/en-us/library/ms186724(v=SQL.100).aspx#GetDateandTimeParts






CODO ERGO SUM
Go to Top of Page

ScottBot
Starting Member

16 Posts

Posted - 2011-03-03 : 10:05:23
Thanks Michael

This is what I've used and it works a treat!

SELECT DATEPART(d, DATEADD(s, - 1, DATEADD(mm, DATEDIFF(m, 0, GETDATE()) + 1, 0))) AS daysInMonth
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2011-03-03 : 11:28:10
This is slightly simpler:
select day(dateadd(mm,datediff(m,-1,getdate()),-1)) as daysInMonth


CODO ERGO SUM
Go to Top of Page

ScottBot
Starting Member

16 Posts

Posted - 2011-03-03 : 11:53:43
Ah fabulous! thanks again for your help, time and knowledge sharing.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2011-03-03 : 15:47:18
Here? At SQL Team?

He must think you're cute.

Did he offer to buy you a margarita?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page
   

- Advertisement -