Forum Discussion

Nathan_H_'s avatar
Nathan_H_
Accelerator I
2 years ago
Solved

C# SQL Date Query

Good day all,

I am trying to create a dynamic report to produce the time it take between starting an activity and ending an activity.  I can get the information I am looking for by using SQL, but it will not limit the returns based on dates.  I have tried a few different ways to do this, but nothing is working for me.  Thanks for any help you can provide.

StringBuilder sql = new StringBuilder();
    sql.AppendLine(
        "Select " +
            "WP.name AS change, " +
            "WP.state AS status, " +
            "Act.name AS activity, " +
            "ClosedBy.keyed_name AS closed_by, " +
            "Act_Assign.created_on, " +
            "Act_Assign.closed_on, " +
            "Act_Assign.modified_on, " +
            "GETDATE() AS today, " +
            "DATEDIFF (d, [Act_Assign].created_on, [Act_Assign].closed_on) + 1 AS date_diff " +
        
        "FROM [Workflow_Process] AS WP " +
        
        "INNER JOIN [Workflow_Process_Activity] AS WPA ON WPA.source_id = WP.id " +
        "INNER JOIN [Activity] AS Act ON Act.id = WPA.related_id " +
        "INNER JOIN [Activity_Assignment] AS Act_Assign ON Act_Assign.source_id = Act.id " +
        "INNER JOIN [sm_Change] as Change ON Change.item_number = WP.name " +
        "LEFT OUTER JOIN [User] AS ClosedBy ON ClosedBy.id = Act_Assign.closed_by " +
        
        "WHERE WP.name = 'CR-1091' OR WP.name = 'CR-1169' " + // AND Act.name = 'ECR Approval' 
        "AND Act_Assign.modified_on BETWEEN '2023-08-01' AND '2023-08-02' " +
        
        "ORDER BY WP.name, Act_Assign.created_on ASC, Act_Assign.closed_on DESC "
    );

 

  • Edit: We are all blind! 

    Take a look at my picture again. Do you notice something? I have added your BETWEEN date filter. Does the filter work? Yes...but NO! It´s uses the filter but with the date in a in wrong format, which can lead to all kind of wrong results. [I cannot remember ever having similar problems before whenever using dates....]

    Solution in my case: WHERE  (Act_Assign.MODIFIED_ON BETWEEN '20230801' AND '20230802')

    As alternative, something with convert(datetime,...) should also do the job. 

7 Replies

  • Does the query deliver correct result in SSMS?

    Do you get too much results, or no results at all?

    If you get no results, your date format is probably not recognized by Innovator and you need to convert it:

     link 

    If you get too much results, check your WHERE query. You have two filters for name. But the second maybe connects to the AND for the date, so you maybe need more brackets.

    • Nathan_H_'s avatar
      Nathan_H_
      Accelerator I

      Angela,

      I do get results.  The results indicate that the Act_Assign.modified_on BETWEEN '2023-08-01' AND '2023-08-02' is being ignored and everything is returned.  If I try to put the date in a format of mm/dd/yy the system will give an error of incorrect date format.  I agree that the mm/dd/yy format is stupid.  The filters for the name are to limit the returns during testing.  The end product will not have those filters.  What is SSMS?  Thanks.

      • angela's avatar
        angela
        Catalyst II

        SSMS ist just the MS SQL Server Management Studio. I assumed you used it to build the query.

        Out of interest I made a quick test of the query. I had to remove one table that I don´t have, but the rest worked (in SSMS).

        Is your ORDER BY part also ignored?