Share via

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.

Developer technologies | Transact-SQL
Developer technologies | Transact-SQL

A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.


2 answers

Sort by: Most helpful
  1. LiHongMSFT-4306 31,621 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

    Was this answer helpful?

    0 comments No comments

  2. 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

    Was this answer helpful?


Your answer

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