SQL, remove appearances of a comma at end of the comma seperated list - sql-server-2005

How can we remove last comma from a comma seperated list in SQL query?
I have done a sample where i get a comma seperated string as output. But i found that a comma is comming after the string ends. I have tried to trim it of but was not able to do it.Can any one help me in getting it off?
my resulting string looks like this
eg:- some text, some more text, even more, yet more,

DECLARE #my_string nvarchar(128)
SET #my_string = N'Some Text, Some More Text, Even More, Yet More,'
select SUBSTRING(#my_string, 1, LEN(#my_string) - 1)
Should you need further assistance, you should provide further information.

Update MyTable
Set MyField = Case
When Right(MyField,1) = ',' Then Substring( MyField, 1, Len(MyField) - 1 )
Else MyField
End
Btw, another alternative:
Update MyTable
Set MyField = Substring( MyField, 1, Len(MyField) - 1 )
Where Value Like '%,'
Test script:
Declare #TestValues Table ( Value nvarchar(100) not null )
Insert #TestValues(Value) Values( 'XYZZY' )
Insert #TestValues(Value) Values( 'PLUGH' )
Insert #TestValues(Value) Values( 'SpiffyValueWithComma,' )
Select Case
When Right(Value,1) = ',' Then Substring( Value, 1, Len(Value) - 1 )
Else Value
End
From #TestValues
Results:
XYZZY
PLUGH
SpiffyValueWithComma
Update
One possibiity is that your trailing value isn't a comma but rather a comma and a space. If that's the case, then you need to modify your query like so:
Update MyTable
Set MyField = Substring( MyField, 1, Len(MyField) - 1 )
Where Value Like '%,'
Or Value Like '%, '

Related

I want to remove the value after *.*, but it the value of the length is not definite

Thank you in advance.
I want to remove the values after ., but the length is not defined it keeps changing but it should not take the values after the second FULL STOP.
1)Example:
Input:- ROL0602.E.DCM.5264403 and COK0105.F.SKE and CLT005.02A.FCM.65721
output: ROL0602.E and COK0105.F and CLT005.02A
2) example :
Input: SKE-5700-00211-000
output: SKE-5700-00211
These are the two columns i want some help with.
I tried using the charindex but as the length keeps on changing i wasn't able to do it.
Try it like this:
DECLARE #tbl TABLE(YourValue VARCHAR(100));
INSERT INTO #tbl VALUES
('ROL0602.E.DCM.5264403'),('COK0105.F.SKE'),('CLT005.02A.FCM.65721'),('SKE-5700-00211-000');
SELECT CASE WHEN CHARINDEX('.',YourValue)>0 THEN LEFT(YourValue,CHARINDEX('.',YourValue,CHARINDEX('.',YourValue,1)+1)-1)
ELSE YourValue END
FROM #tbl AS tbl
The result
ROL0602.E
COK0105.F
CLT005.02A
SKE-5700-00211-000
Please provide a rule for the last example. Don't know how to cut this...
This command uses LEFT to take a string starting at the beginning. The lenght is found by searching for a dot, starting at the position 1 after the first dot.
UPDATE: A more generic solution
The following fill first split the string in its parts (easy to use with other separators too). This is finally re-concatenated depending on a rule you can define yourself:
DECLARE #tbl TABLE(YourValue VARCHAR(100));
INSERT INTO #tbl VALUES
('ROL0602.E.DCM.5264403'),('COK0105.F.SKE'),('CLT005.02A.FCM.65721'),('SKE-5700-00211-000');
WITH Parted AS
(
SELECT YourValue
,CAST('<x>' + REPLACE(REPLACE(tbl.YourValue,'-','.'),'.','</x><x>') + '</x>' AS XML) AS Casted
,CASE WHEN CHARINDEX('.',YourValue)>0 THEN '.' ELSE CASE WHEN CHARINDEX('-',YourValue)>0 THEN '-' ELSE '?' END END AS SeparatorChar
FROM #tbl AS tbl
)
,SingleParts AS
(
SELECT SeparatorChar
,YourValue
,ISNULL(Casted.value('/x[1]','nvarchar(max)'),'') AS Part1
,ISNULL(Casted.value('/x[2]','nvarchar(max)'),'') AS Part2
,ISNULL(Casted.value('/x[3]','nvarchar(max)'),'') AS Part3
,ISNULL(Casted.value('/x[4]','nvarchar(max)'),'') AS Part4
,ISNULL(Casted.value('/x[5]','nvarchar(max)'),'') AS Part5
FROM Parted
)
SELECT CASE SeparatorChar
WHEN '.' THEN Part1 + '.' + Part2
WHEN '-' THEN Part1 + '-' + Part2 + '-' + Part3
ELSE YourValue
END
FROM SingleParts
The result
ROL0602.E
COK0105.F
CLT005.02A
SKE-5700-00211
SHnugo's solution is excellent but will fail if there is only one dot (.) in the string. Here's a tweaked version that will handle that (note my comments).
DECLARE #tbl TABLE(YourValue VARCHAR(100));
INSERT INTO #tbl VALUES
('ROL0602.E.DCM.5264403'),('COK0105.F.SKE'),('CLT005.02A.FCM.65721'),('CLT099.02ACFVVV721'), ('SKE-5700-00211-000');
SELECT
CASE
--If there are two or more dots(.) then return everything up to the second dot:
WHEN LEN(YourValue) - LEN(REPLACE(YourValue,'.','')) > 1
THEN LEFT(YourValue,CHARINDEX('.',YourValue,CHARINDEX('.',YourValue,1)+1)-1)
ELSE YourValue -- if there are 1 or 0 dots(.) then return the entire value
END
FROM #tbl AS tbl;
You can try as follows:
SELECT REPLACE('ROL0602.E.DCM.5264403',
( '.' + REVERSE(SUBSTRING(REVERSE('ROL0602.E.DCM.5264403'), 0,
CHARINDEX('.',
REVERSE('ROL0602.E.DCM.5264403')))) ),
'')

