This question already has answers here:
Error: Column does not exist in postgresql for update [duplicate]
(1 answer)
postgres column "X" does not exist
(1 answer)
Closed 2 years ago.
I have an sql table that looks like this in postgresql called test.
date | data | source
------------+---------+------------------
2015-09-23 | 128 | aaamt
2015-09-24 | 0 | aaamtx2
.....
I type SELECT * FROM test where source="aaamt" but I get the following error,
ERROR: column "aaamt" does not exist
LINE 1: SELECT * FROM test where source = "aaamt";
Why am I getting this error and how to I fix it?
You need to use single quote instead of double quote
SELECT * FROM test where source = 'aaamt'
Double quotes indicate to Postgres that you are trying to specify an identifier such as a column or table name. Use single quotes for string literals, and your query should work:
SELECT *
FROM test
WHERE source = 'aaamt';
To be clear here, you current query is basically being interpreted as this:
SELECT *
FROM test
WHERE source = aaamt;
Here aaamt is being treated as a column name or maybe some other database identifier, but not as a string literal.
Related
This question already has answers here:
MySQL strange behavior when comparing comma-separated string with number
(1 answer)
Error comparing integer with string in MysQL query (1 = '1sk')
(1 answer)
MariaDb 10 condition where id = '1' return same result where id ='1a'
(1 answer)
Closed 1 year ago.
I have this query
SELECT u0_.id AS id_0, u0_.uid AS uid_1, u0_.username AS username_2
FROM user u0_
WHERE u0_.uid = '3XW3Z1zD6JEV1600164577' OR u0_.id = '3XW3Z1zD6JEV1600164577';
that return two results:
user with id 3
user with uid 3XW3Z1zD6JEV1600164577
I tried using sequel pro to confirm and still get wrong result:
If I remove the first 3 from the uid like this XW3Z1zD6JEV1600164577 I get no match. so why is behaving like a LIKE ??
You are comparing an number with a string in which case MySQL will convert both operands to float:
For example, a comparison of string and numeric operands takes place
as a comparison of floating-point numbers.
MySQL is also very forgiving when converting string to numbers:
SELECT CAST('3X' AS FLOAT) -- 3
SELECT CAST('X3' AS FLOAT) -- 0
This explains why it is matching id = 3. So either don't compare different datatypes or convert the integer to string to get expected result:
WHERE u0_.uid = '3XW3Z1zD6JEV1600164577'
OR CAST(u0_.id AS CHAR) = '3XW3Z1zD6JEV1600164577'
This question already has answers here:
PostgreSQL substring get string between brackets
(2 answers)
Closed 1 year ago.
I have the following details column, with varying parameters. How can I get only joblib values? "The Place of joblib is not always the same, so I may bot be able to use substring count"
date:01/12/2014--**--joblib:[snbsd]--**--date_type:no_date--**--max_feat_values:ss,group_filters:[]--**--no_imp_phrases:1--**--variable_facets:auto
date:01/12/2014--**--joblib:[jinxthin]--**--date_type:no_date--**--max_feat_values:ss,group_filters:[]--**--no_imp_phrases:1--**--variable_facets:auto
date:01/12/2014--**--joblib:[snbserv]--**--date_type:no_date--**--max_feat_values:ss,group_filters:[]--**--no_imp_phrases:1--**--variable_facets:auto
date:01/12/2016--**--joblib:[sql12server]--**--date_type:no_date--**--max_feat_values:ss,group_filters:[]--**--no_imp_phrases:1--**--variable_facets:auto
date:01/12/2015--**--joblib:[stfmbinserx]--**--date_type:no_date--**--max_feat_values:ss,group_filters:[]--**--no_imp_phrases:1--**--variable_facets:auto
date:01/12/2011--**--joblib:[ftplikes]--**--date_type:no_date--**--max_feat_values:ss,group_filters:[]--**--no_imp_phrases:1--**--variable_facets:auto
Desired result:
snbsd
jinxthin
snbserv
sql12server
stfmbinserx
ftplikes
Here You go:
substring(substring(var1, position('joblib:' in var1)+8), 1, position(']' in substring(var1, position('joblib:' in var1)+8))-1)
replace var1 with column name containing Your string
With below You can try it out directly on PostgreSQL:
WITH myconstants (var1) as (
values ('date:01/12/2014--**--joblib:[snbsd]--**--date_type:no_date--**--max_feat_values:ss,group_filters:[]--**--no_imp_phrases:1--**--variable_facets:auto')
)
SELECT substring(substring(var1, position('joblib:' in var1)+8), 1, position(']' in substring(var1, position('joblib:' in var1)+8))-1)
FROM myconstants
This question already has answers here:
How to return default value from SQL query
(8 answers)
Closed 2 years ago.
I'm trying to write a SQL Statement (in Microsoft SQL Server) that will always return a value of 0 if there is nothing to be found.
SELECT
ISNULL([ID], 0) AS ID
FROM
ecdb.dbo.tbl_units
WHERE
([GENESYS_NAME] = 'EDRD');
The statement above returns an empty value (nothing), see screenshot.
Now if I enter a true statement that returns a value (replace 'EDRD' with 'ERD'), it returns a value of 1, which is correct.
The reason why I need it to return a value of zero is that I plan to use this statement as a sub query of a master statement where I need the ID (not the GENESYS_NAME) and my table has set aside 0 to be an unknown entry.
Any help?
If you are expecting only 1 row as result you can use an aggregate function like MAX():
SELECT ISNULL(MAX([ID]), 0) AS ID
FROM ecdb.dbo.tbl_units
WHERE ([GENESYS_NAME]='EDRD');
Another way to do it, which will work also if you expect more than 1 rows in the results is with UNION ALL:
SELECT [ID]
FROM ecdb.dbo.tbl_units
WHERE ([GENESYS_NAME]='EDRD')
UNION ALL
SELECT 0
WHERE NOT EXISTS (
SELECT 1 FROM ecdb.dbo.tbl_units
WHERE ([GENESYS_NAME]='EDRD')
)
This question already has answers here:
When do Postgres column or table names need quotes and when don't they?
(2 answers)
Omitting the double quote to do query on PostgreSQL
(5 answers)
PostgreSQL "Column does not exist" but it actually does
(6 answers)
Postgresql Column Not Found, But Shows in Describe
(1 answer)
sql statement error: "column .. does not exist"
(1 answer)
Closed 3 years ago.
So I encountered the following behaviour which surprised me. I first thought DateTime might be a postgres data type but what about BidOpen? Then there is the funny thing with the case of the column name in the error message. I almost feel this has to do with unquoted names being case insensitive. Why do I need to surround the column name in quotes for the query to work?
mydatabase=# select max("DateTime") from fx.candle;
max
---------------------
2019-04-26 20:59:00
(1 row)
mydatabase=# select max(DateTime) from fx.candle;
ERROR: column "datetime" does not exist
LINE 1: select max(DateTime) from fx.candle;
^
HINT: Perhaps you meant to reference the column "candle.DateTime".
mydatabase=# select max(BidOpen) from fx.candle;
ERROR: column "bidopen" does not exist
LINE 1: select max(BidOpen) from fx.candle;
^
HINT: Perhaps you meant to reference the column "candle.BidOpen".
mydatabase=# select max("BidOpen") from fx.candle;
max
---------
125.816
(1 row)
The schema looks like this:
mydatabase=# \d fx.candle;
Table "fx.candle"
Column | Type | Modifiers
-----------+-----------------------------+-----------------------------------------------------------------
id | integer | not null default nextval('fx.candle_id_seq'::regclass)
DateTime | timestamp without time zone |
BidOpen | double precision | not null
BidHigh | double precision | not null
BidLow | double precision | not null
BidClose | double precision | not null
AskOpen | double precision | not null
AskHigh | double precision | not null
AskLow | double precision | not null
AskClose | double precision | not null
symbol_id | integer |
Indexes:
"candle_pkey" PRIMARY KEY, btree (id)
Foreign-key constraints:
"candle_symbol_id_fkey" FOREIGN KEY (symbol_id) REFERENCES fx.symbol(id)
My understanding is that Postgres is not case sensitive regarding column and table names unless you actually create them using double quotes in the beginning. If that be the case, then you would need to forever refer to them using double quotes, to ensure that the proper case literal is being used.
So, to avoid your current situation, you should also avoid creating column/table names in a case sensitive manner.
Your create table should look something like this:
create table fx.candle (
id integer not null default nextval('fx.candle_id_seq'::regclass),
...
datetime timestamp without time zone -- NO quotes here; important!
...
)
I am trying to find out how to write an SQL statement that will grab fields where the string is not 12 characters long. I only want to grab the string if they are 10 characters.
What function can do this in DB2?
I figured it would be something like this, but I can't find anything on it.
select * from table where not length(fieldName, 12)
From similar question DB2 - find and compare the lentgh of the value in a table field - add RTRIM since LENGTH will return length of column definition. This should be correct:
select * from table where length(RTRIM(fieldName))=10
UPDATE 27.5.2019: maybe on older db2 versions the LENGTH function returned the length of column definition. On db2 10.5 I have tried the function and it returns data length, not column definition length:
select fieldname
, length(fieldName) len_only
, length(RTRIM(fieldName)) len_rtrim
from (values (cast('1234567890 ' as varchar(30)) ))
as tab(fieldName)
FIELDNAME LEN_ONLY LEN_RTRIM
------------------------------ ----------- -----------
1234567890 12 10
One can test this by using this term:
where length(fieldName)!=length(rtrim(fieldName))
This will grab records with strings (in the fieldName column) that are 10 characters long:
select * from table where length(fieldName)=10
Mostly we write below statement
select * from table where length(ltrim(rtrim(field)))=10;