Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


TSQL DATEADD Help


TSQL DATEADD Help

Author
Message
GBeezy
GBeezy
SSC-Enthusiastic
SSC-Enthusiastic (161 reputation)SSC-Enthusiastic (161 reputation)SSC-Enthusiastic (161 reputation)SSC-Enthusiastic (161 reputation)SSC-Enthusiastic (161 reputation)SSC-Enthusiastic (161 reputation)SSC-Enthusiastic (161 reputation)SSC-Enthusiastic (161 reputation)

Group: General Forum Members
Points: 161 Visits: 709
I have the following:

DECLARE @StartInsertedDate DATETIME

SET @StartInsertedDate = CAST(CONVERT(VARCHAR(10), DATEADD(MONTH, -18,GETDATE()) - 1, 101) AS DATETIME) --DATEADD(DAY, -1, DATEDIFF(DAY, 0, GETDATE()))

SELECT @StartInsertedDate

AND it returns 2011-11-16 00:00:00.000

I want it to return the 1st day of the month (2011-11-01 00:00:00.000 is what I want) and I can not get it to for the life of me.

Any help will be greatly appreciated.
GBeezy
GBeezy
SSC-Enthusiastic
SSC-Enthusiastic (161 reputation)SSC-Enthusiastic (161 reputation)SSC-Enthusiastic (161 reputation)SSC-Enthusiastic (161 reputation)SSC-Enthusiastic (161 reputation)SSC-Enthusiastic (161 reputation)SSC-Enthusiastic (161 reputation)SSC-Enthusiastic (161 reputation)

Group: General Forum Members
Points: 161 Visits: 709
I give up too easily and over complicate things, I believe I have what I need below:

SELECT DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) - 18, 0)

Thanks
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24285 Visits: 37993
GBeezy (5/17/2013)
I give up too easily and over complicate things, I believe I have what I need below:

SELECT DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) - 18, 0)

Thanks


Or this:

select datadd(month, datediff(month,0,getdate()), 0)

You may want to check out this:

http://www.sqlservercentral.com/blogs/lynnpettis/2009/03/25/some-common-date-routines/

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)
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