TSQL - Replacing or removing a string between two dissimilar strings, multiple times in a field for an entire column

I want to replace or remove all characters between and including these two characters < > they occur numerous times throughout the field and in varying circumstances for each row
I believe the strings to be replaced are html so when I tried to post an example the site just registered it as formatting.
I used replace to remove all common strings such as the the html for line break but some vary field to field using hexadecimal color values.
Thanks!
DECLARE #foo TABLE(TAGS VARCHAR(MAX));
INSERT #foo VALUES('<fname>John</fname><lname>Dewey</lname>');
SELECT 1; --This will initialize ##ROWCOUNT if necessary. Might be optional depending on your query
WHILE ##ROWCOUNT > 0 BEGIN
UPDATE #foo
SET TAGS = STUFF(TAGS,CHARINDEX('<',TAGS),CHARINDEX('>',TAGS) - CHARINDEX('<',TAGS) + 1,'')
WHERE TAGS LIKE '%<%>%'
END
SELECT * FROM #foo;
Result
------------------------
JohnDewey
CREATE TABLE #html(Value NVARCHAR(MAX))
INSERT INTO #html(Value) VALUES('<ShouldBeRemoved>1 <Remove>abc<also remove>def<Take this out>ghi')
INSERT INTO #html(Value) VALUES('<ShouldBeRemoved>2 <Remove>abc<also remove>def<Take this out>ghi')
INSERT INTO #html(Value) VALUES('<ShouldBeRemoved>3 <Remove>abc<also remove>def<Take this out>ghi')
;with Cte(Value) AS (
SELECT Value FROM #html
UNION ALL
SELECT REPLACE(Value, SUBSTRING(Value, CHARINDEX('<', Value), CHARINDEX('>', Value) - CHARINDEX('<', Value) + 1),'')
FROM Cte
WHERE CHARINDEX('<', Value) != 0
)
SELECT Value FROM Cte
WHERE CHARINDEX('<', Value) = 0
OPTION (MAXRECURSION 0);
DROP TABLE #html
CREATE FUNCTION [dbo].[udf_RemoveHtmlTag] (#HTMLText VARCHAR(MAX))
RETURNS VARCHAR(MAX)
AS
BEGIN
WHILE((CHARINDEX('<', #HTMLText)>0) and (CHARINDEX('>', #HTMLText)>0))
Begin
SET #HTMLText = REPLACE(#HtmlText,SUBSTRING(#HTMLText, CHARINDEX('<', #HTMLText), CHARINDEX('>', #HTMLText) - CHARINDEX('<', #HTMLText) + 1),'');
End
RETURN #HTMLText
END
GO
Then how to use this func
select [dbo].[udf_RemoveHtmlTag]([column_name]) from tablename
This code will work for them who wants to avoid calling UDF (User Defined Function).
Assuming that one of your sql table col. contains HTML tags like below and you want to remove everything between < and > .
[Remark] =" the HTML tags like: < div > < p >< span > We are trying to contact the company (Tel: < /span >< span> < /p > < /div > etc' "
Step#1: Store your result set to a Temp table
select .... into #t1 from....where...blah blah
Step#2: Use the following code to remove everything between < and > and update the field.
WHILE ##ROWCOUNT > 0 BEGIN
UPDATE #t1
SET [Remark] = LTRIM(RTRIM(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(LTRIM(RTRIM(STUFF([Remark],CHARINDEX('<',[Remark]),CHARINDEX('>',[Remark]) - CHARINDEX('<',[Remark]) + 1,'') )), CHAR(9), ' '), CHAR(10), ' '), CHAR(11), ' '), CHAR(12), ' '), CHAR(13), ' '), ' ', '')))
WHERE [Remark] LIKE '%<%>%'
END
Step#3: Select your desired result from Temp table
SELECT * FROM #t1

