SQL: Update column value conditioned on same column value - sql

On a SQLite database something I thought was very simple doesn't work at least under my conditions.
I have one column with names and some name contains apostrophe ('), which I want to remove. I know all names which contains an apostrophe, so I am not trying to query for apostrophes. I am doing something much simpler:
UPDATE table SET column_name="name surname1 surname2" WHERE column_name="name surname1'surname2";
which doesn't return what I expect. It doesn't produce an error but it doesn't modify any record.
SQL doesn't like reflexivity?

There should be no issue with querying the current value of a column to update the same column. Try escaping the single-quote by doubling it e.g. ''.
See: http://www.sqlite.org/lang_expr.html which reads:
A string constant is formed by enclosing the string in single quotes
('). A single quote within the string can be encoded by putting two
single quotes in a row - as in Pascal.
Therefore, your update should be:
UPDATE table SET column_name='name surname1 surname2' WHERE column_name='name surname1''surname2'
Update: Added explanation of escape mechanism.

Related

SQL Update table rows where the is a specific value

I have the following table in SQL server.
It is called ScenarioData and I am using the data specifically the fieldValue column to append this data into a form. I have achieved this functionality but unfortunately, the form requires a start date that has to be the present-day or up to 30 days in the future. Because I am storing data in the database as soon as the date passes these values are redundant. I have a stored procedure that selects all values from this table based on the scenarioDataId.
I was thinking to ensure that the date is always viable I could add to this stored procedure to update the relevant rows (coverStartDateDay, coverStartDateMonth, coverStartDateYear) with the current date so that the value will always be accepted.
I have proceeded the following SQL
UPDATE dbo.ScenarioData
SET ScenarioData.FieldValue = DAY(CURRENT_TIMESTAMP)
WHERE ScenarioData.FieldName = "CoverStartDateDay";
This I had hoped would append the current day to the rows in feilfValue wherein the column fieldName it equals the value coverStartDateDay. Unfortunately, I get an error saying CoverStartDateDay is not a column. Firstly where am I going wrong and secondly how can I achieve the functionality I desire?
Thanks in advance
Use single quote :
WHERE ScenarioData.FieldName = 'CoverStartDateDay';
In SQL Server double quotes are considers as column name. However, Double quotes have different usage depending on the setting QUOTED_IDENTIFIER.
By default QUOTED_IDENTIFIER is enabled (ON) & you can't use it to enclose literal strings.
For instance, if you want to go with double quotes then you need to disable QUOTED_IDENTIFIER.
SET QUOTED_IDENTIFIER OFF
Try this as varchar or string values should be passed in the single quote, not in the double quote.
UPDATE dbo.ScenarioData
SET ScenarioData.FieldValue = DAY(CURRENT_TIMESTAMP)
WHERE ScenarioData.FieldName = 'CoverStartDateDay'
Double quotes are for identifiers. Somehow if you want to pass values which contain a single quote, then you can use '' i.e., two times a single quote not the double quote.

How to write the LIKE syntax where the search term includes a percentage (%)?

I am using SQL Server 2012 and I need to perform a search on a specific field, called Notes. The search criteria is to find all rows where the term 8% is mentioned in that specific field.
The WHERE clause of my T-SQL query looks like this:
WHERE [Notes] like '%[8%]%'
However, the query is not filtering correctly based on the above syntax. It is also including rows where the term 8 is mentioned.
I had a look at the answers proposed in the question below, but they are still not giving me the correct answer.
SQL 'LIKE' query using '%' where the search criteria contains '%'
A single character class represents a single character. So [%] means a literal percent symbol, and 8[%] means literal 8%. Try this:
SELECT * FROM yourTable WHERE [Notes] like '%8[%]%'
Demo
you need to escape % in query for example below
SELECT columns FROM table
WHERE column LIKE '%\%%' ESCAPE '\'
Using SQL Escape Sequences
Below is from MSDN
Pattern Matching with the ESCAPE Clause
You can search for character strings that include one or more of the special wildcard characters. For example, the discounts table in a customers database may store discount values that include a percent sign (%). To search for the percent sign as a character instead of as a wildcard character, the ESCAPE keyword and escape character must be provided. For example, a sample database contains a column named comment that contains the text 30%. To search for any rows that contain the string 30% anywhere in the comment column, specify a WHERE clause such as WHERE comment LIKE '%30!%%' ESCAPE '!'. If ESCAPE and the escape character are not specified, the Database Engine returns any rows with the string 30.
you can try below answer given by #TimBiegeleisen that is also easy way.
just change your where clause as
WHERE `Notes` LIKE '%8!%%' ESCAPE '!

SQL query to search for records containing an apostrophe (')

I'm ingesting data into IBM MDM and we are finding source records with row key's containing an apostrophe (') which MDM cannot accept and therefore rejects the records. I want to query the source records to count how many records are affected, but with SQL apostrophes open and close text strings. Is there a way to make a query like
select count (*)
from table1
where field1 like '%'%' ;
Basically, I want to be able to search for a string of letters that have an apostrophe in it like...roundy's
Use backslash to escape special characters. To get aa'bb use aa\'bb.
Escape characters
" instead of ' will get you what you want

How do I deal with SQL tablenames with hyphen (-) when writing raw queries? i.e project-users

I have a table called project-users and want to write a SQL query like SELECT * FROM project-users I get this error ERROR: syntax error at or near "-".
I cannot change the table name at this point.
According to http://www.postgresql.org/docs/9.0/static/sql-syntax-lexical.html, you should use double quotes.
In your case, for PostgreSQL the query should be:
SELECT * FROM "project-users";
It is good practice to avoid the use of characters that need escaping or that contain spaces in identifiers.

How to compare a varchar field having "(" character

If a field value in the table of SQL Server is like A(B) and if I to write a query
SELECT * FROM MyTable WHERE MyField = 'A(B)'
it is not returning any result. How to handle this situation?
Your query should work fine, if you want to specify a different escape parameter, you can use ESCAPE.
WHERE column LIKE '%A#(B#)%' ESCAPE '#'
Also, if you want to match anything that contains "A(B)", don't forget to surround it by percetages symbols.