PostgreSQL database considering the script as ANSI instead of UTF8 - sql

I have executed a script that updates a column in a database and that worked well.
The script would be having an update statement as below. It is trying to update the display_name with an inverted comma in it.
Update table1
Set display_name = 'I'm Kumar'
Where internal_name = 'IK';
When I executed the same script in another database, it is updating the display name with some special character in place of an inverted comma. Seems like the script is being considered as Ansi encoded format instead of UTF-8 format.
Please help me to understand why is this happening. Will there be any setting at the database level to change.

Yes, and that setting is client_encoding.
The default value is specified in the server configuration, and the client has to override it if desired:
SET client_encoding = 'UTF8';

Related

Editing data in grid vs using an update script

I tried using Toad 12.1 DBA and SQLDeveloper to achieve my goal but i got same results.
When i update NVARCHAR2 cell in grid result, i can set and commit '€' sign as value. But when i execute an update script to do the same thing, it prints '?' character as data.
Here is the result when i edit and commit data in grid:
Here is the problem when i use update script to do the same thing:
I tried using differend NLS_LANG parameters but they did not work either.
AMERICAN_AMERICA.WE8ISO8859P9
AMERICAN_AMERICA.AL16UTF16
Database NLS parameters is here:
SELECT * FROM NLS_DATABASE_PARAMETERS where PARAMETER in ('NLS_CHARACTERSET','NLS_NCHAR_CHARACTERSET');
PARAMETER VALUE
------------ ------------
NLS_CHARACTERSET WE8ISO8859P9
NLS_NCHAR_CHARACTERSET AL16UTF16
Create script for the simple table:
CREATE TABLE AAA
(
NVARCHAR2COL NVARCHAR2(50)
);
I also tried using sqlplus to execute update script, but it printed question mark as data as well.
Edit: one more thing, using update script with unistr function works but i need to update data by using readable text:
UPDATE AAA SET NVARCHAR2COL = unistr('\20AC');
COMMIT;
Solution: using #Nationalized annotation on my JPA entities for NVARCHAR fields solved my problem.
Default data type of Text Literal is CHAR. CHAR strings can contain only ASCII symbols and there is no '€' symbol in ASCII table, so '€' is converted to '?' during statement compilation.
To use national character set string value must be prefixed with N. N function converts the data at compilation time to NCHAR.
UPDATE AAA SET NVARCHAR2COL = N'€'; should work.
Alternatively UPDATE AAA SET NVARCHAR2COL = to_nchar('€'); can be used.
When updating a value in a grid the development tool does the same transparently for the user.

Can't update data in NVarChar column of a table in SSMS

I have a table in an SSMS database:
I am trying to update the contents of the "Name" column by removing the leading spaces from every entry. Following the question How to delete leading empty space in a SQL Database Table using MS SQL Server Managment Studio, I am therefore trying to run the following:
UPDATE ReferenceHierarchy set Name = LTRIM(Name)
The problem is that when I try to run it, it says "Name" is an invalid column. When I look at the code completion options for "Name", it sees the three fields ID, ParentID, and Sequence. Interestingly, these are the three non-NVarChar fields.
What could be the problem? And how can I fix it?
LTRIM only removes the leading NORMAL Spaces. In your case it may be the Tab space.
Try this for TAB SPACE
UPDATE ReferenceHierarchy set Name = LTRIM(REPLACE(Name,(CHAR(9)),''))
If your space character not a TAB Space and Normal space then it might by non Unicode character
Then Try this
UPDATE REFERENCEHIERARCHY SET NAME = TRIM(LTRIM(CASE WHEN NAME NOT LIKE '[A-ZA-Z0-9]%'
THEN STUFF(NAME, 1, 1, ' ')ELSE NAME END))
TT's comment was the key to solving this one. I actually think there is an interesting moral here: if things are not working the way you think they should, perhaps your enviornment is not what you think it is. What was actually happening was that I had in SSMS an old version of the same database in which the ReferenceHierarchy table did not have a "name" column. And without realizing it, I was running my query against that version. Running it against the correct version of the database solved my problem.

Replace special characters in SQLITE TEXT record

I got a SQLITE database in android application. In order to increase application performance i want to do some refinement on DB before adding it to android app.
In order to do this:
I want to remove/replace special characters in Name field of Account table.
Unicode of those special characters are in range 8204-8207 (0x200C ~ 0x200F).
What is the correct SQL syntax to update Account Table?
SQLite supports the REPLACE function. See this documentation: http://sqlite.org/lang_corefunc.html
Therefore, you should be able to do something like this:
UPDATE Account
SET Name= REPLACE(Name,'char-to-replace','replacement');

Informix: Update Statement Error In WebSphere

I am trying to run this update statement but informix doesn't allow me to.
I have a table, named ITEMS and below I have selected some records from it.
SELECT SHORT_SKU, ITEMS."STYLE" FROM ITEMS;
SHORT_SKU STYLE
--------- -----
01846173 null
01811752 null
01811748 null
Trying to run the below UPDATE statement, informix says syntax error.
UPDATE ITEMS SET ITEMS."STYLE" = 'M' WHERE SHORT_SKU = '01846173';
^ syntax error here
Then I changed (as below) and got "Column (style) not found in any table in the query (or SLV is undefined)."
UPDATE ITEMS SET STYLE = 'M' WHERE SHORT_SKU = '01846173';
How do I update the "STYLE" field?
UPDATE 1
I did a change to one of WAS data source's custom properties, ifxDELIMIDENT. Originally it was blank. So, I changed it to true. Restarted WAS. And I couldn't login to our application. SQLExceptions were thrown by WAS but was not able to see the stack trace because WAS has truncated the last few lines. After changing the property back to blank, I was able to login to our application.
I tried another approach, which was to write a Java class that updates the ITEMMST.STYLE column. I executed this from a shell script. In the shell script, I defined and exported the variable DELIMIDENT with the value 'Y'. But I am still getting 'Syntax error'.
UPDATE 2
I managed to update the column. This is done by adding the 'DELIMIDENT=Y' property at the end of the connection string which will be passed to the DriverManager object when opening the database connection.
But, this won't work for our web application because it uses the WebSphere data source to create the db connection. It would be super if there's a way to set this property in the Informix environment itself.
Try:
UPDATE ITEMS SET "STYLE" = 'M' WHERE SHORT_SKU = '01846173';
It must be that STYLE is a reserved word so you must double-quote it to refer to the column. But standard UPDATE syntax doesn't allow you to prefix column names with the table name in the SET clause (since you can only be updating the columns of one table: the table mentioned in the UPDATE).
The right Syntax would be
UPDATE ITEMS SET STYLE = 'M' WHERE SHORT_SKU = '01846173';
As stated on IBM Documentation but as STYLE is a reserved word i guess your getting problems, Read IBM Recommendation on this.
Anyway you may find a work arround oat this link, otherwise you may consider changing the column name.
I am not aware of STYLE being a keyword in Informix (but I haven't gone to look for it). However, you can usually use keywords as column names etc without much problem.
If you must quote it, you need to set the DELIMIDENT environment variable - the value doesn't matter, but use DELIMIDENT=1 for concreteness. This enables SQL standard 'delimited identifiers', where double quotes surround identifiers (column names, table names, etc) and single quotes surround strings. (Normally, you can use either single quotes or double quotes around strings.)
One other point: if you use delimited identifiers, they also become case-sensitive (whereas normally, identifiers are case-insensitive). So you need to know how the STYLE column is stored in the system catalog. In most databases, they will be in lower-case. There's an outside chance that in a MODE ANSI database, they are stored in upper-case (but it is a while since I looked to make sure).
Use this query:
UPDATE ITEMS SET ITEMS.STYLE = 'M' WHERE SHORT_SKU = '01846173';
I think double quotes not required for column name.
Updated Answer 1:
Error Description-
-217 Column column-name not found in any table in the query
(or SLV is undefined).
The name appears in the select list or WHERE clause of this query but is
not defined in a table and does not appear as a statement local variable
(SLV) definition. Check that the column name or SLV name and the names of
the selected tables are spelled as you intended.
If all names are spelled correctly, you are not using the right tables,
the database has been changed, or you have not defined the SLV. If the
name not found is a reference to a column, that column might have been
renamed or dropped. If the name not found represents an SLV and you
defined the SLV in the statement, make sure that the SLV definition
appears before all other references to that SLV name.
This error message can also appear during the execution of an ALTER TABLE
statement when the engine tries to update views that depend on the table.
More info link
Updated Answer 2:
If not possible to change column name then get more information about SLV.
You can refer following links for description and use of SLV:
link1
link2
link3
There are 2 solutions for this.
Set the Informix JDBC data source 'ifxDELIMIDENT' property to 'true'
Rename the affected table columns
For the 1st option, we had a problem after setting the data source to 'true'. Suddenly all our queries did not work. After much troubleshooting, we found out that by setting the 'ifxDELIMIDENT' property to 'true', it also changed Informix to be case sensitive. In our Java code, we have all the column names in uppercase, and in Informix (Example: resultSet.getString("STYLE")), but the table column names are lowercase (Example: 'style'). This was why after changing this property, we were not able to login to our application Unfortunately this behavior was not documented anywhere in IBM's Info Centre nor in the internet.
We opted for the 2nd option which involved changing the affected column names to another column name (Example: Changed 'STYLE' to 'ITEM_STYLE').

Varchar2 and Oracle quick question

Hi guys I'm using varchar2 for a product name field, but when I query the database from the run SQL command line it shows too many empty spaces, how can I fix this without changing the datatype
here is the link to the ss
http://img203.imageshack.us/img203/20/varchar.jpg
The data that got inserted into the database (probably through some ETL process) had spaces which were not trimmed.
You could update using (pseudo code)
Update Table Set Column = Trim(Column)
If TRIM does not change the results, that tells you that there are not trailing spaces in the actual database rows; they're just being added as part of the formatted screen output.
By default, sqlplus (the command-line Oracle tool you appear to be using) uses the maximum length of the varchar2 column as the (fixed) width when displaying the results of a select statement.
If you want to change this, use the column format sqlplus command before running the select. For example:
column DEPT_NAME format a20
Hi
Try to use trim on both sides,
Update TableName set FieldName = RTrim(LTrim(FieldName))
Regards