SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to get the first day of preceding month


How to get the first day of preceding month

Author
Message
manibad
manibad
SSC Veteran
SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)

Group: General Forum Members
Points: 245 Visits: 143
I need to get the first day of the followingmonth.How is it possible.I am unable to find a way to proceed

i.e my output should be like this
Scenario 1:
Input-2012/01/29
Output-2012/02/01

Scenario 2:
Input-2012/12/29
output-2013/01/01

I need to accumlate both the scenarios in same coding
Please guide me in solving this.

Thanks in advance
Phil Parkin
Phil Parkin
SSC-Forever
SSC-Forever (48K reputation)SSC-Forever (48K reputation)SSC-Forever (48K reputation)SSC-Forever (48K reputation)SSC-Forever (48K reputation)SSC-Forever (48K reputation)SSC-Forever (48K reputation)SSC-Forever (48K reputation)

Group: General Forum Members
Points: 48490 Visits: 21121
I can't really see what this has to do with SSIS, but here's one way.

declare @MyDate datetime

set @MyDate = '2012-12-29'

select DATEADD(dd, - (DAY(DATEADD(mm, 1, @MyDate )) - 1), DATEADD(mm, 1, @MyDate ))




Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.

Please surround any code or links you post with the appropriate IFCode formatting tags. It helps readability a lot.
Sachin4all
Sachin4all
SSC-Enthusiastic
SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)

Group: General Forum Members
Points: 163 Visits: 314
Here is an example query to do it

Declare @day date

set @day = '2012/01/29'

select @day

Select DAY(@day) as Noofdays

SELECT DATEADD(mm,1,DATEADD(dd,-(DAY(@day)-1),@day)) AS Day

Select DATEADD(yy,1,DATEADD(dd,-(DAY(@day)-1),@day)) as Day
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)

Group: General Forum Members
Points: 89686 Visits: 38933
Using T-SQL:



declare @MyDate datetime;
set @MyDate = '20121229';
select dateadd(mm, datediff(mm,cast('19000101' as datetime),@MyDate) + 1, cast('19000101' as datetime)), @MyDate;




Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
manibad
manibad
SSC Veteran
SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)

Group: General Forum Members
Points: 245 Visits: 143
I have to work on SSIS to get the first day of following month and the output should be as string value.

Input:getdate()
Output:Firstday of the following month
format of output is=yyyymmdd

Sorry for not providing the perfect information in the previous post.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search