I am currently using h2 version 1.4.196. I have a CSV file as follows,
Status,message,code
1,hello,13
2,world,14
3,ciao,26
Following query works as expected & returns proper Status values.
select "Status" from CSVREAD('myfile.csv',
'Status,message,code', null) ;
Following query produces an exception. It seems like the literal string "Status" is being passed to convert instead of the values returned by the
execution of the query. I am not sure if my understanding of convert is
correct. Please help me with fixing this query to return the desired result. Thanks in advance.
select convert("Status", int) from CSVREAD('myfile.csv', 'Status,message,code', null) ;
Caused by: java.lang.NumberFormatException: For input string: "Status"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at org.h2.value.Value.convertTo(Value.java:940)
----------------------Detailed Exception------------------------------
select convert("Status", int) from CSVREAD('myfile.csv',
'Status,message,code', null) ;
Data conversion error converting "Status"; SQL statement:
select convert("Status", int) from CSVREAD('myfile.csv',
'Status,message,code', null) [22018-196] 22018/22018 (Help)
org.h2.jdbc.JdbcSQLException: Data conversion error converting "Status"; SQL statement:
select convert("Status", int) from CSVREAD('myfile.csv',
'Status,message,code', null) [22018-196]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
at org.h2.message.DbException.get(DbException.java:168)
at org.h2.value.Value.convertTo(Value.java:996)
at org.h2.expression.Function.getSimpleValue(Function.java:945)
at org.h2.expression.Function.getValueWithArgs(Function.java:1196)
at org.h2.expression.Function.getValue(Function.java:591)
at org.h2.command.dml.Select$LazyResultQueryFlat.fetchNextRow(Select.java:1459)
at org.h2.result.LazyResult.hasNext(LazyResult.java:79)
at org.h2.result.LazyResult.next(LazyResult.java:59)
at org.h2.command.dml.Select.queryFlat(Select.java:519)
at org.h2.command.dml.Select.queryWithoutCache(Select.java:625)
at org.h2.command.dml.Query.queryWithoutCacheLazyCheck(Query.java:114)
at org.h2.command.dml.Query.query(Query.java:371)
at org.h2.command.dml.Query.query(Query.java:333)
at org.h2.command.CommandContainer.query(CommandContainer.java:113)
at org.h2.command.Command.executeQuery(Command.java:201)
at org.h2.jdbc.JdbcStatement.executeInternal(JdbcStatement.java:186)
at org.h2.jdbc.JdbcStatement.execute(JdbcStatement.java:164)
at org.h2.server.web.WebApp.getResult(WebApp.java:1380)
at org.h2.server.web.WebApp.query(WebApp.java:1053)
at org.h2.server.web.WebApp$1.next(WebApp.java:1015)
at org.h2.server.web.WebApp$1.next(WebApp.java:1002)
at org.h2.server.web.WebThread.process(WebThread.java:164)
at org.h2.server.web.WebThread.run(WebThread.java:89)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NumberFormatException: For input string: "Status"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at org.h2.value.Value.convertTo(Value.java:940)
... 22 more
The way you're using it you're defining the columns and reading all the rows from the csv, so you're getting the first row with Status as well. Then you try to convert the string Status to an integer which obviously results in exeption.
Instead you shouldn't define the columns but let h2 get them from the first row of the csv:
select convert(status,int) from CSVREAD('myfile.csv');
Related
I have got below data in some complex format of json, which is neither a pure json not array.
I have tried to pull the specific values by using below queries that did not work
SELECT json_array_extract_scalar(bankdetails, '$.bank') FROM ade.evangelist_oao.oaocustomfundingupdateaoapplicationsvc ;
Query failed (#20200717_121411_01261_cav8k): line 1:8: Unexpected
parameters (row(bank varchar), varchar(6)) for function
json_extract_scalar. Expected: json_extract_scalar(varchar(x),
JsonPath) , json_extract_scalar(json, JsonPath) [DB Errorcode=1]
SELECT json_extract_scalar(accounts, '$.fundingsrcs.method') FROM ade.evangelist_oao.oaocustomfundingupdateaoapplicationsvc ;
Query failed (#20200717_121600_01270_cav8k): line 1:8: Unexpected
parameters (array(row(fundingsrcs array(row(method varchar,amount
bigint)))), varchar(20)) for function json_extract_scalar. Expected:
json_extract_scalar(varchar(x), JsonPath) , json_extract_scalar(json,
JsonPath) [DB Errorcode=1]
How can i get the b840 value of bankdetails field and CUSTOMFUNDING value of accounts field in this case?
The actual type is ROW (not JSON), so you can retrieve the value with
SELECT bankdetails.bank FROM ...
In a PostgreSQL 9.5 table I have an integer column social.
When I try to update it in a stored procedure given the following JSON data (an array with 2 objects, each having a "social" key) in the in_users variable of type jsonb:
'[{"sid":"12345284239407942","auth":"ddddc1808197a1161bc22dc307accccc",**"social":3**,"given":"Alexander1","family":"Farber","photo":"https:\/\/graph.facebook.com\/1015428423940942\/picture?type=large","place":"Bochum,
Germany","female":0,"stamp":1450102770},
{"sid":"54321284239407942","auth":"ddddc1808197a1161bc22dc307abbbbb",**"social":4**,"given":"Alxander2","family":"Farber","photo":null,"place":"Bochum,
Germany","female":0,"stamp":1450102800}]'::jsonb
Then the following code is failing:
FOR t IN SELECT * FROM JSONB_ARRAY_ELEMENTS(in_users)
LOOP
UPDATE words_social SET
social = t->'social',
WHERE sid = t->>'sid';
END LOOP;
with the error message:
ERROR: column "social" is of type integer but expression is of type jsonb
LINE 3: social = t->'social',
^
HINT: You will need to rewrite or cast the expression.
I have tried changing that line to:
social = t->'social'::int,
but then I get the error:
ERROR: invalid input syntax for integer: "social"
LINE 3: social = t->'social'::int,
^
Why doesn't PostgreSQL recognize that the data is integer?
From the JSON-TYPE-MAPPING-TABLE I was having the impression that JSON number would be auto-converted to PostgreSQL numeric type.
A single set-based SQL command is far more efficient than looping:
UPDATE words_social w
SET social = (iu->>'social')::int
FROM JSONB_ARRAY_ELEMENTS(in_users) iu -- in_user = function variable
WHERE w.sid = iu->>'sid'; -- type of sid?
To answer your original question:
Why doesn't PostgreSQL recognize that the data is integer?
Because you were trying to convert the jsonb value to integer. In your solution you already found that you need the ->> operator instead of -> to extract text, which can be cast to integer.
Your second attempt added a second error:
t->'social'::int
In addition to the above: operator precedence. The cast operator :: binds stronger than the json operator ->. Like you found yourself already, you really want:
(t->>'social')::int
Very similar case on dba.SE:
Querying JSONB in PostgreSQL
I've ended up using:
FOR t IN SELECT * FROM JSONB_ARRAY_ELEMENTS(in_users)
LOOP
UPDATE words_social SET
social = (t->>'social')::int
WHERE sid = t->>'sid';
IF NOT FOUND THEN
INSERT INTO words_social (social)
VALUES ((t->>'social')::int);
END IF;
END LOOP;
How to set decimal value in java prepared statement.
I was tried as follows
My table query
create table A (x decimal(22,0))
In java I tried to set it as
preperedStatmentObj.setLong(1,aLongValue);
But i am getting the following error
java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer
Thank you
Try using setBigDecimal
BigDecimal d = new BigDecimal(aLongValue);
preperedStatmentObj.setBigDecimal(1, d);
I have the following linq to sql query:
DateTime linqdate = (from de in dvlist
where de.DataValue == value
select de.DateTime);
I want to get the date value form database in a datetime variable but I got the following error:
cannot implicitly convert type
'System.Collections.Generic.IEnumerable'
to 'System.DateTime'
any ideas where the problem is? thanks in advance
A Linq query returns an IEnumerable<T> that you can iterate or you can convert it to another type of object (using some extension methods)
In order to get what you want you should do something like this :
var dateTime=(from de in dvlist
where de.DataValue == value
select de.DateTime).FirstOrDefault();
this way you are returning the first element of your enumerable object, or the default value for that type (T, in this case DateTime) if there is no match in the query.
I'm getting this error when trying to run a query that inserts results into a table in sql.
im passing the table name as parameter,how to give the hierarchy value to the insert statement.
here is my code:
declare #pathhere hierarchyid
select #pathhere=Path from SectionDetails where Sectionid=#sectionid and SectionName=#sectionname and Batchid=#batchid and Deptid=#deptid and Schoolid=#schoolid
insert stmt:
set #sqlstmt = 'insert into '+#batch+'(StudentID, StudentName,SectionID,SectionName,BatchID,BatchName, DeptID,DeptName, SchoolID,Path)
values('''+#sectionid+''','''+#sectionname+''','''+#sectionid+''','''+#sectionname+''','''+#batchid+''','''+#batchname+''','''+ #deptid+''','''+#deptname+''', '''+#schoolid+''','+ CAST(#pathhere as hierarchyid)+')'
exec(#sqlstmt)
im getting error in this line:
'+ CAST(#pathhere as hierarchyid)+'
as Invalid operator for data type. Operator equals add, type equals hierarchyid.
can anyone pls help me out how to pass the hierarchy value
You're trying to create a string that can be executed as a statement. So you need to get your hierarchyid into nvarchar(max) instead.
try: #pathhere.ToString()