0

I have a df with clients and products, and I have to get all information about this items from SQL Server, but I have to parsed with substring, because DateBase contains product path, like this:

productpath:

fruits/apple
vegetables/tomato

And list of my products looks like this, but real list contains more than 1000 values:

df['prod']= ['apple', 'orange', 'tomato']

So I decided to use this code:

sql_sales = """
SELECT
    date,
    client,
    productpath,
    cost
FROM all_sales
WHERE 
    client == %(c1)s
    AND productpath LIKE '%%(prod)s%'

"""

And I've got mistake with using LIKE with this part '%%(prod)s%' like this:

not enough arguments for format string unable to rollback

Without LIKE-part it is working

3
  • 1
    Side note client == isn't valid syntax. T-SQL uses = for both assignment and comparison. Commented Jul 14, 2020 at 11:53
  • Does productpath have several '\' in it? Commented Jul 14, 2020 at 12:52
  • Yes, it has a different quantity of '/' Commented Jul 14, 2020 at 14:20

1 Answer 1

1

One method uses a subquery:

. . . 
where . . . and
      exists (select 1
              from string_split('apple,orange,tomato', ',') s
              where all_sales.productpath like concat('%', s.value, '%')
             )
Sign up to request clarification or add additional context in comments.

4 Comments

split_part still isn't a SQL Server function, Gordon. If you're thinking of STRING_SPLIT then you need to provide 2 parameters, the string to split and the delimiter.
Unfortunatelly my list with products contains more than 1000 items, that is why I can't put list in brackets
@Larnu . . . Arrgh. To much Postgres thinking. I fixed the answer. Ironically, I would need multiple arguments for that original function in Postgres as well.
No worries, Gordon. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.