SQLDeveloper removes spaces from a blankline when inserting or updating - sql

When I execute the script (whithout comments and replacing the "." by spaces):
SET SQLBLANKLINES ON
UPDATE ANY_TABLE SET VARCHAR2_COLUMN='Hello
..... -- 1st empty line with trailing spaces
Kitty
...' -- last empty line with trailing spaces
WHERE ID='something';
it is saved in the database like:
'Hello
-- 1st empty line WITHOUT spaces
Kitty
...' -- last empty line WITH trailing spaces
So the spaces in the 1st blankline are lost but not on the last one. And I am using "SET SQLBLANKLINES ON" !!
Can anybody explain me what I'm doing wrong? Or What misconception I have?
The problem is the same with an insert and is independent of using simple spaces or tabs.
In fact, thanks to Alex Pool, we can simplify the example:
Why are the next queries returning different value lengths?
select length('Hello
...... --6 characters (spaces)
Kitty') from dual;
-- returns 12
select length('Hello
x..... -- 6 characters (an 'x' + 5 spaces)
Kitty') from dual;
-- returns 18
select length('Hello
'||'......
Kitty') from dual;
-- returns 18
What I am using:
SQLDeveloper 20.2.0.175
Oracle database 19
The problem may be related to this other one

The q'[]' function is your friend.
create table multiline_strings
(id integer,
words varchar2(256));
insert into multiline_strings (id, words) values
(
1,
q'[Hello
Kitty
]');
See this question/answer - probably should close this as a duplicate.

Related

SQL Server Not Recognizing Leading Spaces in Select Query

I get a TXT file from one of our sources systems each night. It's basically a terminal report with headers, footers, titles, column headers, sub-totals, etc. I'm trying to scrape discrete data elements from the file using SQL Server. The file is being FTP'd to a Windows file share. The source system is AIX and the file's encoding is UTF-8, with an EOL marker of LF. I'm using SSIS to import the raw text report into a single column table with each report row being one record in my table. The column I'm storing the rows in is a VARCHAR(240) and I'm using SQL Server 2016.
For the report rows that I want to use, the one thing they have in common is that they all start with two spaces. Here's an example record from a text report I've loaded to SQL:
COLUMN_NAME
AD DEPT 0 0 0 0 0 0 0 0 0 0.0 0 0 0.00 0.00 0 0 0 0.0
When I try to select the record using:
SELECT *
FROM TABLE_NAME
WHERE COLUMN_NAME LIKE ' %';
No rows are returned in my result set. However, REPLACE seems to recognize the the row starts with two spaces.
So this:
SELECT REPLACE([COLUMN_NAME],' ','$')
FROM TABLE_NAME
Returns this:
COLUMN_NAME
$$AD$DEPT$$$$$$0$$$$$0$$$$$0$$$0$$$$$0$$$$$0$$$$0$$$$$0$$$$0$$$0.0$$$0$$$$$$0$$0.00$$0.00$$$$$$0$$$$$$0$$$$$$0$$$0.0
Can someone help me understand why REPLACE sees that there are two leading spaces in the row but the plain SELECT does not?
If you know that you have 2 spaces as the start of the column then you can use the single character wildcards in your LIKE expression.
For example:
CREATE TABLE testString (
sampleImport varchar(20)
)
GO
INSERT testString
VALUES (' AD DEPT 0 0 0'), ('BD DEPT 0 0 0')
GO
SELECT *
FROM testString
WHERE sampleImport LIKE '[ ][ ]%'
GO
SELECT *
FROM testString
WHERE sampleImport NOT LIKE '[ ][ ]%'
GO
The [] is used to signify a single character - the specific character is the one enclosed in the brackets. So placing a single space within the brackets allows you to match the spaces.
I also noted that your criteria had only a single space before the % character. Although I cannot see it documented as such, I suspect that your version is failing as it is not seeing a leading space as a valid character for the wildcard (although by definition it should). When using LIKE ' %' it works with my test data.

How to remove any special characters from a string even with dot and comma and spaces

INPUT STRING:'HI every one. I want to (2-21-2022) remove the comma-dot and other any special character from string(123)'.
OUTPUT STRING:'HI every one I want to 2-21-2022 remove the #comma dot and other any special #character from string 123'
Thanks IN Advance.
If what you said in title:
remove any special characters from a string even with dot and comma and spaces
means that you'd want to keep only digits and letters, then such a regular expression might do:
SQL> with test (col) as
2 (select 'HI every one. I want to (2-21-2022) remove the comma-dot and other any special character from string(123)' from dual)
3 select regexp_replace(col, '[^[:alnum:]]') result
4 from test;
RESULT
---------------------------------------------------------------------------------
HIeveryoneIwantto2212022removethecommadotandotheranyspecialcharacterfromstring123
SQL>
On the other hand, that's not what example you posted represents (as already commented).

Comma inside like query fails to return any result

Using Oracle db,
Select name from name_table where name like 'abc%';
returns one row with value "abc, cd" but when I do a select query with a comma before % in my like query, it fails to return any value.
Select name from name_table where name like 'abc,%';
returns no row. How can I handle a comma before % in the like query?
Example:
Database has "Sam, Smith" in the name column when the like has "Sam%" it returns one row, when i do "Sam,%" it doesn't return any row
NOT AN ANSWER but posting it as one since I can't format in a comment.
Look at this and use DUMP() on your own machine... see if this helps.
SQL> select dump('Smith, Stan') from dual;
DUMP('SMITH,STAN')
-----------------------------------------------------
Typ=96 Len=11: 83,109,105,116,104,44,32,83,116,97,110
If you count, the string is 11 characters (including the comma and the space). The comma is character 44, and the space is character 32. If you look at YOUR string and you don't see 44 where the comma should be, you will know that's the problem. You could then let us know what you see there (just for that character, I understand posting "Leno, Jay" would be a violation of privacy).
Also, make sure you don't have any extra characters (perhaps non-printable ones!) right before the comma. Just compare the two strings you are using as inputs and see where the differences may be.

Oracle: remove first 4 characters from a string

So I want to remove the first 4 characters from a string in oracle. Those characters can be different every time.
In my case I need to take away the first 4 characters of an IBAN and put them at the end of the string. I got the part of putting them to the end of the string but I can't get the first 4 characters to be removed. Every solution I find on the internet removes specified characters, not characters from a certain position in the string (1 to 4).
I used the code below to get the first 4 characters to the end of the string and wanted to try something similar for removing them at the front but without success.
SELECT SUBSTR(iban_nummer, 1, 4) INTO iban_substring FROM dual;
iban_nummer := iban_nummer || iban_substring;
See the docs:
substring_length ...
When you do not specify a value for this argument, then the function returns all characters to the end of string. When you specify
a value that is less than 1, the function returns NA.
So iban_nummer := substr(iban_nummer, 5) || substr(iban_nummer, 1,4) should work. The first part selects all characters beginning from the 5th, the second character numbers 1..4.
update table_name set col_name=substr(col_name,5);
try regexp, like:
SELECT regexp_replace(t.iban_nummer,'(.{4})(.*)','\2\1') FROM t;
Alternative way using regexp :
SELECT regexp_replace(t.iban_nummer,'^.{4}(.*)','\2\1') FROM dual;

T/SQL - String Manipulation

I have the below query.
SQLFiddle
I can only have possible 7 characters (- and A-Z combined)
There can only be ONE "-" for first 5 characters (Monday to Friday)
For Saturday and Sunday, we can only have one character or dash
I am replacing Sa and Su with just S
However, whenever they are passing in TWO dashes for Saturday AND/OR Sunday, I need to replace them with ONE dash for each
so length can only be 7 after all manipulations.
I have tried w/e I could but getting stuck at the two vs one dash scenario for Saturday/Sunday position.
Please help! I will keep this updated as I find more.
THX in ADV
Code:
CREATE Table TempTable (string varchar(50))
INSERT INTO TempTable (string)
VALUES ('MTWRFSS')
,('MTWRFSaS')
,('MTWRFSaSu')
,('----F--')
,('----F----')
,('MT------')
,('MT------')
,('----FSa--')
,('----FSa-')
,('----FS--')
,('----FS-')
,('----F-Su')
,('----F--Su')
,('----F-S')
,('----F--S')
UPDATE TempTable
SET string = REPLACE(REPLACE(RTRIM(LTRIM(string)),'SA','S'),'SU','S')
SELECT string
,LEN(String) AS stringLengh FROM TempTable
--DROP TABLE TempTable
Try to manipulate only characters after 5th, because from MON to FRY you always have 1 -.
So I think this will work:
SELECT
string as InitialString
,LEFT(LEFT(String,5) + replace(replace(replace(RIGHT(String,LEN(String)-5),
'Sa','S'),'Su','S'),'--','-') + '--',7) as FinalString
FROM TempTable;
You have to cut string into 2: left 5 and the rest. Then using several replaces you can have correct Sat/Sun combination. Concatenate both and you will have final decision.
Also 2 more dashes have to be added and the you have to take only LEFT 7, because if you have '--' it will be replaced with '-'.