I have two strings in SQL and the REPLACE function only works on one of them, why is that?
Example 1:
SELECT REPLACE('18 286.74', ' ', '')
Example 2:
SELECT REPLACE('z z', ' ', '')
Example 1's output is still "18 286.74" whereas Example 2's output is "zz". Why does SQL not react the same way to both strings?
UPDATE:
When running select replace('123 123.12', ' ', '') that works fine, still not with '18 286.74'.
Test it the following way.
select unicode(substring('18 286.74', 3, 1))
If the code returns 32 then it's a space, if not, it's a different Unicode character and your replace ' ' won't work.
maybe cast is needed.
UPD: or not(on sql 2005 works fine too)
Are you sure it is a space? i.e. the same whitespace character that you are passing as the second argument? The code you've posted works fine for me on SQL Server 2008.
Re working on your friends PC - perhaps the whitespace got normalized when you sent it to him?
You are probably using non-breakable space.
I could reproduce it by typing ALT+0160 into the number in SELECT REPLACE('18 286.74', ' ', '')
Could you please issue this following:
SELECT CAST('18 286.74' AS BINARY), REPLACE('18 286.74', ' ', '')
by copying the '18 286.74' from REPLACE into CAST?
I was having the same issue and found that it was a char(10) (line feed). when copied out of Managment Studio it became a char(32) but in the record it was a char(10) try
Select Replace(#string, char(13), '')
Related
I have a table in Postgres with following column:
col1
The study was terminated early by the sponsor on 13 January 2014 due to a decision to modify the drug development plan.
Due to positive preliminary results from other palifermin studies.
Asset terminated by PIB
Inconsistent training status of sniffer dogs
This study was terminated early due to poor recruitment
The study was terminated due to lack of recruitment.
The scientific director decided to terminate: low priority study with slow accrual
See Termination Reason in Detailed Description.
Investigator moved to new institution
This study was terminated for administrative reasons
The app was not completed in time to conduct a clinical trial on it within the funding grant's award period
There are leading and lagging spaces in the string and either '\n' or '\t' in between. I tried following queries but seems nothing is working out.
select btrim(col1, '\s') from table;
update table
SET col1 = upper(substring(REGEXP_REPLACE(col1, '(\s+)', '') from 1 for 1)) || lower(substring(REGEXP_REPLACE(why_stopped, '(\s+)', '') from 2));
update table
set col1= regexp_replace(col1, E'[\\n\\r\\f\\u000B\\u0085\\u2028\\u2029]+', ' ', 'g' );
select distinct replace( replace( replace( col1, E'\n', '\n' ), E'\t', '\t' ), E'\r', '\r' )
from table;
Any suggestion would be really helpful here.
To use backslash escapes in string literals, you have to prepend them with an E; see the documentation.
So try
btrim(col1, E' \t\n')
you can use trim() function SQL String Functions and Operators
TRIM([LEADING | TRAILING | BOTH] [characters] FROM string)
you can find example here:
postgresql-trim-function
trim function
I resolved the issue by:
regexp_replace(why_stopped, '\s{2,}', ' ', 'g')
I have a simple insert statement I want to execute using sqlrpgle.
I build the statement first depending on what variables are empty and then want to use exec sql to do the insert.
This does not work.
But entering the same insert statement with subquery in STRSQL works fine.
I have broken down the sql statement to its simplest version for this question.
Here is the SQLRPGLE code snippet
And here is where I enter the same statement in STRSQL
Any Assistance would be greatly appreciated.
**EDIT
This only happens when I add a where clause, which is essentially what I need and why I am trying to do it using sqlrpgle.
Statements like
sqlstmt = 'insert into jallib/sbrmrpt (shscdt, shcmdt) ' +
'select shscdt, shcmdt ' +
'from r50files/sbschd ';
and
sqlstmt = 'insert into jallib/sbrmrpt (shscdt, shcmdt) ' +
'values (10, 10) ';
All work just fine, once a where clause is added to the subquery is where it does not work in the RPG code but in STRSQL
You have sqlstmt defined as 100 characters maximum and with the where clause your string is over 100 characters long. Your SQL statement is getting truncated and that's why its not working. I suspect you know how to fix this but just for completeness, the solution is the following:
D sqlstmt s 500a varying
I'm trying to use SELECT regexp_replace(m.*, '[\n\r]+', ' ', 'g') to remove carriage returns and new lines from my field to generate a CSV from my table; however, looks like my postgresql version (7.4.27) does not support that function.
function regexp_replace(members, "unknown", "unknown", "unknown") does not exist
I also tried doing it this way:
SELECT replace(replace(m.*, '\r', ''), '\n', '')
function replace(members, "unknown", "unknown") does not exist
No function matches the given name and argument types. You may need to add explicit type casts.
or this way:
SELECT replace(replace(m.*, chr(13), ''), chr(10), '')
function replace(members, text, "unknown") does not exist
and still got similar errors.
How can a achieve that using another function or solution?
m.* makes no sense where you put it. It would work like this:
SELECT replace(replace(m.some_column, chr(13), ''), chr(10), '')
FROM tbl m;
But that just removes all "linefeed" and "carriage return" characters completely instead of replacing each string consisting only of these characters with a single space character like your original. If that's what you want, single character replacement is simpler and cheaper with translate() - also available in ancient pg 7.4:
SELECT translate(some_column, chr(13) || chr(10), '');
To achieve what your original regexp_replace() does (just without the nonsensical m.*), identify a single character that's not in the string and use that as stepping stone. Say: ° does not pop up, then:
SELECT replace(replace(replace(
translate(some_column, chr(13) || chr(10), '°') -- replace with dummy
, '°°', '°') -- consolidate to single dummy
, '°°', '°') -- repeat as many times as necessary
, '°', ' '); -- replace dummy with space
Looks awkward, and it's imperfect: fails for too many consecutive line breaks. But it's probably still faster than regexp_replace(), even in modern Postgres, because regular expressions are much more expensive. Then again, performance is probably not an issue here.
Upgrade to modern Postgres and you don't need this.
I've got a list 400 rows +. Each row looks similar to this: example-example123 I would like to remove everything past '-' so that I'm left with just the beginning part: example123
Any help would be greatly appreciated.
try it like this:
UPDATE table SET column_name=LEFT(column_name, INSTR(column_name, '-')-1)
WHERE INSTR(column_name, '-')>0;
If you only want to select you do it this way:
SELECT LEFT(column_name, INSTR(column_name, '-')-1) FROM table;
INSTR function gets you the position of your - then you update the column value to become from the first letter of the string till the position of the - -1
Here's a fiddle
You can use SQL Trim() function
SELECT TRIM(TRAILING '-' FROM BHEXLIVESQLVS1-LIVE61MSSQL)
AS TRAILING_TRIM
FROM table;
The result should be "BHEXLIVESQLVS1"
select SUBSTRING(col_name,0,Charindex ('-',col_name))
Assuming you need to do this in a query, you can use the string functions of your database.
For DB2 this would look something like
select SUBSTR(YOURCOLUMN, 1, LOCATE('-',YOURCOLUMN)) from YOURTABLE where ...
In SQL Server you could use
SUBSTRING
and
CHARINDEX
For SQL server you can do this,
LEFT(columnName, charindex('-', columnName)) to remove every character after '-'
to remove the special character as well do this,
LEFT(columnName, charindex('-', columnName)-1)
SELECT SUBSTRING(col_name,0,Charindex ('-',col_name)) FROM table_name
WHERE col_name='yourvalue'
Eg.
SELECT SUBSTRING(TPBS_Path,0,Charindex ('->',TPBS_Path)) FROM [CFG].[CFG_T_Project_Breakdown_Structure] WHERE TPBS_Parent_PBS_Code='LE180404'
here TPBS_Path is the column for which trim is to be done and [CFG].[CFG_T_Project_Breakdown_Structure] is table name and TPBS_Parent_PBS_Code='LE180404' is the select condition. Everything after '->' will be trimmed
What is wrong with this statement that it is still giving me spaces after the field. This makes me think that the syntax combining the WHEN statements is off. My boss wants them combined in one statement. What am I doing wrong?
Case WHEN LTRIM(RTRIM(cSHortName))= '' Then NULL
WHEN cShortname is NOT NULL THEN
REPLACE (cShortName,SUBSTRING,(cShortName,PATINDEX('%A-Za-z0-9""},1,) ''_
end AS SHORT_NAME
Judging from the code, it seems that you may be trying to strip spaces and non-alphanumeric characters from the beginning and ending of the string.
If so, would this work for you?
I think it provides the substring from the first alphanumeric occurrence to the last.
SELECT
SUBSTRING(
cShortName,
PATINDEX('%A-Za-z0-9',cShortName),
( LEN(cShortName)
-PATINDEX('%A-Za-z0-9',REVERSE(cShortName))
-PATINDEX('%A-Za-z0-9',cShortName)
)
) AS SHORTNAME
Replace TRIM with LTRIM.
You can also test LEN(cShortName) = 0
Ummm there seems to be some problems in this script, but try this.
Case
WHEN LTRIM(RTRIM(cSHortName))= '' Then NULL
WHEN cShortname is NOT NULL THEN REPLACE(cShortName, SUBSTRING(cShortName, PATINDEX('%A-Za-z0-9', 1) , ''), '')
end AS SHORT_NAME
Why do you think it is supposed not to give you spaces after the field?
Edit:
As far as I understand you are trying to remove any characters from the string that do not match this regular expression range [a-zA-Z0-9] (add any other characters that you want to preserve).
I see no clean way to do that in Microsoft SQL Server (you are using Microsoft SQL Server it seems) using the built-in functions. There are some examples on the web that use a temporary table and a while loop, but this is ugly. I would either return the strings as is and process them on the caller side, or write a function that does that using the CLR and invoke it from the select statement.
I hope this helps.