alternative to single quote in MSSQL query - sql-server

My question may be silly.
I have an application that does not like single quotes.
I want to execute SQL query from that application.
When I form my SQL query, I cannot use single quotes.
I am looking for alternative way for single quotes.
I am on MicroSoft SQL 2012.
Example, my SQL is like this,
SELECT name
FROM People
WHERE peopleId = '123'
However I want to write this without single quotes,
Something like below I was trying,
SELECT name
FROM People
WHERE peopleId = CHAR(39)123CHAR(39)
Thank you

If it accepts double quotes...
SET QUOTED_IDENTIFIER ON
GO
https://msdn.microsoft.com/en-us/library/ms174393.aspx
When SET QUOTED_IDENTIFIER is ON, identifiers can be delimited by double quotation marks, and literals must be delimited by single quotation marks. When SET QUOTED_IDENTIFIER is OFF, identifiers cannot be quoted and must follow all Transact-SQL rules for identifiers. For more information, see Database Identifiers. Literals can be delimited by either single or double quotation marks.

If your PeopleID or filter value is initially a numeric value you can do this. If it is not numeric I don't know.
SELECT name
FROM People
WHERE peopleId = CAST(123 AS VARCHAR(25))

Related

Searching for records with where clause having single quotes

I have a table column(nvarchar type) which contains an entry "XYZ INT'ABC" and I am using this column in a stored procedure in the where clause. So when adding the condition in the where clause, the check should be done as below:
select * from tableName where ColumnName = 'XYZ INT''ABC';
When passing the parameter to the SP from VB.net code, the parameter is passed correctly as shown in the select query above. But the SP searches using the below query due to which I am not getting the desired output for the mentioned where clause.
select * from tableName where ColumnName = 'XYZ INT''''ABC';
Please help. Thanks in advance.
You've got some suggestions in comments already. Just some explanation:
The doubled quotes are not really part of the string
DECLARE #SomeString VARCHAR(100) = 'With one quote''!';
The string you'll find within your variable will be With one quote'!. The doubled quotes are just a syntax speciality to allow the statement parser to see, whether a quote terminates the string or not. Something like
DECLARE #SomeString VARCHAR(100) = 'With one quote'!'; --wrong!
...is wrong, because the string ends after quote and the !' is a syntax error suddenly.
You need this only in cases, where the single quote is the string marker. In VB.Net you'd have the same issue, if you want to place a double quote " within your string, but this would work in SQL like here:
DECLARE #SomeString VARCHAR(100) = 'With one double quote"!';
You must think two-folded
How do you assign a value? (depending on your IDE and statement parser)
What is the actual value stored in a variable (this will be used at run-time)
Related and more obvious example:
Very often you see code like this (assignment)
string SomeString = "Some text/nwith a line-break";
which is translated to (actual value)
Some text
with a line-break

How to replace semicolons?

I have an SQL SELECT query that's grabbing some data from my database. I need to replace a certain word that contains a semicolon in my SELECT query. Exactly this:
REPLACE(Table.Field,'"','') AS Field1
The error I'm getting reads
Unclosed quotation mark after the character string '"'.
So I think the semicolon is terminating the query. How can I escape that semicolon?
I tried backslashes and using double quotes.
Some sample data and expected output, as requested
Sample data
Field
"Hello"
"Goodbye"
Expected output
Field1
Hello
Goodbye
Full Query
SELECT REPLACE(Table.Name,';','') AS Name,
SUM(Table.Quantity) AS Quantity,
SUM(Table.Price*Table.Quantity) AS Price
FROM Table
GROUP BY Name
The ; symbol doesn't terminate the query and it should not be escaped, if it is part of the string literal (the text enclosed in single quotes ').
Here is a complete example that demonstrates that it works fine in SSMS:
CREATE TABLE #TempTable (Name varchar(50));
INSERT INTO #TempTable (Name) VALUES('Field');
INSERT INTO #TempTable (Name) VALUES('"Hello"');
INSERT INTO #TempTable (Name) VALUES('"Goodbye"');
SELECT
Name
,REPLACE(Name,'"','') AS ReplacedName
FROM #TempTable;
DROP TABLE #TempTable;
This is the result set:
Name ReplacedName
---- ------------
Field Field
"Hello" Hello
"Goodbye" Goodbye
You didn't provide all details of how you construct and execute your query, so I have a guess. It looks like you are:
building the text of the query dynamically
use some web-based tools/languages/technologies for that
web-based text processing tool/language that you use parses the text of your SQL query as if it was HTML and interferes with the result. For one thing, it changes " to the " symbol.
during all this processing you end up with unmatched ' symbol in the text of your SQL. It could come from the user input that you concatenate to your query of from a value stored in your database.
it has nothing to do with the ; symbol. Your error message clearly states that the matching quotation mark (which is ') is missing after the " symbol.
To understand what is going on you should print out the text of the actual SQL query that is sent to the server. Once you have it, it should become obvious what went wrong. I don't think that the Full Query that you put in the question is the real query that you are trying to run. It has syntax error. So, get the real thing first.
This works fine for me
declare #a as nvarchar(50) = '"Hello"'
select REPLACE(#a,'"','') AS Field1
declare #b as nvarchar(50) = '"Goodbye"'
select REPLACE(#b,'"','') AS Field1
Error message says unclosed quotation mark ?
Do you have single quotes in few of your fields ?
In that case you can replace them first as below
REPLACE(Table.Field,'''','') AS Field1
Let me know you need more help with this.
Source
"
the double quote sign "
I think there is no where that this parameter is known as a special phrase that refers to " and cause you error message.
In SQL Server there is just a function like QUOTENAME ( 'character_string' [ , 'quote_character' ] ) that used like this: -Just for ' or " or []-
SELECT QUOTENAME('Sample', '"') --> result is `"Sample"`
SELECT QUOTENAME('Sam"ple', '"') --> result is `"Sam""ple"`
In SQL Server identifiers can be delimited by ", When SET QUOTED_IDENTIFIER is ON -for following the ISO rules-. When SET QUOTED_IDENTIFIER is OFF, identifiers cannot be quoted and must follow all Transact-SQL rules for identifiers. Literals can be delimited by either single or double quotation marks.
I suggest you using SET QUOTED_IDENTIFIER OFF that make sure, that you've not identifier between " in your query.
Note:
When a table is created, the QUOTED IDENTIFIER option is always stored as ON in the table's metadata even if the option is set to OFF when the table is created.
If you are using a SQL string I suggest this syntax:
REPLACE(Table.Field, CHAR(34), '') As Field1
or
REPLACE(REPLACE(Table.Field, ';', '.'), '&quot.', '') As Field1

