SQL Server starts with list of strings - pymssql

I was previously using MySQL and had a query which used a REGEXP. I'm trying to find site_ids beginning with certain characters, and used the following:
WHERE site_id REGEXP '^(AB|BC|AO|BO|CA|PAF|Z)'
Basically trying to find rows where site_id LIKE 'AB%' OR 'BC%'..., but because I have quite a few strings to match against, I'd like to do it in a less verbose manner.
Unfortunately SQL Server doesn't seem to like this syntax and I get an error:
An expression of non-boolean type specified in a context where a condition is expected, near 'REGEXP'.DB-Lib error message 4145, severity 15: General SQL Server error: Check messages from the SQL Server
Is there a neat way of doing this without using heaps of LIKE 'XX%' OR ...?

You can use a combination of left, or, and in to shorten your query.
WHERE left(site_id,2) in ( 'AB', 'BC', 'AO', 'BO', 'CA') or left(site_id, 3) = 'PAF' or left(site_id, 1) = 'Z'

Related

How to prevent SQL injection in InfluxDB for a user-supplied measurement

Say I have an InfluxDB query where the user supplies a measurement. In this case the user supplies the value "foo". Then I would construct the query:
SELECT * from "foo"
WHERE time > "2022-06-21T18:27:16.041Z"
What's the best way to prevent injection attacks here? I know InfluxDB supports bind parameters, but apparently that feature only works for the WHERE clause, so it wouldn't help me.
I was thinking of trying this:
const query = `
SELECT "value" FROM "${Influx.escape.measurement(key)}"
WHERE time > "2022-06-21T18:27:16.041Z"
`
...but based on my testing that function doesn't escape quotation marks, only spaces.
I'm using InfluxDB 1.x in Node.js via the influx npm package.
Before sending the value to the query try filtering chars like single and double quotes
Looking into docs, articles I can see that every subquery is in brackets, like:
select * from (select "value" from "measurement") <where_caluse>
So filtering for brackets beetwen "FROM" and "WHERE" should be enough.
Based on:
#1 https://www.influxdata.com/blog/tldr-influxdb-tech-tips-january-26-2017/
#2 https://docs.influxdata.com/influxdb/v1.7/query_language/data_exploration/#subqueries

Malformed SQL Statement: Expected token 'USING' but found Identifier with value 't' instead

I am trying to merge to a SQL Database using the following code in Databricks with pyspark
query = """
MERGE INTO deltadf t
USING df s
ON s.SLAId_Id = t.SLAId_Id
WHEN MATCHED THEN UPDATE SET *
WHEN NOT MATCHED THEN INSERT *
"""
driver_manager = spark._sc._gateway.jvm.java.sql.DriverManager
con = driver_manager.getConnection(url) #
stmt = con.createStatement()
stmt.executeUpdate(query)
stmt.close()
But I'm getting the following error:
SQLException: Malformed SQL Statement: Expected token 'USING' but found Identifier with value 't' instead at position 25.
Any thoughts on where might be going wrong?
I don't know why you're getting this exact error. However I believe there are a number of issues with what you are trying to do.
Running the query via JDBC makes it run in SQL Server only. Construct like WHEN MATCHED THEN UPDATE SET * / WHEN NOT MATCHED INSERT * will not work. Databricks accepts it, but for SQL Server you need to explicitly provide columns to update and values to insert (reference).
Also, do you actually have tables named deltadf and df in SQL Server? I suppose you have a Dataframe or temporary view named df... this will not work. As said, this query executes in SQL Server only. If you want to upload data from Dataframe use df.write.format("jdbc").save (reference).
See this Fiddle - if deltadf and df are tables, running this query in SQL Server (any version) will only complain about Incorrect syntax near '*'.
SQLException: Malformed SQL Statement: Expected token 'USING' but found Identifier with value 't' instead at position 25.
if you missed updating any specific field or specific syntax, you will get this error.
I performed merge operation its working fine for me without error, Please follow below reference .
Reference:
https://www.youtube.com/watch?v=i5oM2bUyH0o
https://docs.databricks.com/delta/delta-update.html#upsert-into-a-table-using-merge
https://www.sqlshack.com/sql-server-merge-statement-overview-and-examples/

