migrating carriage returns from oracle to sql server - sql

I've been trying to migrate some carriage returns from an oracle varchar2 field to a sql server varchar field and it's not working at all.
I had working code in Oracle of the form:
'_Person making Contact__' ||chr(13) ||
which works perfectly fine in Oracle, but seems to get lost by the time SSIS has extracted the data to sql server.
I tried to pass the below:
'Name: ' || nvl2(p.surname || p.forename ,p.surname || ', ' || p.forename,'None Recorded') || 'char(13)' ||
'Organisation: ' || nvl(a.agency_name, 'None Recorded')
as ContactDetails
but the carriage returns end up embedded as text. e.g "Person making Contact___char(13)Name: None"
I've tried replacing the char (13) in various ways at the sqlserver end but cannot get the results to be anything other than char(13)s embedded in the text.
e.g
update mytable set [ContactDetails] = REPLACE(contactdetails,'char(13)',CHAR(13))
update mytable set [ContactDetails] = REPLACE(contactdetails,'char(13)',''' + CHAR(13) + ''' )
Things like "select myfield + char(13) + 'test text' give me the results I'm after so I can rule out really daft mistakes.
Can anyone offer any suggestions? At Oracle or sqlserver end is fine by me :)

don't know how this didn't work before but Jayvee has pointed me in the right direction. Re-tested this and it works now...
update mytable
set [ContactDetails] = REPLACE(contactdetails,'char(13)',CHAR(13))
Thanks all

Related

PL/SQL Query to check row's length

