Collation Problem in asp.net connecting sql - sql

Hi when I use this query I don't get any result.
In my table I have used collation Macedonian_BIN so the letters are in Macedonian Language.
What I need to do this code to work, it doesn't compare 2 values
For example
НОВ=НОВ
The ed is getting the value HOB I guess the problem is when is comparing the HOB value with the HOB in database
select * from Publisher where Motor like '%" + ed + "%'

You can explicitly add the COLLATE keyword to change the default collation as follows:
select * from Publisher
where Motor COLLATE Macedonian_BIN like '%" + ed + "%' ";
(substitute your desired collation)

Related

SQL select rows case INsensitive not works my statement

I have Android app that sends to server pattern to find user by name. I want to be able to find rows where VARCHAR column is like pattern (but case INsensitive) that I send to server. Lets say we have a nickname like 'Bigboss'. The SQL statement should be able to find this row either when is passed 'Bigboss' or 'bIGBOSS' or 'biGbOsS' or 'bi' etc. Currently it works weird.
Here is my SQL statement:
SELECT *
from users_details
where upper(name) like '" + pattern + "%'
or lower(name) like '" + pattern + "%'
Help me please.
SELECT *
from users_details
where name ilike '" + pattern + "%'
see the manual :
The key word ILIKE can be used instead of LIKE to make the match
case-insensitive according to the active locale
Can you try this.
SELECT * from users_details where upper(name) like '%PATTERN%'
If you are passing pattern dynamically then like pattern.touppercase()

How to make spring boot code generic or dynamic if my requirement is both postgresql and sql server?

For example
sqlQuery = "SELECT COUNT(*) FROM " + table_name + " WHERE " + timeKey + " like '%"
+ currentDate + "%' AND game_type = '" + game_type + "'";
I used query like above in my spring boot code, this query means
that
Select COUNT(*) from table_one where timestamp like "2020-03-07%" And game_type ='Darksider';
This query works fine with mssql or sql server but it gives error when I use postgresql in my spring boot code .
I dont want to change my query and make two different projects for mssql and postgresql I want my code to be dynamic so how can I implement it so that my spring code works fine with both postgresql and mssql .

SQL Query to search by name(containing combination of two or more word)

When i search by using
....LIKE '%" + txtName.Text + "%'.....
Then it returns all the rows containing txtName. It is okay for name like
Bijay or
Bijay kumar.
And rows contains data like
Bijay,
Bijay kumar kush,
Bijay kush.
In this case it should return, Bijay kumar kush and Bijay Kush when search by using txtName like Bijay Kush. But not getting the output like this. It is empty when search by using first name and last name without supplying middle name.HeretxtNameis the Full Name text Box used for searching the name.I want to know where am I making the mistake.Any help would be appreciated. thanks in advance.I am using this type of query which doesn't return the result if searched without middle name as stated above.
...
if (!String.IsNullOrEmpty(txtName.Text) && !String.IsNullOrWhiteSpace(txtName.Text))
SearchSqlDataSource.SelectCommand += " AND (New_Employees.Full_Name LIKE '%" + txtName.Text + "%' OR New_Employees.Full_Name LIKE '%" + txtName.Text + "%')
I might suggest replacing all spaces with the wildcard character. You can do this in the application or the database. Assuming the underlying database is SQL Server, here is the database code:
WHERE New_Employees.Full_Name LIKE REPLACE('%" + txtName.Text + "%', ' ', '%')

How do I run an SQL update query using a like statement