Calculate amount of text

message
------------
01d,2s3,1wee (nvarchar)
01,32,32154, (nvarchar)
df,g,d, (nvarchar)
dd,12,2 (nvarchar)
I dont know how to achieve the result below and to take account to the ',' in the end of the cell. The data in the list can be changed daily.
Requested result
message Count
------------ -----
01d,2s3,1wee 3
01,32,32154, 3
df,g,d, 3
dd,12,2 3
// DWD
declare #table table(message varchar(20))
insert into #table values('01d,2s3,1wee');
insert into #table values('01,32,32154,');
insert into #table values('df,g,d,');
insert into #table values('dd,12,2');
SELECT message
,CASE WHEN SUBSTRING(REVERSE(message),1,1) = ',' THEN
LEN(message) - LEN(REPLACE(message, ',', '')) ELSE
( LEN(message) - LEN(REPLACE(message, ',', '')) +1 )END As Count
FROM #table
This approach checks if the final character is a ,, and then removes it if it is...
it then compares the length of the original message to the length of the message with all , removed to determine how many , existed originally.
select
len(message) - len(replace(message, ',', '')) as wordCount
from
(
select
case
when right(rtrim(message),1) = ','
then substring(rtrim(message),1,len(rtrim(message))-1)
else message
end as message
) trimmed
I think this is what you are looking for:
SELECT message
,[Count] = LEN(CASE WHEN RIGHT(message, 1) = ',' THEN message ELSE message + ',' END) - LEN(REPLACE(CASE WHEN RIGHT(message, 1) = ',' THEN message ELSE message + ',' END, ',' ,''))
FROM yourtablename
First, You need to get rid of the trail ",", and then count the length of the original string and substract the length of the string replacing "," with nothing. Something like this:
SELECT message, LEN(Newmessage) - LEN(REPLACE(Newmessage,',','')) + 1 [Count]
FROM (SELECT message, CASE WHEN RIGHT(message,1) = ',' THEN LEFT(message,LEN('01,32,32154,')-1) ELSE
message END AS Newmessage
FROM YourTable) A
This assumes that there is no trailing spaces on your columns, otherwise you should use RTRIM
SELECT message
, 1 + LEN(message) - REPLACE ( message , ',' , '' ) AS count
FROM my_table
The tick consists in counting the number of comas (",") in the column.
For this, it calculates the difference between the string and the same string without the coma.

Truncating Strings from a column name