I'm having some trouble on solving this.
I have to check on the table, if there's any row that exceed the length of 34 characters (for this, I'm using this first part of the query using Lenght command), if found, return the error with the variable 'END_CNPJ$' (that's being populated by the second part of the query 'ENDERECO') so the user can see which row has more than 34 characters. Is this code correct (probably not)? If it isn't, how can I fix it?
SELECT
LENGTH(CONCAT (CONCAT (CONCAT(CONCAT (CONCAT (CONCAT (CONCAT (
'', T.TTIPO_LOGR),
''), T.TENDERE),
''), T.NNRO_ENDER),
''),T.TCOMPL_ENDER) ),
T.TTIPO_LOGR || ' ' || T.TENDERE || ', ' || T.NNRO_ENDER || ' ' || T.TCOMPL_ENDER || ' - ' || TMUNICI || ' CNPJ: ' || T.NCGC AS ENDERECO
INTO CHARACTER_COUNT$, END_CNPJ$
FROM TBENDER T
WHERE T.CEMPRES = :ENDER_BLK.CEMPRES;
IF CHARACTER_COUNT$ > 34 THEN
MSG_ALERT_COSMO(' You exceeded 34 character for this address: ' || END_CNPJ$ );
RAISE FORM_TRIGGER_FAILURE;
END IF;
I hope I'm not violating any rule, just got here yesterday :D
TIA
That's actually Oracle Forms, is it not? raise form_trigger_failure and :ender_blk smell so.
In that case, the only drawback might be possibility of no rows in that table for block item value (which will raise no_data_found) or two or more rows for it (which will then raise too_many_rows).
Other than that, this should be OK.
Though, it is kind of unclear why you nicely concatenated values (using the double pipe || operator) for END_CNPJ$ and nested that many concat functions for CHARACTER_COUNT$.
Also, you didn't post the whole trigger code (missing declarations, begin-end keywords, perhaps something else).
But, as I said, in general - that's OK.

How do I parameterize table & column in a Postgres-custom-function, selecting PK if value exists, otherwise insert it and return PK anyways?

Trying to do what I specified in the title, I already got the upsert-functionalities working, however when I try to parameterize it, I'm just out of my depth and can't debug it.
My query:
CREATE OR REPLACE FUNCTION custom_upsert(target_value_input text,
target_table_input text,
target_column_input text,
OUT pk_output int)
LANGUAGE plpgsql AS
$func$
BEGIN
LOOP
execute 'SELECT id '
' FROM ' || target_table_input ||
' WHERE ' || target_column_input || ' = ' || target_value_input ||
' INTO pk_output';
EXIT WHEN FOUND;
execute 'INSERT INTO ' || target_table_input || 'AS o ( ' || target_column_input || ' )'
' VALUES ( ' || target_value_input || ' ) '
' ON CONFLICT ( ' || target_column_input || ' ) DO NOTHING '
' RETURNING o.id'
' INTO pk_output';
EXIT WHEN FOUND;
END LOOP;
END
$func$;
now when I try to use the function, I get:
ERROR: syntax error at or near "INTO"
LINE 1: ...module WHERE artifact_id = artifact_id_example_1 INTO pk_ou...
^
QUERY: SELECT id FROM maven_module WHERE artifact_id = artifact_id_example_1 INTO pk_output
CONTEXT: PL/pgSQL function custom_upsert(text,text,text) line 4 at EXECUTE
What puzzles me about this is the fact that this syntax works fine in an unparameterized version:
https://dbfiddle.uk/?rdbms=postgres_14&fiddle=765389a746d3a392bc646fbedb7ed3b3
My attempts at parameterization:
https://dbfiddle.uk/?rdbms=postgres_14&fiddle=1bffab45d8a9587342a7c3253ea35fc8
https://dbfiddle.uk/?rdbms=postgres_14&fiddle=de6ba235aa21dae33b922f8fddac3b63
Thank you very much in advance, first time posting so if there's anything I should do differently when asking a question, I'm happy about feedback
edit: this is my function call:
-- should return pk of existing artifact_id
SELECT custom_upsert('artifact_id_example_1', 'maven_module', 'artifact_id');
-- should return pk of new artifact_id
SELECT custom_upsert('artifact_id_example_2', 'maven_module', 'artifact_id');
Do not concatenate strings like that. The function format() makes your life much easier (safer), e.g.
EXECUTE format('INSERT INTO %1$I AS o (%2$I)
VALUES (%3$L) ON CONFLICT (%2$I) DO NOTHING RETURNING o.id',
target_table_input,
target_column_input,
target_value_input) INTO pk_output;
%I will wrap the identifiers with double quote, which is handy when tables or columns are case sensitive of contain special characters.
%L will wrap the literals with single quotes
1$, 2$ and 3$ are the variables positions provided in the format() call, which is quite handy if one variable is used more than once.
Demo: db<>fiddle

Replacing a space-looking character in SQL Server

I have a database backup that I restored in SQL Server 2014 and I am using SQL Server Management Studio 18 (15.0.18206.0)
In a column of type text strings of XML files are stored. Yes, I know that the datatype text is obsolete. But I cannot change the datatype in the old program so easily.
Now I want to remove the string <SCHNITTLINIE>.
In the cell, it looks like this: ..._MIN> <SCHNITTLINIE> <S...
SELECT CAST(REPLACE(CAST(xml AS NVARCHAR(MAX)), '<SCHNITTLINIE>', '') AS TEXT)
FROM [dbo].[XMLdatei]
WHERE xml LIKE '%SCHNITTLINIE%'.
As a result, I get: ...MIN> <S...
So far so good.
I'd like to remove the "spaces" as well.
SELECT CAST(REPLACE(CAST(xml AS NVARCHAR(MAX)), ' <SCHNITTLINIE> ', '') AS TEXT)
FROM [dbo].[XMLdatei]
WHERE xml LIKE '%SCHNITTLINIE%'
Now he finds the string ' ' but not anymore.
At the beginning of the cell is a <?xml version="1.0". So I tried the following to test the spaces:
SELECT CAST(REPLACE(CAST(xml AS NVARCHAR(MAX)), 'xml ', 'xxx') AS TEXT)
FROM .[dbo].[XMLdatei].
WHERE xml LIKE '%SCHNITT%'.
As a result I got the following: <?xxxversion="1.0"
The following also works:
SELECT CAST(REPLACE(CAST(xml AS NVARCHAR(MAX)), 'xml' + char(32), 'xxx') AS TEXT)
FROM [dbo].[XMLdatei]
WHERE xml LIKE '%SCHNITT%'
Check, the test was successful. :)
Only with the string ' <SCHNITTLINIE> ' this does not work.
How can I find out what the characters before < or after > are?
How can I remove them without knowing what the characters are?
Thanks for your help.
Have a nice weekend.
Christoph