I am trying to update a field in a table using an SQL update query where there is a like statement referencing a value in another table. They syntax unfortunately is not working. Below is my code. In short, I am trying to put a '1' in the field 'Query07ParolaChiave' in the table 'tblSearchEngine01' when the value located in table 'tblsearchengine07' is present in the field 'tblMasterListOfEventsNotes' located in the table 'tblSearchEngine01'. I think my code is almost complete but there is a syntax issue which i cant find.
st_sql = "UPDATE tblSearchEngine01, tblSearchEngine07 SET tblSearchEngine01.Query07ParolaChiaveSelect = '1' WHERE ((([tblSearchEngine01].[tblMasterListOfEventsNotes]) Like " * " & [tblsearchengine07].[ParolaChiave] & " * "))"
Application.DoCmd.RunSQL (st_sql)
I suggest you 2 solutions :
This one is using EXISTS functions, and will check for each row in tblSearchEngine01 if there is a matching value in tblsearchengine07
UPDATE
tblSearchEngine01
SET
tblSearchEngine01.Query07ParolaChiaveSelect = '1'
WHERE
EXISTS (SELECT 1
FROM tblsearchengine07
WHERE [tblSearchEngine01].[tblMasterListOfEventsNotes] Like '*' & [tblsearchengine07].[ParolaChiave] & '*')
This one is more performant because it uses JOIN
UPDATE
tblSearchEngine01
INNER JOIN tblsearchengine07
ON [tblSearchEngine01].[tblMasterListOfEventsNotes] Like '*' & [tblsearchengine07].[ParolaChiave] & '*'
SET
tblSearchEngine01.Query07ParolaChiaveSelect = '1'
I read something like in ADO/VBA, you have to use % instead of * as the wildcard.
You can have more information on wildcard and LIKE comparator here
UPDATE
Why the '1' after select in your first solution?
EXISTS (SELECT 1 ... is better for performance because it return only the number 1 instead of fields, anyway EXISTS just stop the excecution after 1 element found.
'Performant' means more consuming in regards to space and memory?
JOIN is more performant in term of time of execution, RDBMS are far better at joining tables than using subquery, in some rare case, it's more interesting to use the 1st solution.
Also, any initial thoughts as to why my original solution (coming straight from an Access Query which works) does not function?
I cannot really know but perhaps it's because of " * ", because you are saying SPACE + * + SPACE + VALUE + SPACE + * + SPACE. For ex : 'John' LIKE ' John '
May be with "*" instead of " * " could solve it...
I have no other track, I'm not Access sql developper, I usually play around Sql server/Oracle/mySql, hope it helped. ;)
Try to change your like this way:
... Like '*" & tblsearchengine07.parolachiave & "*'))"
The like statement go into the WHERE clause.
If you do want to use LIKE without you care about caps letters, then you can use it like this:
LIKE COLUMN_NAME = '%WhatYouLike%'
My suggestion is:
Use a table variable (#Table) with a unique/primary key coming from the table to be updated.
SELECT all the data to be updated (you can add the like statement here) and then INSERT that in the created table variable.
Construct the UPDATE statement with an INNER JOIN to the table variable matching with the unique/primary key.
I know this may take a lot of steps but believe me these are more efficient than using a black list approach.

Matching text string on first letter in SQL query

SAMPLE CODE:
Dim sql As String = "SELECT * FROM " + tblName + " WHERE needsTranslation = 'True' AND dataText LIKE " & "'" & alpha & "%" & "'" & " ORDER BY dataText;"
da = New SqlDataAdapter(sql, strConnection)
OP:
I would like to create a SQL query that returns all records when the first letter of a string matches my variable. I am coding this in an ASP.net code behind page in vb.net.
SELECT * FROM " + tblName + " WHERE textData = ' & alpha & "
In this exmample textData is a string of text and alpha is a single letter a through z or A through Z.
I don't need the criteria to be case sensitive, but I do need only the first letter of textData to match alpha.
I have tested the LIKE comparator and it does not return all records that begin with alpha.
What is the best way to do this? Any and all help will be appreciated.
thanks again,
The LIKE operator is what you'd want to use, but you have to use the % wildcard character like so:
SELECT * FROM MyTable WHERE textData LIKE 'a%'
SQL has sub-string operator SUBSTR() or SUBSTRING()
select * from tableName where substr( textData ) in ( 'A', 'B', 'C', ... );
I couldn't add to the comments on one of the other posts, but I'll strongly second the need to use a parameterized query for these reasons (you can include usage of the like operator with the wildcard % like the other answer correctly summarized to answer your question):
It will protect you from making mistakes with single quotes, especially if the user enters a search string that includes them
(they will cause your query to fail).
It protects you from SQL injection exploits. Example, a user were able to input the value of the variable "alpha" in the above
example they could enter something like:
'; DELETE FROM ;
If the user you were using had excessive database rights, they could
wreak all kinds of havoc (or they could potentially get access to
data they shouldn't have access to).