I have a column which has values divided by colon ":".
For example DT:CSDF , SFT:TAHS etc...
I just need to take the right side i.e. CSDF,TAHS etc
How do I do it in the select clause?
If you will never have dots, you can use this
PARSENAME(REPLACE(ColumnName,':','.'),1)
example
DECLARE #v VARCHAR(100) = 'DT:CSDF'
SELECT PARSENAME(REPLACE(#v,':','.'),1)
otherwise use PATINDEX and RIGHT
SELECT RIGHT(ColumnName,LEN(ColumnName) -PATINDEX('%:%',ColumnName))
Example
DECLARE #v VARCHAR(100) = 'DT:CSDF'
SELECT RIGHT(#v,LEN(#v) -PATINDEX('%:%',#v))
Like this:
SELECT SUBSTRING(YourField,
CHARINDEX(':', YourField) + 1,
LEN(YourField)
) AS YourNewField
something like this:
SUBSTR( INSTR( mycol, ':' ) )
SELECT SUBSTRING(fieldname, CHARINDEX(':', fieldname) + 1, LEN(fieldname))
FROM ...
More t-sql string functions you can find here:
http://msdn.microsoft.com/en-US/library/ms181984(v=sql.90).aspx

SQL Server 2008: How to find trailing spaces

How can I find all column values in a column which have trailing spaces? For leading spaces it would simply be
select col from table where substring(col,1,1) = ' ';
You can find trailing spaces with LIKE:
SELECT col FROM tbl WHERE col LIKE '% '
SQL Server 2005:
select col from tbl where right(col, 1) = ' '
As a demo:
select
case when right('said Fred', 1) = ' ' then 1 else 0 end as NoTrail,
case when right('said Fred ', 1) = ' ' then 1 else 0 end as WithTrail
returns
NoTrail WithTrail
0 1
This is what worked for me:
select * from table_name where column_name not like RTRIM(column_name)
This will give you all the records that have trailing spaces.
If you want to get the records that have either leading or trailing spaces then you could use this:
select * from table_name where column_name not like LTRIM(RTRIM(column_name))
A very simple method is to use the LEN function.
LEN will trim trailing spaces but not preceeding spaces, so if your LEN() is different from your LEN(REVERSE()) you'll get all rows with trailing spaces:
select col from table where LEN(col) <> LEN(REVERSE(col));
this can also be used to figure out how many spaces you have for more advanced logic.
SELECT * FROM tbl WHERE LEN(col) != DATALENGTH(col)
Should work also.
There's a few different ways to do this...
My favorite option, assuming your intention is to remove any leading and / or trailing spaces, is to execute the following, which will dynamically create the T-SQL to UPDATE all columns with an unwanted space to their trimmed value:
SELECT
'UPDATE [<DatabaseName>].[dbo].['+TABLE_NAME+']
SET ['+COLUMN_NAME+']=LTRIM(RTRIM(['+COLUMN_NAME+']))
WHERE ['+COLUMN_NAME+']=LTRIM(RTRIM(['+COLUMN_NAME+']));'+CHAR(13)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME LIKE '<TableName>%'
AND DATA_TYPE!='date'
ORDER BY TABLE_NAME,COLUMN_NAME
If you really need to identify them though, try one of these queries:
SELECT *
FROM [database].[schema].[table]
WHERE [col1]!=LTRIM(RTRIM([col1]))
More dynamic SQL:
SELECT 'SELECT ''['+TABLE_NAME+'].['+COLUMN_NAME+']'',*
FROM [<your database name>].[dbo].['+TABLE_NAME+']
WHERE ['+COLUMN_NAME+'] LIKE ''% ''
OR ['+COLUMN_NAME+'] LIKE '' %'';
GO
'
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME LIKE '<filter table name as desired>%'
AND DATA_TYPE!='date'
Here's an alternative to find records with leading or trailing whitespace, including tabs etc:
SELECT * FROM tbl WHERE NOT TRIM(col) = col
Try this:
UPDATE Battles
SET name = CASE WHEN (LEN(name+'a')-1)>LEN(RTRIM(name))
THEN REPLICATE(' ', (LEN(name+'a')-1)- LEN(RTRIM(name)))+RTRIM(name)
ELSE name
END
Spaces are ignored in SQL Server so for me even the leading space was not working .
select col from table where substring(col,1,1) = ' '
wont work if there is only one space (' ') or blank ('')
so I devised following:
select * from [table] where substring(REPLACE(col, ' ', '#'),1,1) = '#'
Here is another alternative for trailing spaces.
DECLARE #VALUE VARCHAR(50) = NULL
DECLARE #VALUE VARCHAR(50) = ' '
IF ((#VALUE IS NOT NULL) AND (LTRIM(RTRIM(#VALUE)) != ''))
BEGIN
SELECT 'TRUE'
END
ELSE
BEGIN
SELECT 'FALSE'
END
I have found the accepted answer a little bit slower:
SELECT col FROM tbl WHERE col LIKE '% ';
against this technique:
SELECT col FROM tbl WHERE ASCII(RIGHT([value], 1)) = 32;
The idea is to get the last char, but compare its ASCII code with the ASCII code of space instead only with ' ' (space). If we use only ' ' space, an empty string will yield true:
DECLARE #EmptyString NVARCHAR(12) = '';
SELECT IIF(RIGHT(#EmptyString, 1) = ' ', 1, 0); -- this returns 1
The above is because of the Microsoft's implementation of string comparisons.
So, how fast exactly?
You can try the following code:
CREATE TABLE #DataSource
(
[RowID] INT PRIMARY KEY IDENTITY(1,1)
,[value] NVARCHAR(1024)
);
INSERT INTO #DataSource ([value])
SELECT TOP (1000000) 'text ' + CAST(ROW_NUMBER() OVER(ORDER BY t1.number) AS VARCHAR(12))
FROM master..spt_values t1
CROSS JOIN master..spt_values t2
UPDATE #DataSource
SET [value] = [value] + ' '
WHERE [RowID] = 100000;
SELECT *
FROM #DataSource
WHERE ASCII(RIGHT([value], 1)) = 32;
SELECT *
FROM #DataSource
WHERE [value] LIKE '% ';
On my machine there is around 1 second difference:
I have test it on table with 600k rows, but larger size, and the difference was above 8 seconds. So, how fast exactly will depend on your real case data.
Another way to achieve this by using CHARINDEX and REVERSE like below:
select col1 from table1
WHERE charindex(' ', reverse(col1)) = 1
See example Here
Simple use below query to get values having any number of spaces in begining or at end of values in column.
select * from table_name where column_name like ' %' or column_name like '% ';
We can try underscore to find the entries which are blanks,though not an accurate solution like using '% %' or ' ', But I could find entries which are blanks.
select col_name from table where col_name like '_'