Handling column names with spaces in SQL Server

I have created a SQL Query to update various columns, where the column names contain spaces. It works if I run it manually as a query:
UPDATE dbo.Survey
SET PhotoPathQ1='(null)'
WHERE "Q1 Photo Taken"='0'
UPDATE dbo.Survey
SET PhotoPathQ2='(null)'
WHERE "Q2 Photo Taken"='0'
UPDATE dbo.Survey
SET PhotoPathQ3='(null)'
WHERE "Q3 Photo Taken"='0'
... and further similar updates
However if I try to automate this using SQL Server Agent as a Transact-SQL script (T-SQL) it does not actually do anything to my table, the job says that it has run successfully but the data has not been updated.
Any help would be appreciated.
Am I missing something obvious with this?
Looks like an error in your syntax, try this as an example:
UPDATE dbo.Survey
SET PhotoPathQ1 = null
WHERE [Q1 Photo Taken] = 0
This assumes that the field PhotoPathQ1 is nullable and you actually want to insert a true null value in to it rather than a string '(null)'.
It also assumes that [Q1 Photo Taken] is a bit or int field, although SQL Server will handle the conversion happily if you have it in quotes. If it's a string data type, then you should leave the quotes there.
You should use square brackets on field names that contain spaces instead of double quotes:
[Q1 Photo Taken]

Openedge progress - Can't escape single quote - Is this a bug?

I am trying to run a simple query over jdbc ALTER TABLE Customer ALTER \"Cust-Name\" set PRO_DESCRIPTION 'Customer Name'
This works perfectly well. But, when I have to set description as "Customer's Name", i.e, include a single quote - I am unable to get it to work.
I tried
ALTER TABLE Customer ALTER \"Cust-Name\" set PRO_DESCRIPTION 'Customer~'s Name'
ALTER TABLE Customer ALTER \"Cust-Name\" set PRO_DESCRIPTION 'Customer~~'s Name'
ALTER TABLE Customer ALTER \"Cust-Name\" set PRO_DESCRIPTION 'Customer\\'sName'
ALTER TABLE Customer ALTER \"Cust-Name\" set PRO_DESCRIPTION "Customer's Name"
Nothing works.
I don't know Progress, but the SQL standard is to duplicate the single quote:
'Customer''s Name'
While I was learning Progress I encountered a function called QUOTER that can be used in your situation.
QUOTER function
Converts the specified data type to CHARACTER and encloses the results
in quotes when necessary.
The QUOTER function is intended for use in QUERY-PREPARE where a
character predicate must be created from a concatenated list of string
variables to form a WHERE clause. In order to process variables,
screen values, and input values so that they are suitable for a query
WHERE clause, it is often necessary to enclose them in quotes. For
example, European-format decimals and character variables must always
be enclosed in quotes. You can use the Quoter function to meet that
requirement.

How to escape quotes in strings in vertica (vsql)?

So I need to insert some values into a vertica database (via vsql), which may contain quotes and all sorts of special characters. But vertica does not seem to understand character escaping. For example:
rpt=> select "asdasda\"asdasdad" from some_table limit 1;
rpt"> ";
ERROR: syntax error at or near "" from some_table limit 1;
"" at character 26
LINE 1: select "asdasda\"asdasdad" from some_table limit 1;
This is not the insert statement, but you should get the idea.
Well, first off I should have used single quotes. Escape sequences used to work in earlier versions (before 4.0 I believe), but now they are off by default. If you do not want to tweak database config parameters you have two options.
Use E' syntax:
select E'somethin\' here' from v_catalog.dual_p;
Or double the quotes that need to be escaped:
select 'somethin'' here' from v_catalog.dual_p;

Resources