Cognos Analytics 11 - Age groups

I have a data element expression I want to use as a category for a crosstable.
This gives me the errors "QE-DEF-0459 CCLException" and "QE-DEF-0261 QFWP", although I have followed the syntax properly. Any ideas what is causing this? It seems to be related to the [BIRTHDATE] column inside the when-clauses.
The error message goes like this: qe-def-0260 parsing error before or near position: 40 in: "case when (_years_between(current_date,"
The source database is Oracle.
Usually there are messages which are appended after the error number. The message should be helpful in solving your problem, so reading it would be helpful for you and including the text, rather than just quoting an error number when you ask for assistance could be helpful to others.
I'm not familiar with any case function in Cognos where the query item is required after the case.
Also Case requires an end operator.
Re-write your expression to be something like this, where I've removed birthdate and added the end.
case
when (_years_between(current_date, [BIRTHDATE])>=0 and _years_between(current_date, [BIRTHDATE])<=49) then '0-49'
when (_years_between(current_date, [BIRTHDATE])>=50 and _years_between(current_date, [BIRTHDATE])<=100) then '50-100'
else 'null'
end

Snowflake Python connector insert doesn't accept variables

Using snowflake connector I am trying to insert a record in a table.
In snowflake doc they have shown examples with hard coded strings, but when I try to use my variables instead, it doesn't work. Please suggest how to use variables in this case.
conn.cursor().execute(
"INSERT INTO cm.crawling_metrics(FEED_DATE,COMP_NAME,REFRESH_TYPE,CRAWL_INPUT,CRAWL_SUCCESS) VALUES " +
"(score_creation_date,compName,sRefreshType,mp_sku_count,comp_sku_count)"
I get the below error
snowflake.connector.errors.ProgrammingError: 000904 (42000): SQL compilation error: error line 1 at position 100
invalid identifier 'SCORE_CREATION_DATE'
NOTE: In the above code if i hard code with String instead of variables, it works.
Kindly suggest what is the right way ?
You need to use string interpolation / formatting for your code to use these as actual variables:
conn.cursor().execute(
"INSERT INTO cm.crawling_metrics (FEED_DATE, COMP_NAME, REFRESH_TYPE, CRAWL_INPUT, CRAWL_SUCCESS) VALUES " +
f"('{score_creation_date}', '{compName}', '{sRefreshType}', '{mp_sku_count}', '{comp_sku_count}')"
)

How do I filter a SQL query by exact match to string?

I'm using MS SQL 2008 and I have a status field that comes like this:
"REF CNF PCNF REL"
I need to get all the orders with status CNF without returning PCNF.
I could do it using spaces before and after WHERE STATUS LIKE '% CNF %', but if CNF is the first or last status it wouldn't work.
One solution that worked was:
WHERE
PATINDEX('CNF %',STATUS)=0 AND
PATINDEX('% CNF %',STATUS)=0 AND
PATINDEX('% CNF',STATUS)=0
But that is just horrible.
Thanks,
As said by Marc B., you should normalize your table to avoid storing more than one value in a single field.
If you don't have the credentials to do that, or if you want to keep your model as it is, you can try to add spaces before and after your string:
WHERE ' '+STATUS+' ' LIKE '% CNF %'
This way you don't have to worry about CNF being first or last item in your list.
I don't know if it's the most elegant/effective solution, but it works.
Why not simply
WHERE STATUS LIKE '% CNF%' OR STATUS LIKE 'CNF%'
?
The wildcard % matches any character(s), also none.
Using SQL 2008's own internal functions, the best I can think of is getting it down to just two conditions like:
where STATUS like 'CNF%' or STATUS like '%[^P]CNF%'
But if you were willing to install a .Net add-on, you could use regular expressions like so:
where 1 = dbo.RegExpLike(STATUS, '(CNF| CNF)')

Resources