T-sql related help needed.

Kritika arora 1 Reputation point
2022-09-09T17:37:55.87+00:00

I have a UDF where I am generating current date like this month but I want 36 month old data but as soon as I enter my 37 month it should delete the first month data.My UDF loads data in one of the fact table where I need to show only previous month data.How to achieve this.
I did get 36 month data but the second condition is still a question mark to me.

Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,637 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Kritika arora 1 Reputation point
    2022-09-09T22:06:14.647+00:00

    Suppose
    DATEADD(month,DATEDIFF(mm,0,DATEADD(mm,-36,GETDATE()))

    This gives me 36 month data in my UDF but need to get below requirement in my fact table
    Can you help me with a query which gives previous month data and at the same time delete current month data


  2. LiHongMSFT-4306 27,016 Reputation points
    2022-09-12T01:37:00.087+00:00

    Hi @Kritika arora

    Can you help me with a query which gives previous month data and at the same time delete current month data

    Not sure what you actually want. However, if it is a Table_Valued UDF, why not add a where filter to remove datas of current month.

    select <column_list>  
    from fn_UDF_Name()  
    where date_column < dateadd(month,datediff(month,0,getdate()),0)  
    

    Best regards,
    LiHong

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.