Update column to remove everything before and including a space in SQLite

I have looked at these somewhat related articles for ideas, and did a number of searches like "sqlite ltrim %" but I'm having a surprisingly hard time figuring out what I'm doing wrong.
What I want to do is remove everything before, and including a space in SQLite.
One major issue is that I have to use SQLite from PortableApps, and cannot use MySQL for this.
I tried:
UPDATE `temp_Week17` SET `c_name`=ltrim(%,' ');
I was thinking I could trim the space from the front easily enough after, but I get a "near '%': syntax error."
Just to see what would happen, I also tried:
UPDATE temp_Week17
SET c_name = LEFT(c_name, CHARINDEX(' ', c_name) - 1)
WHERE CHARINDEX(' ', c_name) > 0
And that produces a "near '(': syntax error."
Another one I tried was:
UPDATE temp_Week17 SET c_name = ltrim(c_name, ' ');
That one runs succesfully but does not change anything.
Updated to add per #scaisEdge:
The column is arranged as so:
|c_name|
--------
|John Smith|
|Abe Lincoln|
|Taco Cat|
And the desired outcome is:
|c_name|
--------
|Smith|
|Lincoln|
|Cat|
Thanks very much for any help!
You should concatenate the two string (the trimmed and the space)
UPDATE temp_Week17 SET c_name = ltrim(c_name) || ' ' ;
or you need the space before the trimmed
UPDATE temp_Week17 SET c_name = ' ' || ltrim(c_name) ;
based on the sample provide seems you need substring
UPDATE temp_Week17 SET c_name = ltrim(substr( c_name, instr(c_name, ' '))) ;

Using convert in a select statement for Jasper Reports

I'm trying to retrieve some data (String) from a database, and perform some actions on them that would require them to be converted to DateTime.
First off, though, I'd like to know if it is possible to pull data of type String from the Database, Convert them into DateTime, and then use them in a report.
I can't change the database, unfortunately, which I believe would help greatly.
So I looked up convert commands, and came up with this:
(convert(datetime[10], fld7, 101))
Which is used here (as DTEEEFFEC):
SELECT fld2 as EMPNAME, fld5 as EMPCLASSFROM,
fld6 as EMPCLASSTO, (convert(datetime[10], fld7, 101)) as DTEEFFEC,
(b.LNAME || ', ' || b.FNAME || ' ' || b.MNAME) AS HROFF
FROM dbase.table1
LEFT JOIN dbase.person_info ON $P{hrStoreOp}= b.ID_PERSONAL
WHERE compname = $P{COMPNAME} order by fld7,fld2 asc
It returns an error, though: missing right parenthesis, or, if I remove the brackets and the 10, this one: "DATETIME": invalid identifier
Edit: I'm so sorry I left this out, but does using an Oracle Database mean anything? I thought they both use similar SQL statements.
Try this
CONVERT(VARCHAR(10),GETDATE(),110)
http://www.w3schools.com/sql/func_convert.asp
SELECT fld2 as EMPNAME, fld5 as EMPCLASSFROM,
fld6 as EMPCLASSTO, CONVERT(VARCHAR(10),fld7, 101) as DTEEFFEC,
(b.LNAME || ', ' || b.FNAME || ' ' || b.MNAME) AS HROFF
FROM dbase.table1
LEFT JOIN dbase.person_info ON $P{hrStoreOp}= b.ID_PERSONAL
WHERE compname = $P{COMPNAME} order by fld7,fld2 asc