Forum Discussion

Nathan_H_'s avatar
Nathan_H_
Ideator I
3 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.

[embed:dc8ab71f-3b98-42d9-b0f6-e21e02a0f8e2:ed95bac7-b9b0-4d83-9998-62b7a1797922:type=text&text=StringBuilder%20sql%20%3D%20new%20StringBuilder%28%29%3B%0A%20%20%20%20sql.AppendLine%28%0A%20%20%20%20%20%20%20%20%22Select%20%22%20%2B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22WP.name%20AS%20change%2C%20%22%20%2B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22WP.state%20AS%20status%2C%20%22%20%2B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Act.name%20AS%20activity%2C%20%22%20%2B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22ClosedBy.keyed_name%20AS%20closed_by%2C%20%22%20%2B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Act_Assign.created_on%2C%20%22%20%2B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Act_Assign.closed_on%2C%20%22%20%2B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Act_Assign.modified_on%2C%20%22%20%2B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22GETDATE%28%29%20AS%20today%2C%20%22%20%2B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22DATEDIFF%20%28d%2C%20%5BAct_Assign%5D.created_on%2C%20%5BAct_Assign%5D.closed_on%29%20%2B%201%20AS%20date_diff%20%22%20%2B%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%22FROM%20%5BWorkflow_Process%5D%20AS%20WP%20%22%20%2B%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%22INNER%20JOIN%20%5BWorkflow_Process_Activity%5D%20AS%20WPA%20ON%20WPA.source_id%20%3D%20WP.id%20%22%20%2B%0A%20%20%20%20%20%20%20%20%22INNER%20JOIN%20%5BActivity%5D%20AS%20Act%20ON%20Act.id%20%3D%20WPA.related_id%20%22%20%2B%0A%20%20%20%20%20%20%20%20%22INNER%20JOIN%20%5BActivity_Assignment%5D%20AS%20Act_Assign%20ON%20Act_Assign.source_id%20%3D%20Act.id%20%22%20%2B%0A%20%20%20%20%20%20%20%20%22INNER%20JOIN%20%5Bsm_Change%5D%20as%20Change%20ON%20Change.item_number%20%3D%20WP.name%20%22%20%2B%0A%20%20%20%20%20%20%20%20%22LEFT%20OUTER%20JOIN%20%5BUser%5D%20AS%20ClosedBy%20ON%20ClosedBy.id%20%3D%20Act_Assign.closed_by%20%22%20%2B%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%22WHERE%20WP.name%20%3D%20%27CR-1091%27%20OR%20WP.name%20%3D%20%27CR-1169%27%20%22%20%2B%20%2F%2F%20AND%20Act.name%20%3D%20%27ECR%20Approval%27%20%0A%20%20%20%20%20%20%20%20%22AND%20Act_Assign.modified_on%20BETWEEN%20%272023-08-01%27%20AND%20%272023-08-02%27%20%22%20%2B%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%22ORDER%20BY%20WP.name%2C%20Act_Assign.created_on%20ASC%2C%20Act_Assign.closed_on%20DESC%20%22%0A%20%20%20%20%29%3B]

  • 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_
      Ideator 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.

      • AngelaIp's avatar
        AngelaIp
        Ideator I

        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?