LTRIM usage with SQL server 2005 - sql

I am a bit of an sql noob so please forgive. I can't seem to find a usage example of LTRIM anywhere.
I have a NVARCHAR column in my table in which a number of entries have leading whitespace - I'm presuming if I run this it should do the trick:
SELECT LTRIM( ColumnName)
From TableName;
Will this give the desired result?

No, it will trim leading spaces but not all white space (e.g. carriage returns).
Edit
It seems you are looking for an UPDATE query that will remove leading and trailing whitespace.
If by that you only mean "normal" spaces just use
UPDATE TableName
SET ColumnName = LTRIM(RTRIM(ColumnName ))
For all white space this should do it (from the comments here). Backup your data first!
UPDATE TableName
SET ColumnName =
SUBSTRING(
ColumnName,
PATINDEX('%[^ ' + char(09) + char(10) + char(13) + char(20) + ']%',
ColumnName),
LEN(ColumnName) - PATINDEX('%[^ ' + char(09) + char(10) + char(13) + char(20) + ']%'
, ColumnName) -
PATINDEX('%[^ ' + char(09) + char(10) + char(13) + char(20) + ']%',
REVERSE(ColumnName)) + 2)

Your example will work to remove the leading spaces. This will only select it from the database. IF you need to actually change the data in your table, you will need to write an UPDATE statement something like:
UPDATE TableName
SET ColumnName = LTRIM(ColumnName)
If you need to remove spaces from the right side, you can use RTRIM.
Here is a list of the string functions in SQL Server 2005 that I always refer to:
http://msdn.microsoft.com/en-us/library/ms181984(v=SQL.90).aspx

Did you run it to find out? It's just a select, it won't blow up your database. But, yes.
Select LTRIM(myColumn) myColumn
From myTable
Should return the myColumn values with any leading whitespace removed. Note this is only leading whitespace.
EDIT To Update the column, with the above, you'd do:
Update myTable
Set myColumn = LTRIM(myColumn)
From myTable

Related

Search SQL Server table column which has multiple leading and trailing spaces only

I am selecting data from a SQL Server table which has leading space characters (could be one, two or more) and also has trailing spaces (could be one, two, and more) also.
I have used LIKE ' %' to search leading spaces value and used LIKE '% ' for trailing spaces records, but the search result displayed only returns with records which has only one space as leading/trailing space. But I am not sure the number spaces available in records.
This is the query that I tried:
SELECT [ColumName1], [ColumName], *
FROM [table1]
WHERE [ColumName] LIKE ' %' OR [ColumName] LIKE '% '
Expected result:
' Testdata1', 'Test2 ', ' Test3',' Test4 '
But actual result is :
'Test3 '
There's an easier way to do this, without using likes. TRIM cuts off trailing whitespaces, you can use that to filter. What you're doing here is comparing the trimmed version of every [ColumName] to the untrimmed version, and returning those that differ. This accomplishes what you want.
select
[ColumName1], [ColumName], *
from [table1]
WHERE TRIM([ColumName]) <> [ColumName]
This produces all fields that have any (one or more) trailing or leading whitespaces.
Depending on your version of SQL Server, TRIM might not be available. No matter, there's a workaround around that as well:
select
[ColumName1], [ColumName], *
from [table1]
WHERE LTRIM([ColumName]) <> [ColumName]
OR RTRIM([ColumName]) <> [ColumName]
Try to use LTRIM with RTRIM and LIKE operator to find spaces. Let me show an example:
DECLARE #tbl TABLE
(
FooColumn VARCHAR(100)
)
INSERT INTO #tbl
VALUES
(' Example1'),
(' Example2'),
('Example3 '),
('Example4 '),
('Example5')
SELECT
t.FooColumn
FROM #tbl t
WHERE RTRIM(t.FooColumn) LIKE '% %' OR LTRIM(t.FooColumn) LIKE '% %'
You can try this,
DECLARE #Temp_Table TABLE
(
ColumnName VARCHAR(100)
)
INSERT INTO #Temp_Table
VALUES
(' Test1'),
(' Test2'),
('Test3 '),
('Test4 '),
('Test5')
SELECT ColumnName
FROM #Temp_Table
WHERE (LEFT(ColumnName,1)=' ' OR RIGHT(ColumnName,1)=' ')
If I am not mistaken, SQL Server's LIKE allows character sets. We can use this to find control and whitespace characters by excluding characters that we allow (letters and numbers). Please try:
SELECT *
FROM [table1]
WHERE [ColumName] LIKE '[^A-Za-z0-9]%' OR [ColumName] LIKE '%[^A-Za-z0-9]';
Finally i found that column values has been saved with special ascii values, hence those characters were not removed while using LTRIM, RTRIM.
This worked : LTRIM(RTRIM(REPLACE(REPLACE(REPLACE(REPLACE(plaincitation, CHAR(10), CHAR(32)),CHAR(13), CHAR(32)),CHAR(160), CHAR(32)),CHAR(9),CHAR(32))))

