Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everyone,

I was working on stored procedure in SQL-SERVER 2008.

Where i trying hard to get 10th of every month.

Secondly, in the current month, i need to get previous month, means current month -1.

thnx
Posted
Updated 4-Mar-14 21:11pm
v2
Comments
Vedat Ozan Oner 5-Mar-14 3:21am    
see here: http://technet.microsoft.com/en-us/library/ms186724%28v=sql.105%29.aspx
Ankur\m/ 5-Mar-14 3:46am    
10th of every month means? Do you want the day that's there on 10th of every month?

Here's my Solution:

SQL
DECLARE @LowerDate DATE 
SET @LowerDate = GETDATE()

DECLARE @UpperLimit DATE
SET @UpperLimit = '20111231'

;WITH Firsts AS
(
    SELECT
        CONVERT(VARCHAR(10),DATEADD(DAY, -1 * DAY(@LowerDate) + 10, @LowerDate), 101) AS 'FirstOfMonth'

    UNION ALL

    SELECT
        CONVERT(VARCHAR(10),DATEADD(MONTH, 1, f.FirstOfMonth), 101) AS 'FirstOfMonth'
    FROM
        Firsts f
    WHERE
        CONVERT(VARCHAR(10),DATEADD(MONTH, 1, f.FirstOfMonth), 101)  <= @UpperLimit
)   
SELECT * 
FROM Firsts
 
Share this answer
 
v2
Hi,
If you want to get every month 10th date for one year use this.

SQL
Declare   @FromDate            VARCHAR(20)  ='2013-01-01'
     Declare   @ToDate            VARCHAR(20)  = '2013-12-12'
IF OBJECT_ID('tempdb..#TEMP_EveryWk_Snday') IS NOT NULL
    DROP TABLE #TEMP_EveryWk_Snday

 DECLARE @TOTALCount INT
    SET @FromDate = DATEADD(DAY,-1,@FromDate)
    Select  @TOTALCount= DATEDIFF(dd,@FromDate,@ToDate);--DATEDIFF(DD,@FromDate,@ToDate);


   WITH d AS
            (
              SELECT top (@TOTALCount) AllDays = DATEADD(DAY, ROW_NUMBER()
                OVER (ORDER BY object_id), REPLACE(@FromDate,'-',''))
              FROM sys.all_objects
            )


                 Select Distinct

 DATEADD(MONTH, DATEDIFF(MONTH,number, d.AllDays), 09)

FROM master..spt_values M  ,d


WHERE M.Type = 'P' and M.number between 1 and 12
       AND
        AllDays  >=@FromDate
   AND AllDays  <=@ToDate
     group by
    M.number
      ,d.AllDays
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900