Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Add week number column in query results from existing date column

I am just learning SQL, using SQL Server Management Studio.

The current query is shown below and I am struggling with trying to add a column to my results query, where it converts the PRODUCTION_DATE into fiscal week.

SELECT        
    ORDER_QTY, SKU, INVOICE_NUMBER, CUSTOMER_NUMBER, ROUTE, 
    ALLOCATED_QTY, SHORTED_QTY, PRODUCTION_DATE
FROM          
    [DATEBASE_NAME].[XYZ].[ORDERS]
WHERE 
    [PRODUCTION_DATE] >= DATEADD(day, -300, GETDATE())  
    AND [PRODUCTION_DATE] <= GETDATE()

I believe DATEPART is part of the function, I just don’t know how to execute. End goal would be a column in the results that just returns the fiscal week along with all other data. The little query above returns my data proper now, I am just trying to get that column in.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thanks for looking.

>Solution :

As you have already mentioned about DATEPART;

(And you were almost there !!)

Here is the modified query

SELECT        
    ORDER_QTY, SKU, INVOICE_NUMBER, CUSTOMER_NUMBER, ROUTE, 
    ALLOCATED_QTY, SHORTED_QTY, PRODUCTION_DATE,
    DATEPART(wk, PRODUCTION_DATE) AS FISCAL_WEEK
FROM          
    [DATEBASE_NAME].[XYZ].[ORDERS]
WHERE 
    [PRODUCTION_DATE] >= DATEADD(day, -300, GETDATE())  
    AND [PRODUCTION_DATE] <= GETDATE();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading