T-SQL initial capitalization - leave 2-n chars alone - tsql

I have a t-sql function I found online, maybe on stack, that works perfectly for camel-casing all words in a string:
CREATE FUNCTION InitialCap(#String VARCHAR(75))
RETURNS VARCHAR(75)
AS
BEGIN
DECLARE #Position INT;
SELECT #String = STUFF(LOWER(#String),1,1,UPPER(LEFT(#String,1))) , #Position = PATINDEX('%[^A-Za-z''][a-z]%',#String);
WHILE #Position > 0
SELECT #String = STUFF(#String,#Position,2,UPPER(SUBSTRING(#String,#Position,2))) COLLATE Latin1_General_Bin,
#Position = PATINDEX('%[^A-Za-z''][a-z]%',#String COLLATE Latin1_General_Bin);
RETURN ISNULL(#String,'');
END
The problem is that I now need a slight mod of this, such that it leaves alone all but the first letter of each word. I still want it to capitalize the first letter of each word, but then I don't want it to ever change other letters in that word. So here are some examples of my desired outcome:
'here is foo' -> 'Here Is Foo'
'i like the FBI' -> 'I Like The FBI'
For the 2nd example, note that my function shown above changes "FBI" to "Fbi", and that's the problem. I'm not sure how to change the sql to achieve it so that it would leave FBI as FBI. Now of course if the original string were "fbi", I know it would change it to "Fbi", and that's fine.
In summary, I never want the function to change an upper case letter to lower case.

Just get rid of LOWER() in STUFF():
CREATE FUNCTION InitialCap(#String VARCHAR(75))
RETURNS VARCHAR(75)
AS
BEGIN
DECLARE #Position INT;
SELECT #String = STUFF(#String,1,1,UPPER(LEFT(#String,1))) , #Position = PATINDEX('%[^A-Za-z''][a-z]%',#String);
WHILE #Position > 0
SELECT #String = STUFF(#String,#Position,2,UPPER(SUBSTRING(#String,#Position,2))) COLLATE Latin1_General_Bin,
#Position = PATINDEX('%[^A-Za-z''][a-z]%',#String COLLATE Latin1_General_Bin);
RETURN ISNULL(#String,'');
END
GO
DECLARE #String VARCHAR(75) = 'i like the FBI'
SELECT dbo.InitialCap(#String)
Results:
I Like The FBI

Related

TSQL Replace Doubled Characters

Let's say I have data:
heloo
cuube
triniity
How to write script that will replace those "doubled" characters with only one? So the result from the above data set would be:
helo
cube
trinity
Usually I post some script where I tried to achieve this, but this time I can't think of any.
This should work:
CREATE PROCEDURE remove_duplicate_characters(#string VARCHAR(100))
AS
DECLARE #result VARCHAR(100)
SET #result=''
SELECT #result=#result+MIN(SUBSTRING(#string ,number,1)) FROM
(
SELECT number FROM master..spt_values WHERE type='p' AND number BETWEEN 1 AND len(#string )) AS t GROUP BY SUBSTRING(#string,number,1) ORDER BY MIN(number)
)
SELECT #result
GO
You then call it like this:
EXEC remove_duplicate_characters 'heloo'
Source
This script does not depend on having access to master functions, and just relies on t-sql string functions.
declare #word varchar(100) = 'aaaacuuuuuubeeeee', #result varchar(100) = ''
declare #letter char, #idx int = 0, #lastletter char = ''
while(#idx <= len(#word))
begin
select #letter = substring(#word,#idx,1)
if (#letter != #lastletter)
begin
select #result = concat(#result,#letter)
end
select #lastletter = #letter,#idx = #idx + 1
end
select #result

Concatenate string using IF statement

Sorry if this is a duplicate, I feel like this has had to have been asked before, but I might just not know how to word to search correctly.
So, in my stored I take in some information and I want to create a varchar string based on this information. Lets say I have these three variables.
#String varchar(MAX) = '',
#BroughtInInfo bit
And now I have something like the following
SET #String = 'Here is a string and I want to add'
IF #BroughtInInfo = 1
BEGIN
+'This info'+
END
ELSE
+'That info'+
'Then more stuff here after conditional statement'
Now, I'm getting syntax error near + I've tried a lot of combinations of moving the plus signs around the conditional statement but it doesn't play nice.
I'm pretty new to SQL so any tips and tricks will help! Thanks!
The expression you are looking for is CASE (MSDN).
SET #String = 'Here is a string and I want to add' +
CASE WHEN #BroughtInInfo = 1
THEN 'This info'
ELSE 'That info'
END
+ 'Then more stuff here after conditional statement'
Try This on SQLServer 2008 it will work
declare #String varchar(max);
declare #BroughtInInfo bit;
SET #String = 'Here is a string and I want to add'
IF #BroughtInInfo = 1
BEGIN
SET #String +='This info'
END
ELSE
SET #String +='This info' select #String

How to find which postgresql text fields do contain most occurances of a specific string?

I am looking for a SQL query that would list me the fields that do contain a specific string more times.
While it very easy to search for a string, I do want to sort the results based on number of occurences.
select count(*) from bodycontent WHERE body LIKE '%tag%'
select
body,
(select count(*) from regexp_matches(body, 'tag', 'gi')) ocurr
from bodycontent
order by ocurr desc
The i flag will make a case insensitive match.
Select the length of the value minus the length of the value after the searched-for string has been replaced with zero-length string, divided by the length of the searched-for string.
http://www.postgresql.org/message-id/20091020172452.GA10593#tux
I found this on a Chinese website, but at least the code was readable:
create or replace function regexp_count(str text,search text) returns int as $$
declare
str_len int;
search_len int;
i int;
begin
str_len := length(str);
search_len := length(search);
i := 0;
for x in 1..str_len-search_len+1 loop
if substr(str, x, search_len) = search then
i := i+1;
end if;
end loop;
return i;
end;
$$ language plpgsql strict;
select * from regexp_count('i am digoal test test', 'test');
Maybe someone else has a shorter solution... or even a faster one.

How to use a T-SQL UDF in a SELECT or UPDATE statement?

Context: SQL Server 2000
I've written a UDF that gives me the text between two other texts, viz
CREATE FUNCTION dbo.StrBetween
(
#Text nvarchar(4000),
#Lhs nvarchar(4000),
#Rhs nvarchar(4000)
)
RETURNS nvarchar(4000)
AS
BEGIN
DECLARE #LhsOffset INT;
DECLARE #RhsOffset INT;
DECLARE #Result NVARCHAR(4000);
SET #LhsOffset = CHARINDEX( #Lhs, #Text );
IF #LhsOffset = 0
BEGIN
RETURN #Text;
END
SET #Result = SUBSTRING( #Text, #LhsOffset+1, LEN(#Text)-LEN(#Lhs));
SET #RhsOffset = CHARINDEX( #Rhs, #Result );
IF #RhsOffset = 0
BEGIN
RETURN #Result;
END
SET #Result = SUBSTRING( #Result, 1, #RhsOffset - 1 );
RETURN #Result;
END
This works fine in SQL Query Analyser if I have, say,
SELECT dbo.StrBetween('dog','d','g')
However, when I pass a column in as the value of the first argument, I get no response. For example,
SELECT [TEST].[dbo].StrBetween(Referrer,'//', '/') as tst FROM tblTest
Referrer is declared as an nvarchar field. I'm a newbie when it comes to T-SQL. What obvious thing am I not seeing?
It's not an issue with calling - it's a logic issue, and the fact that your #Rhs value is part of the #Lhs value.
SET #Result = SUBSTRING( #Text, #LhsOffset+1, LEN(#Text)-LEN(#Lhs));
This is removing the first character of your #Lhs string. However, since the second character is /, and that's what your #Rhs match is searching for, it immediately finds it at position 1 and so you get an empty string.
Instead, try:
SET #Result = SUBSTRING( #Text, #LhsOffset+LEN(#Lhs), 4000);
You don't have to be exact with computing a length. If you ask for 4000 characters and the string is only 12 characters long, SUBSTRING will give you back at most 12 characters. So don't bother computing the new length.

Removing space sql server 2008

I am trying to write a stored procedure in sql server 2008,I need to remove unwanted spaces in the entries of my table.I categorized the entries in my table to 3 types.My store procedure should remove the spaces around single letter,like,
A G M words to AGM words
words A G M words to words AGM words
A G words to AG words
I tried following stored procedure.
CREATE proc At1 #name nvarchar(100)
as
declare #start int
declare #temp1 nvarchar(100)
declare #temp nvarchar(100)
declare #NthPosition int
declare #N int
set #N=LEN(#name)
set #start=1
set #temp1=''
set #temp=''
set #NthPosition=charindex(' ',#name,#start)
if(#NthPosition<>0)
begin
while (#NthPosition<>0 and #N<>0)
begin
set #temp1=SUBSTRING(#name,#start,#NthPosition-1)
if(#temp<>'')
begin
if(len(#temp1)=1)
begin
set #temp=(#temp+#temp1)
end
else
begin
set #temp=(#temp+' '+#temp1)
end
end
else
begin
set #temp=#temp1
end
set #start=#NthPosition+1
set #N=#N-#NthPosition
set #NthPosition=0
set #NthPosition=CHARINDEX(' ',#name,#start)
end
end
else
begin
select #name
end
select #temp
GO
and i used ,
exec At1 'apple A G M mango'
My expected result: apple AGM mango
But my actual result:apple
I am unable to figure out where the error is..Any suggestions in this regard is more helpful.
I tried to use computed column that would clear the space and i was able to find solution only for pattern #3.I am unable to frame a computed column definition suitable for all the 3 patterns..... Please share your thoughts that will be helpful to me
I think this covers all the cases:
CREATE proc At1 #Name nvarchar(100)
as
declare #New nvarchar(100)
declare #SpacePos int
declare #Single bit
select #New = '',#Single = 0
select #Name = LTRIM(#Name)
while LEN(#name) > 0
begin
set #SpacePos = CHARINDEX(' ',#Name)
if #SpacePos = 0 --No more spaces in the string
begin
select #New = #New + CASE WHEN #Single = 1 and LEN(#Name) > 1 THEN ' ' ELSE '' END + #Name,
#Name = ''
end
else if #SpacePos = 2 --Single character "word"
begin
select #New = #New + SUBSTRING(#Name,1,1),
#Name = SUBSTRING(#Name,3,100),
#Single = 1
end
else --Multi-character word
begin
select #New = #New + CASE WHEN #Single = 1 THEN ' ' ELSE '' END + SUBSTRING(#Name,1,#SpacePos),
#Name = SUBSTRING(#Name,#SpacePos+1,100),
#Single = 0
end
end
select #New
go
And the examples:
exec At1 'apple A G M mango'
exec At1 'A G M words'
exec At1 'words A G M'
Produces:
apple AGM mango
AGM words
words AGM
(As a simplifying assumption, I assumed I was okay to remove any leading spaces from the original string. I also assume there are no double spaces in the string. If neither of those assumptions is accurate, a bit more work is required)
There might be a little more simpler approach to this, use replace instead of looping through everything and use the substring method.
But then again, you also might look at your input. How does this "processor" knows what a word is? For a matter of fact, the word applea (apple a) might not be a word you are looking for, where this processor potentially will see it as a word (theoretical)
The best thing you can do is to separate your input, for example with an semicolon ";". Then you can use a split functionallity to make those values into a table (for example look at this post : T-SQL: split and aggregate comma-separated values). Next you can use the replace function on it.
You get something like this
select replace(s.value, ' ' , ''), * from split(#value) as s