How to add '~' sign to a returned nvarchar value?

I am trying to add a '~' sign between two columns in SQL Server:
SELECT CODE + '~' + NAME FROM TEST_TABLE
It should return 'CCC~NNN' but it displays a newline instead of '~'. How can I fix this?
Maybe theres a hidden new line char in one of the selected fields. Try this:
SELECT REPLACE(REPLACE(CODE + '~' + NAME, CHAR(13), ''), CHAR(10), '') FROM TEST_TABLE
I would suggest using different column names in the future.
For now, it might suffice to simply surround your column names with brackets:
SELECT [CODE] + '~' + [NAME] FROM TEST_TABLE
Works fine for me.

How to remove newline from column values in Sybase

i have a table in sybase that has 7 columns.
The sixth column has a new line as the last character in all the values. This newline is messing up the output when i export the table. How can i remove the newline character from all values in this column ?
this did the trick
update tableName
set column_name =str_replace(column_name, char(13), null)
Depending on whether the newline is in windows or unix format, one of the below should work:
UPDATE tableName SET column_name = str_replace(column_name, char(13), null)
or
UPDATE tableName SET column_name = str_replace(column_name, CHAR(13) + CHAR(10), '')

struggling with creating Insert query

I create Insert statement for organization table like this:
select'Insert into Organizations(Name,ContactPerson,ContactNumber,Mobilenumber)values('''+Nameofthecompany+''+','+Nameofthepersonresponsibleforrecruitment+','+PhoneNumber+','+MobileNumber+''')' from Organization
When I execute this statement I get insert statement. But the issue is where the value is null, it shows all columns null.
Example: (in database)
Name: xxxx
ContactPerson: zzzz
ContactNumber:444444
MobileNumber: null
so my insert statement looks like:
Null.
I want only that column provide null. other details showing properly. Is there any way in sql server? Help me anyone...
The result of concatenating anything to NULL, even itself, is always NULL. Workaround with ISNULL function:
select'Insert into Organizations(Name,ContactPerson,ContactNumber,Mobilenumber)
values('''+ISNULL(Nameofthecompany, 'NULL')+''+','
+ISNULL(Nameofthepersonresponsibleforrecruitment, 'NULL')+','
+ISNULL(PhoneNumber, 'NULL')+','
+ISNULL(MobileNumber, 'NULL')+''')'
from Organization
Demo on SQLFiddle
Sure - just use ISNULL(..) to turn a NULL into e.g. an empty string:
SELECT
'INSERT INTO Organizations(Name, ContactPerson, ContactNumber, Mobilenumber) VALUES(''' +
ISNULL(Nameofthecompany, '') + '' + ',' +
ISNULL(Nameofthepersonresponsibleforrecruitment, '') + ',' +
ISNULL(PhoneNumber, '') + ',' + ISNULL(MobileNumber,'') + ''')'
FROM Organization
When you are adding each of the parameters to the SQL statement, you need to check whether they're null, and if so use the keyword NULL, otherwise include a literal string surrounded with single quotes, but bearing in mind that if the string contains any single quotes, they need to be replaced with two single quotes.
Update the SQL for each parameter something like the following:
CASE WHEN MobileNumber IS NULL THEN 'NULL' ELSE '''' + REPLACE(MobileNumber, '''', '''''') + '''' END

Importing a table from Access into SQL and removing the carriage returns

I'm importing a table from Access that has an address field the address field contains carriage returns, these remain when I have imported them into SQL, can I run a SQL script to remove them? Any ideas much appreciated.
This will replace Carriage Returns (char(13)) with a single space:
UPDATE MyTable
SET MyColumn = Replace(MyColumn, CHAR(13), ' ')
If you have Carriage Return + LineFeeds:
UPDATE MyTable
SET MyColumn = Replace(MyColumn, CHAR(13)+CHAR(10), ' ')
You can use this query to replace newline with empty space:
replace ( replace(TextValue, char(10),' '), char(13), ' ')