Invalid column name when selecting - sql

I have a table called Jobs with the following column names: JobID, Name, and Value. The table is filled like just one entry: JobID: 1, Name: TestJob, Value: 10
I want to do select * from Jobs where Name="TestJob", but this gives me an error saying "Invalid column name 'TestJob'". Why can't I select by the value of the Name column? Doing JobID=1 or Value=10 will give me the proper result.

Use single quotes instead of double quotes. Single quotes are the standard for SQL string and date constants:
select *
from Job
where Name = 'TestJob';
Some databases do accept double quotes for this purpose. It is always safest to use single quotes for string and date constants and double quotes to escape identifier names (if needed).

Change your double quotes " to single quotes '. Double quotes are used to surround object names, probably in the same way [] can be used, so you can have spaces and other normally-invalid object name characters in the object name. Single quotes, on the other hand, are used for string literals.

Use ' instead of ". It will work.

Related

How to escape single quotes in RLIKE Hive? Double single quotes not working

I would like to escape the single quote in RLIKE input. I used double single quotes like so:
SELECT * FROM TABLE
WHERE column RLIKE 'o''brien'
But it returned the results with "obrien" rather than "o'brien". I tried "\\'" instead of double single quotes too, but that doesn't work either. So what is the correct escape character for single quote?
Three methods:
1 Put the whole regexp into double-quotes, single quote is shielded inside double-quotes:
where column rlike "o'brien"
See also: https://stackoverflow.com/a/66175603/2700344
2 Use unicode \u0027
where column rlike 'o\\u0027brien'
3 Use HEX \x27
where column rlike 'o\\x27brien'
Using \\x or \\u codes you can check any special character if you know it's code.
You can just use =:
WHERE column = 'o''brien'
I'm not sure why you are using RLIKE unless you intent:
WHERE column LIKE '%o''brien%'

Matching literal percent in h2/postgres?

I have the following value in a db column:
some%thing
I'm using h2 and postgres, but can't figure out why this query, where I have escaped the % to perform a literal search, is not matching the row highlighted?:
LIKE 'some\%thing'
Because 'value' is a character string literal and not the name of your column. You need to use double quotes instead.
"value" LIKE 'some\%hing'
One option is to use regular expressions:
where value ~ '%'
LIKE also supports an explicit escape character:
where value like '%$%%' escape '$'
Or the default one, \.

How do I use single quotes as part of a string in SQL

I have a where clause that uses a string,
Where
pm.Alias = 'Toys'R'Us France'
However part of the string uses single quotation marks, 'R'
How do i wrap up the whole string to pass through into my Where clause
I cannot use:
Where
pm.Alias = 'Toys''R''Us France'
As i need the whole string encased, as i will use this in Excel to pass this as a paramter into my query
in SQL, if you want to have Single Quotes inside a string, then you should specify it as 2 consecutive single quotes for every single quote in your string. So
Where
pm.Alias = 'Toys'R'Us France'
should be written as
Where
pm.Alias = 'Toys''R''Us France'
You might try using extra quotes after and before the existing quotes.
In this case add quote before and after 'R', and the query will be like below.
Where
pm.Alias = 'Toys''R''Us France'
I recently face this issue in my sqlite database, you can resolve using like this.
Where
pm.Alias = "Toys'R'Us France"
use double quote (") instead of single quote (') after equal sign.

Aliases for sql column

I would like to understand why this does not work :
select my_table.my_column AS 'what I want to write'
from my_table
The error:
ORA-00923: FROM keyword not found where expected
How do I name my column what I want to write ?
Thank you
Single quotes are for string literals, double quotes are for delimited identifiers, e.g. "what I want to write".

SQl Output to CSV WITHOUT single Quotes

I am running a query on Adaptive Server Anywhere v7.
select customerinfo.customerid, Name, Address1, Address2, City, State, ZIP, Country from customerinfo, addressinfo
where customerinfo.customerid = addressinfo.customerid
and MEMBER = (Date(GetDate()-4))
and addressinfo.addresstype = 's';
Output to C:\SamplePacks.CSV
Output is:
123, 'name','address1,'address2'.....
Is there a way to run the query so that the single quotes DO NOT show?
See doc
QUOTE clause The QUOTE clause is for the TEXT output format only.
The quote string is placed around string values. The default is a
single quote ('). If ALL is specified in the QUOTE clause, the quote
string is placed around all values, not just around strings. To
suppress quoting, specify empty single quotes. For example, QUOTE ''.
So it seems that you should do
Output to 'C:\SamplePacks.CSV' QUOTE ''