Remove only leading or trailing carriage returns
Find the first character that is not CHAR(13)
or CHAR(10)
and subtract its position from the string's length.
LTRIM()
SELECT RIGHT(@MyString,LEN(@MyString)-PATINDEX('%[^'+CHAR(13)+CHAR(10)+']%',@MyString)+1)
RTRIM()
SELECT LEFT(@MyString,LEN(@MyString)-PATINDEX('%[^'+CHAR(13)+CHAR(10)+']%',REVERSE(@MyString))+1)
VBA Replace Only leading and trailing line breaks
Here is a trimCHAR
function that works similarly to Excel's TRIM
function, except you can specify the character to be TRIM'd.
It will remove all leading and trailing char
, as well as any doubled char
within the string (leaving a single char
within the string, as TRIM
does with spaces)
Function trimCHAR(ByVal S As String, char As String)
'similar to worksheet TRIM function except can specify character(s) to TRIM
Dim RE As Object
Dim I As Long
Set RE = CreateObject("vbscript.regexp")
With RE
.Global = True
.multiLine = True
'need to do separately, otherwise multiple chars within will
'be removed
.Pattern = char & "*$"
S = .Replace(S, "") 'Remove extra chars at end of string
.Pattern = char & "*([^" & char & "]" & char & ")*"
S = .Replace(S, "$1") 'Remove extra chars at start of string or within
End With
trimCHAR = S
End Function
Original
Usage on worksheet: =trimCHAR(cell_ref,CHAR(10))
in a macro, you might use =trimChar(myStringVariable, vblf)
Result
Does the MySQL TRIM function not trim line breaks or carriage returns?
Trim()
in MySQL only removes spaces.
I don't believe there is a built-in way to remove all kinds of trailing and leading whitespace in MySQL, unless you repeatedly use Trim()
.
I suggest you use another language to clean up your current data and simply make sure your inputs are sanitized from now on.
Using AWK or Sed how can I remove trailing carriage return and a line feeds before first txt
All you need is:
awk 'NF{f=1}f' file
Remove carriage return and space from a string
Try:
t.replace(/[\n\r]+/g, '');
Then:
t.replace(/\s{2,10}/g, ' ');
The 2nd one should get rid of more than 1 space
How to remove newlines from beginning and end of a string?
Use String.trim()
method to get rid of whitespaces (spaces, new lines etc.) from the beginning and end of the string.
String trimmedString = myString.trim();
Related Topics
Create View' Must Be The First Statement in a Query Batch
Tools to Work with Stored Procedures in Oracle, in a Team
Way to Abort Execution of MySQL Scripts (Raising Error Perhaps)
Checking If a Given Date Fits Between a Range of Dates
If Exists Statement in SQL to Linq
Copy Data from One Existing Row to Another Existing Row in SQL
How to Select a Row Based on Its Row Number
Postgres: Select All Row with Count of a Field Greater Than 1
How to Generate a Permutations or Combinations of N Rows in M Columns
Building a Table Dependency Graph with a Recursive Query
SQL - Subquery in Aggregate Function
How to Troubleshoot Ora-02049 and Lock Problems in General with Oracle
Using SQL Query to Determine If a Table Exists
How to Copy a Row with Every Column Except Identity Column (Sql Server 2005)