Solved

Get Year, Month, beginning of next month

Posted on 2015-02-13
9
104 Views
Last Modified: 2015-02-13
I want to pass a string sample '02/01/2015' or any day of that month and get the following.

@enddate should return '03/01/2015'  (the beginning of next month)
@month   should return '02'
@year      should return '2015'

Declare @EndDate as varchar(12)
declare @month	  as varchar(2)
declare @year	  as varchar(4)



Set @EndDate 		= @startdate  > should be '03/01/2015' (the first day of next month)
Set @Month		        = @startdate  > should be '02'
Set @Year		        = @startdate  > should be '2015'


select @enddate
select @month 
select @year

select MONTH('02/01/2015')  < how can I return '02' instead of '2'
select YEAR('02/01/2015')    < this is ok   '2015'

Open in new window

0
Comment
Question by:VBdotnet2005
  • 6
  • 3
9 Comments
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40609063
Set @EndDate  = DATEADD(dd, - (DAY(DATEADD(mm, 1, getdate() )) - 1), DATEADD(mm, 1, getdate() ))
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40609066
I'm sorry but WHY are you using varchar values?
0
 

Author Comment

by:VBdotnet2005
ID: 40609077
Hi PorletPaul,

It is fine. I am just test a small tmp table.
How about Month something like this ?
'0' + convert(char(10), DATEPART(MM, @startdate))   > I want it to return 02 instead of 2
0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
ID: 40609078
untested
declare @EndDate as varchar(12)
declare @month	  as varchar(2)
declare @year	  as varchar(4)



Set @EndDate  = CONVERT(varchar(12), DATEADD(dd, - (DAY(DATEADD(mm, 1, getdate() )) - 1), DATEADD(mm, 1, getdate() )), 101)
Set @Month    = CONVERT(varchar(2), getdate() , 101)
Set @Year     = CONVERT(varchar(4), getdate() , 111)


select @enddate
select @month 
select @year

Open in new window

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:VBdotnet2005
ID: 40609084
The beginning of next month '03/01/2015', not the first day of the month that is passing.

Set @EndDate  = DATEADD(dd, - (DAY(DATEADD(mm, 1, getdate() )) - 1), DATEADD(mm, 1, getdate() ))
0
 

Author Comment

by:VBdotnet2005
ID: 40609088
it is all good. Thank you very much
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40609090
?
@enddate should return '03/01/2015'  (the beginning of next month)

it returns see COLUMN_0
|   COLUMN_0 | COLUMN_1 | COLUMN_2 |
|------------|----------|----------|
| 03/01/2015 |       02 |     2015 |

Open in new window

0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40609092
OK
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40609097
I do hope you are NOT using @enddate like this:

select * from some_table where a_datetime_field < @enddate;

If you are, it should be a datetime or date datatype

and don't ever assume MM/DD/YYYYY is "always safe to use" because it isn't
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now