cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
SimonMeadows
Level 8

datetime query with SQL Server

 

I am trying to do a lookup a field in an SQL Server table using a date time field but cant get it to work.

I have used SQL server profiler and managed to see the query it is passing to the database and I'm a bit stuck.

 

I am doing thid formula.

Collect(test,LookUp('[dbo].[tblContact]', LastUpdate = newDateTime))

I am sure that the value of 'LastUpdate' and 'newDateTime' have a matching value.

 

When I run this formula, the SQL query looks like this:

execute sp_executesql N'select top 1
    [$Ordered].[ID],
    [$Ordered].[CustCodeLink],
    [$Ordered].[LastUpdate],
    [$Ordered].[ReferralName],
    [$Ordered].[CultureInt]
from 
(
    select [_].[ID],
        [_].[CustCodeLink],
        [_].[LastUpdate],
        [_].[ReferralName],
        [_].[CultureInt]
    from [dbo].[tblContact] as [_]
    where 0 = 1
) as [$Ordered]
order by [$Ordered].[ID]'

Now the obvoius problem is 

where 0 = 1

Which will always return no results.

I cant seem to find a way to make this lookup work.

If I use text fields or number fields it's fine but I need to Lookup based on this date time field.

 

A couple of notes:

- The table is more than 500 rows so I can't convert to text and match that way (over 3000 rows so the querey must delegate to the SQL server)

- I cant change the schema of the database as it is intrinsically linked to software I can't modify.

 

Any suggestions would be apperciated.

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Dawidvh
Level 10

Re: datetime query with SQL Server

Hi @SimonMeadows 

 

Below are two videos dealing with advanced queries to SQL from PowerApps (using Flow).
For SQL Azure you can use direct (native) queries:
https://youtu.be/DII10gK715I

 

For on-premesis SQL servers, native queries are not supported from Flow (you will get an error: operation Execute Native Sql is currently not supported using an on-prem gateway connection), so we have to use stored procedures to accomplish the same:
https://youtu.be/BAGBzI4zdww

 

Please let me know if you don't come right.

 

Dawid van Heerden
Follow on Twitter: @davestechtips
Subscribe to YouTube: https://www.youtube.com/davestechtips?sub_confirmation=1
**If you found this reply helpful, please mark this as the answer to close the topic and make it easier to find for other people with similar questions.

View solution in original post

3 REPLIES 3
Community Support Team
Community Support Team

Re: datetime query with SQL Server

Hi SimonMeadows,

 

Please try to format the Datetime value using the DateValue function, then it would work as expected.

 

To test this issue, I use a function looks like below and it works for me:

Collect(test,LookUp('[dbo].[a2]',DateValue(Text(DateTime,DateTimeFormat.ShortDate))=DateValue(Text(Now(),DateTimeFormat.ShortDate))))

About DateValue function, please see more details at here:
https://powerapps.microsoft.com/en-us/tutorials/function-datevalue-timevalue/


Best regards,
Mabel Mao

Community Support Team _ Mabel Mao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
SimonMeadows
Level 8

Re: datetime query with SQL Server

Hi @v-yamao-msft,

 

Thanks for the suggestion.

I gave it a try but it doesn't work, there are two problems.

 

1. Using a Date only field is a problem for a last updated field.

I need it to be more accurate than to the day as many records may change in a day and I need to get the one I just created.

 

2. The where clause in the query to the database still becomes 

where 0 = 1

Here is the formula creating the record then getting its ID by looking it up using the LastUpdate Field

 

Collect(
    '[dbo]'.[tblContact]',
    {
        Contactname: "test",
        firstname: "test",
        surname: "test",
        LastUpdate: DateValue(Text(newDateTime,ShortDate),"en-GB"),
    }
);

Collect(
    '[dbo].[tblLinkCustContact]',
    {
        Customer_Code: First(newCust).code,
        ContactID: Value(
            LookUp(
                    '[dbo].[tblContact]',
                    DateValue(Text(newDateTime,ShortDate),"en-GB") = LastUpdate
            ).ID
        )
    }
)

 

Here is the query to the database triggered by the LookUp()

execute sp_executesql N'select top 1
    [$Ordered].[ID],
    [$Ordered].[Contactname],
    [$Ordered].[firstname],
    [$Ordered].[surname],
    [$Ordered].[LastUpdate]
from 
(
    select [_].[ID],
        [_].[Contactname],
        [_].[firstname],
        [_].[surname],
        [_].[LastUpdate]
    from [dbo].[tblContact] as [_]
    where 0 = 1
) as [$Ordered]
order by [$Ordered].[ID]'

 

 

 

Dawidvh
Level 10

Re: datetime query with SQL Server

Hi @SimonMeadows 

 

Below are two videos dealing with advanced queries to SQL from PowerApps (using Flow).
For SQL Azure you can use direct (native) queries:
https://youtu.be/DII10gK715I

 

For on-premesis SQL servers, native queries are not supported from Flow (you will get an error: operation Execute Native Sql is currently not supported using an on-prem gateway connection), so we have to use stored procedures to accomplish the same:
https://youtu.be/BAGBzI4zdww

 

Please let me know if you don't come right.

 

Dawid van Heerden
Follow on Twitter: @davestechtips
Subscribe to YouTube: https://www.youtube.com/davestechtips?sub_confirmation=1
**If you found this reply helpful, please mark this as the answer to close the topic and make it easier to find for other people with similar questions.

View solution in original post

Helpful resources

Announcements
thirdimage

Power Apps Community User Group Member Badge

Fill out a quick form to claim your user group badge now!

sixthImage

Power Platform World Tour

Find out where you can attend!

Power Platform 2019 release wave 2 plan

Power Platform 2019 release wave 2 plan

Features releasing from October 2019 through March 2020

SecondImage

Difinity Conference

The largest Power BI, Power Platform, and Data conference in New Zealand

Top Solution Authors
Top Kudoed Authors (Last 30 Days)
Users online (5,692)