Need to write query for Date logic

ramu

I have a below query which have a date filter like EST_PICK_DATE between '2015-02-01' and '2015-06-01', where the logic is EST_PICK_DATE should be 3 months from the current month and 1st date of next month. I.E for current month MAY, EST_PICK_DATE should be between '2015-02-01' and '2015-06-01'.
I need to write below query dynamically.
In below query i have hardcoded the values '2015-02-01' and '2015-06-01', but it should take dynamically.
How to achieve this? I am using this query in SSIS package, So Shall i do in SQL level or we should implement this logic in package? If yes, How?

INSERT INTO STG_Open_Orders (Div_Code, net_price, gross_price)
SELECT ord.DIV_CODE AS Div_Code, ord_l.NET_PRICE AS net_price, ord_l.gross_price AS gross_price, 
FROM ORD ord inner join ORD_L ord_l ONord.ORD_ID=ord_l.ORD_ID
WHERE ord_l.EST_PICK_DATE BETWEEN '2015-02-01' AND'2015-06-01'
Pravin Deshmukh

Try this

INSERT INTO STG_Open_Orders (Div_Code, net_price, gross_price)
SELECT ord.DIV_CODE AS Div_Code, ord_l.NET_PRICE AS net_price, ord_l.gross_price AS gross_price, 
FROM ORD ord inner join ORD_L ord_l ONord.ORD_ID=ord_l.ORD_ID
WHERE ord_l.EST_PICK_DATE BETWEEN DATEADD(m, DATEDIFF(m, 0,DATEADD(month,-3,getdate())), 0) AND DATEADD(m, DATEDIFF(m, 0,DATEADD(month,1,getdate())), 0)

here are your start date and end date function

SELECT DATEADD(m, DATEDIFF(m, 0,DATEADD(month,-3,getdate())), 0)
2015-02-01 00:00:00.000

SELECT DATEADD(m, DATEDIFF(m, 0,DATEADD(month,1,getdate())), 0)
2015-06-01 00:00:00.000

For Specific date :

Declare @YourDate as date = '6/3/2015' -- note the format is mm/dd/yyyy
SELECT CAST(DATEADD(m, DATEDIFF(m, 0,DATEADD(month,-3,@YourDate)), 0) as date)
SELECT CAST(DATEADD(m, DATEDIFF(m, 0,DATEADD(month,1,@YourDate)), 0) as date)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Need to write notes to date

From Dev

Need help to write MongoDB Query

From Dev

need help to filter data for a specific year in a table however date defined as varchar. how to write my join query?

From Dev

Need to write a query in mysql to find the following

From Dev

Need help to write a frequent query in Laravel 3

From Dev

Need Better SQL Query And Datstage Logic(Using Only Files)

From Dev

Where can I write the after query logic in RetrieveAPIView?

From Dev

SQL query - need to add most recent date

From Dev

Need Oracle sql query for grouping the date

From Dev

Oracle SQL query Logic - Group by based on Date Difference

From Dev

How to write To and From date in a MySql Query method?

From Dev

Write a SQL Query to find year from date

From Dev

I need to write a query which repeats the rows by incrementing the values

From Dev

Why need @Query when I can write a method in Spring JPA

From Dev

Need a logic in to get elements

From Dev

need to find logic in calc

From Dev

Need assistance with boolean logic

From Dev

In lotus notes script i need complete logic for subtracting thirty days from current date

From Dev

I need a query multiple columns each date in same table

From Dev

Simplest way to write this logic

From Dev

Why in all tutorials write named query in the entity class. It's the logic part

From Dev

how to write Mysql query to get data from database based on date?

From Dev

How to write date range query in Nest ElasticSearch client?

From Dev

How to write a query in Google Spreadsheet that returns date >= today?

From Dev

How can I write a query to show 'breaks' in date ranges?

From Dev

How to write a query to display the mobiles whose manufacturing date is specified as 2010?

From Dev

Date Difference Logic in LINQ

From Dev

SQL Date Logic Clause

From Dev

Generating Date Logic for UIPickerView

Related Related

  1. 1

    Need to write notes to date

  2. 2

    Need help to write MongoDB Query

  3. 3

    need help to filter data for a specific year in a table however date defined as varchar. how to write my join query?

  4. 4

    Need to write a query in mysql to find the following

  5. 5

    Need help to write a frequent query in Laravel 3

  6. 6

    Need Better SQL Query And Datstage Logic(Using Only Files)

  7. 7

    Where can I write the after query logic in RetrieveAPIView?

  8. 8

    SQL query - need to add most recent date

  9. 9

    Need Oracle sql query for grouping the date

  10. 10

    Oracle SQL query Logic - Group by based on Date Difference

  11. 11

    How to write To and From date in a MySql Query method?

  12. 12

    Write a SQL Query to find year from date

  13. 13

    I need to write a query which repeats the rows by incrementing the values

  14. 14

    Why need @Query when I can write a method in Spring JPA

  15. 15

    Need a logic in to get elements

  16. 16

    need to find logic in calc

  17. 17

    Need assistance with boolean logic

  18. 18

    In lotus notes script i need complete logic for subtracting thirty days from current date

  19. 19

    I need a query multiple columns each date in same table

  20. 20

    Simplest way to write this logic

  21. 21

    Why in all tutorials write named query in the entity class. It's the logic part

  22. 22

    how to write Mysql query to get data from database based on date?

  23. 23

    How to write date range query in Nest ElasticSearch client?

  24. 24

    How to write a query in Google Spreadsheet that returns date >= today?

  25. 25

    How can I write a query to show 'breaks' in date ranges?

  26. 26

    How to write a query to display the mobiles whose manufacturing date is specified as 2010?

  27. 27

    Date Difference Logic in LINQ

  28. 28

    SQL Date Logic Clause

  29. 29

    Generating Date Logic for UIPickerView

HotTag

Archive