Search string which contains specail characters(!,#,#./,comma(,)) - sql-server

I have a column in DB where it may contain data with special characters.
for ex:a column,Name may have data as following
santosh's
santosh/s
santosh%s
How to search for these values in DB using like operator.
Select * from table where name like '%santosh%';
How to change the above query to search for apostrophe(santosh's)

for 1. apostrophe s you can use
Select * from table where column like '%santosh''s%'
for 2. /s you can give directly
Select * from table where column like '%santosh/s%'
for 3. %s you can use
Select * from table where column like '%santosh[%]s%'
hope this helps.....

Just use 2 apostrophe to escape the character
Select * from table where name like '%santosh''s%';

Related

Fulltext Contains does not return rows

I have a row in a table that contains "DS012345" in a column called description
When I use this query:
Select * from Tablename where Contains(Description, ' "*012345*" ')
This query returns no result.
I have created the unique index, fulltext catalog, I have turned off the Stop Words using the Object Explorer. Still do not know why it does not return that row.
Any suggestion or cause for this?
Thanksl.
Why not just use LIKE instead to do a search.
Select * from Tablename where Description LIKE '%012345%'
Just does a search where 012345 appears anywhere within the description column.
Stop words is the number that it starts to seek for a word in your database..
Fulltext should be used to get the exact word, if you just want a part of the word you should use LIKE %...%.

Deleting rows in PostgreSQL if column begins with a specific pattern

I am very new at PostgreSQL and want to be able to delete rows from a table if a specific column starts with a certain pattern.
For example,
I have a reference column which is a sort of time stamp (YYMMDDHHMM). If the reference column starts with 16********, then i'd like it to be deleted.
So far, I know how to delete the column if the whole reference is known. E.g:
DELETE FROM my_tbl WHERE name='test1' AND ref_no=1601010000;
However, I need to change this so that the condition only specifies the first 2 digits of the reference number.
Read about LIKE operator: https://www.postgresql.org/docs/8.3/static/functions-matching.html
Use this command to delete rows:
DELETE FROM my_tbl WHERE reference LIKE '16%'
Before deleting rows from the table, first check if a delete condition really works as expected using it in a regular SELECT statement:
SELECT * FROM my_tbl WHERE reference LIKE '16%'
If reference column is not of type varchar but for example int, you need first to conver (cast) it to a varchar datatype in this way:
SELECT * FROM my_tbl WHERE reference::varchar LIKE '16%'
DELETE FROM my_tbl WHERE reference::varchar LIKE '16%'
Try the 'like' operator, it allows you to do pattern matching.
DELETE FROM my_tbl WHERE name LIKE '%test%';
The '%' character is a wildcard. The above line should delete every column where the name contains the string 'test'.
More info here: pattern matching

Return rows where ID is in semicolon separated string from subquery MSSQL

I'm trying to query my sql database to return all the rows where the ID is contained in a separate tables column. The list of project IDs is kept in the Feedback table in the Project_ID Column with datatype varchar. I am trying to return the rows from the Projects table where the IDs are kept in the Project_ID column with datatype varchar.
I am doing this using the query
SELECT * FROM Projects WHERE Project_ID IN (
SELECT Project_ID FROM Feedback WHERE ID = 268 and Project_ID IS NOT NULL
)
When I run this query I am returned with the message:
Conversion failed when converting the varchar value '36;10;59' to data type int
This is yet another example of the importance of normalizing your data.
Keeping multiple data points in a single column is almost never the correct design, and by almost never I mean about 99.9999%.
If you can't normalize your database, you can use a workaround like this:
SELECT *
FROM Projects p
WHERE EXISTS (
SELECT Project_ID
FROM Feedback F WHERE ID = 268
AND Project_ID IS NOT NULL
AND ';'+ F.Project_ID +';' LIKE '%;'+ CAST(p.Project_ID as varchar) +';%'
)
You can't use the IN operator since it's expecting a list of values delimited by a comma, while you try to supply it with a single value that is delimited by a semicolon. Even if the values in Project_ID was delimited by a comma it would still not work.
The reason I've added the ; on each side of the Project_ID in both tables is that this way the LIKE operator will return true for any location it finds the Projects.Project_Id inside the Feedback.Project_Id. You must add the ; to the Projects.Project_Id to prevent the LIKE to return true when you are looking for a number that is a partial match to the numbers in the delimited string. Consider looking for 12 in a string containing 1;112;455 - without adding the delimiter to the search value (12 in this example) the LIKE operator would return true.

How to search in fields delimited by the tilde character

I have a field in the database separated by tilde ~
I can't figure out how to search in the field with a mssql query.
Ex select * from table where john in repnames
Data is in the field like tom~john~max
I've tried a few searches on Google but haven't found any tips.
Try below :
select column1, column2
from table
where repnames like '%~%'
Note with limited info that you have provied, this is not the most efficient way as it will be scanning the table.

MS SQL Select Where ntext-Column contains specific hex String

I have a problem in a select on mssql
in my table there is a column with datatype ntext and i'm searching for an specific character.
when i read the value and convert it to hex ill get something like that
SELECT convert(varbinary(max),convert(nvarchar(max),COLUMN_Value)) FROM db.TABLE WHERE PK = 1234
0x3C005000200063006C006100730073003D004D0073006F004E006F0072006D0061006C0020007300740...
In the hex value there is for example 3C00, when I now rewrite my select and search for this value no row is matching :/
SELECT * FROM db.TABLE WHERE convert(varbinary(max),convert(nvarchar(max),COLUMN_Value)) LIKE '%3C00%'
Does anybody know whats the problem here?
finally i've done it with following query
SELECT * FROM db.TABLE WHERE CONVERT(varchar(max),convert(varbinary(max),convert(nvarchar(max),COLUMN_Value)),2) LIKE '%3C00%';
i used the hex value from my first query (3C00) and found every row containing it
Try this
SELECT * FROM db.TABLE WHERE convert(nvarchar(max),COLUMN_Value) LIKE '%3C00%'

Resources