I am trying to perform a simple task in SQL Server and receiving an error. I have a table called World.Internet_CountryYear. The table has year columns (i.e. 1960, 1965, etc.) with numerical data in the fields, but the data type for the columns is varchar(max). Currently, the fields that do not have data have "" in them, which I would like to replace with 0.00. So, I try to run a simple update command as follows:
UPDATE World.Internet_CountryYear
SET 1960 = '0.00'
WHERE 1960 = '""'
For some reason, I receive an error saying "Incorrect syntax near '1960'"
Is there a problem with the DDL of this table? What am I missing here? I have tried specifying World.Internet_CountryYear.1960 as well, but still receive a similar error: Incorrect syntax near '.1960'
Any suggestions?
Enclose the column name with square brackets (you need to use the square brackets when you are using skeywords, special characters in the column names)
Try this:
UPDATE World.Internet_CountryYear
SET [1960] = '0.00'
WHERE [1960] = '""'
Related
I am trying to parse JSON with poorly formatted code into sql table, however some of the values are creating problem.
Database is oracle 19.2
The json data is :
Insert into r_data (id,data)values
(1,'{'"View":"100",
"Assignment Title":"Collect all snippets from the Library",
"Status":"In Progress",
"Active/Not Active":"Depends"}');
I want the result as:
View Assignment_Title Status Active_Not_Active
100 Collect all snippets from Library In progress Depends
When i write the query,
select * from r_data x JSON_TABLE(x.data,'$',
COLUMNS (view NUMBER(10) PATH '$.view')
--for the first column view , it throws error of invalid identifier at the word view.
It only works fine for column Status as probably its single word and because view is keyword it is throwing problem. However i cannot change these names and want column View as View, Assignment Title as Assignment Title and Active/not Active as Active_not_Active.
How can this be done?
The main issue is that view is a reserved keyword, and therefore it can't be used as a column name (unless in double quotes - which is best avoided). You wanted to create a column named view from the data in your JSON - that's why you got the "invalid identifier" error.
But there are numerous other errors in your code; if you even made it as far as the "invalid identifier" error, that means that the code you posted is not the code you ran.
For example, even in the insert statement, you have an extra single-quote after the opening brace. That means that the opening brace is a string (one character) and the rest is who-knows-what. I had to remove that errant single-quote to make the insert work. How were you able to use that obviously syntactically incorrect statement?
In the select statement you are missing a comma after the alias x (before the keyword JSON_TABLE). No way you would get the "invalid identifier" error with that syntax error in the code.
In the JSON_TABLE function, there should be no comma between '$' and the COLUMNS clause. With that comma there, you would get a different error, not "invalid identifier" - so I don't believe that what you posted is your real code.
Etc. If all you got was an "invalid identifier" error, just choose a different name for the column (view won't work) and see what happens. Although... there is one more mistake, and it will result in an unexpected result. JSON is case sensitive. The attribute name in the JSON is View, with a capital V. So you must reference it as $.View in JSON_TABLE; you have $.view, which doesn't correspond to any attribute in your JSON, so you will get null for that column (if you don't change it to match capitalization from the JSON).
Here is the complete example, with all the errors corrected.
First, create the table. I do it all in one step:
create table r_data (id,data) as select 1,'{"View":"100",
"Assignment Title":"Collect all snippets from the Library",
"Status":"In Progress",
"Active/Not Active":"Depends"}' from dual;
Then, here is the query and its output. Notice the double-quotes around property names with embedded spaces (and forward slash, etc.).
select x.id,
j.view_, assignment_title, j.status, j.active_not_active
from r_data x,
json_table(x.data,'$'
columns (view_ number(10) path '$.View',
assignment_title varchar2(50) path '$."Assignment Title"',
status varchar2(20) path '$.Status',
active_not_active varchar2(20) path '$."Active/Not Active"'
)
) j
;
ID VIEW_ ASSIGNMENT_TITLE STATUS ACTIVE_NOT_ACTIVE
-- ----- ------------------------------------- ----------- ------------------
1 100 Collect all snippets from the Library In Progress Depends
I have a problem in getting the sum of one of my column names in database (PRICE). If I insert 1000.00 and 3600.00 to get the sum of them, I am getting 4.00 instead of 4600.00. But when the total is below 1000 I get the correct answer.
It appears that your price_per_case column is char or varchar column rather than a numeric column such as decimal(10,2). So, it is converting the column to double but ignoring everything after the , character (and probably issuing warnings during the conversion). You can verify this by trying the following:
select '1,234' + '3,456';
You will end up with 4;
You should try running:
update ordered set price_per_case = replace(price_per_case, ',', ''); /* get rid of commas */
Then you should change the type of this column to decimal(10,2) or some appropriate precision.
Maybe the machine when you are programming have different “Locale” of MySql Server (are both in the same machine)?
In this case also format currency/double/decimal is different.
To avoid that you need to change Locale in you .NET method/sub/function before/into you get query result
Threading.Thread.CurrentThread.CurrentCulture = New CultureInfo("it-IT")
Threading.Thread.CurrentThread.CurrentUICulture = New CultureInfo("it-IT")
or change Locale in MySql
SET lc_time_names = 'it_IT'; Here continue your query
In this example I have used Italian Locale
Trying to run this query -
INSERT INTO rmedvedeva993#gmail.com (url,unix)
VALUES (#https://youtu.be/xXsuqrhD8pw,#1500152563.66077);
after reading about this issue tried wrapping database like this- rmedvedeva993#gmail.com
getting an error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '://youtu.be/xXsuqrhD8pw,#1500152563.66077)' at line 1
not quite sure what's the issue here,
P.S.: my columns are formated as char(255)
`
#hhttps://youtu.be/xXsuqrhD8pw and #1500152563.66077 aren't valid.
Neither is an email address as the name of a table. If you MUST use an email address as a table name, enclose it in backticks. But think long and hard about why you're doing that, then don't do it.
You probably want VALUES ('https://youtu.be/xXsuqrhD8pw',1500152563.66077); .
The # symbol in MySQL's dialect of structured query language denotes a user-defined variable. So you could have this:
#url := 'https://youtu.be/xXsuqrhD8pw';
#ts := 1500152563.66077;
INSERT INTO table (url,unix) VALUES (#url,#ts);
You need to quote your strings (whether char(xx), varchar(xx), or any other type which is represented as a string); and, when the names of your tables are not just letters and numbers, you have to quote them with the backtick quote: `. You most probably don't won't either the # symbol.
INSERT INTO `rmedvedeva993#gmail.com` (url,unix)
VALUES ('https://youtu.be/xXsuqrhD8pw','1500152563.66077');
Side Note: Is your table really named rmedvedeva993#gmail.com? Can you post your table definitions (use SHOW CREATE TABLE table_name).
I'm working on the data pipeline. In one of the steps CSV from S3 is consumed by RedShift DataNode. My RedShift table has 78 columns. Checked with:
SELECT COUNT(*) FROM information_schema.columns WHERE table_name = 'my_table';
After failed RedshiftCopyActivity 'stl_load_errors' table shows "Delimiter not found" (1214) error for line number 1, for column namespace (this is second column, varchar(255)) on position 0. Consumed CSV line looks like that:
0,my.namespace.string,2119652,458031,S,60,2015-05-02,2015-05-02 14:51:02,2015-05-02 14:51:14.0,1,Counter,1,Counter 01,91,Chaymae,0,,,,227817,1,Dine In,5788,2015-05-02 14:51:02,2015-05-02 14:51:27,17.45,0.00,0.00,17.45,,91,Chaymae,0,0.00,12,M,A,-1,13,F,0,0,2,2.50,F,1094055,Coleslaw Md Upt,8,Sonstige,900,Sides,901,Sides,0.00,0.00,0,,,0.0000,0,0,,,0.00,0.0000,0.0000,0,,,0.00,0.0000,,1,Woche Counter,127,Coleslaw Md Upt,2,2.50
After simple replacement ("," to "\n") I have 78 lines so it looks like the data should be matched... I'm stuck on that. Maybe someone knows how I can find more information about the error or see the solution?
EDIT
Query:
select d.query, substring(d.filename,14,20),
d.line_number as line,
substring(d.value,1,16) as value,
substring(le.err_reason,1,48) as err_reason
from stl_loaderror_detail d, stl_load_errors le
where d.query = le.query
and d.query = pg_last_copy_id();
results with 0 rows.
I figured it out and maybe it will be useful for someone else:
There were in fact two problems.
My first field in the redshift table was of the type INT IDENTITY(1,1) and in CSV I had 0 value there. After removing the first column from CSV, even without specified columns mapping everything was copied without a problem if...
DELIMITER ',' commandOption was added to S3ToRedshiftCopyActivity to force using comma. Without it RedShift recognized dot from namespace (my.namespace.string) as delimiter.
You need to add FORMAT AS JSON 's3://yourbucketname/aJsonPathFile.txt'. AWS has not mentioned this already. Please note that this is only work when your data is in json form like
{'attr1': 'val1', 'attr2': 'val2'} {'attr1': 'val1', 'attr2': 'val2'}
{'attr1': 'val1', 'attr2': 'val2'} {'attr1': 'val1', 'attr2': 'val2'}
So I have a form that can get data from a database by its ID (auto-incremented column(Primary Key)) and display all fields in the <input> tags via value properties. And when I submit the form I want it to either INSERT a new row if the ID from the ID column doesn't already exist and if it does I want to UPDATE the rest of the data in the row with the same ID.
I have been trying to research this, but no one seems to be doing the same thing I am trying to do, its always slightly different. I found a REPLACE INTO and created it like below:
$sqlString = 'REPLACE INTO coursework
SET cwID=`'. $cwID .'`,
cwTitle=`'. $cwTitle .'`,
cwContent=`'. $cwContent .'`,
cwProgress=`'. $cwProgress .'`,
cwDue=`'. $cwDue .'`;
All the $cw[] variables being content received from $_POST method.
I keep getting a Error code: 1054-Unknown column '6' in 'field list' -
the "Unknown column '6'" is mysql trying to call $cwID (value of $_POST['cwID']) instead of the cwID column(which is the Primary Key for my table). I feel like there is something simple and stupid I am missing but I have never used this REPLACE INTO method before.
I saw a post about INSERT IGNORE INTO and INSERT ... ON DUPLICATE KEY UPDATE but both of those sound more destructive than what I am looking for.
I just want to make sure that the table is updated if the cwID exists and the auto-increment is kept in tact, or a new row is added if there is no ID. Should I just run a SELECT query to see if it exists and INSERT/ UPDATE appropriately?
Remove Replace the back-ticks (`) surrounding the strings with single quotes (').
MySql is trying to find a column named by the strings you are using as values.
See http://dev.mysql.com/doc/refman/5.5/en/identifiers.html
Replace the backticks with single quotes:
$sqlString =
"REPLACE INTO coursework SET cwID='$cwID', cwTitle='$cwTitle', cwContent='$cwContent', cwProgress='$cwProgress', cwDue='$cwDue'";
Also, note that " will interpolate your variables.