| Author |
Topic |
|
pelegk2
Aged Yak Warrior
723 Posts |
Posted - 2005-12-28 : 06:23:53
|
| i want to count the number of words in a colum of type nvarchar(250)o know how many word it contains and which of the cels contain more then 2 wordshow can i do this?(of course the words are seperated with space)thnaks in advancepelegIsrael -the best place to live in aftr heaven 9but no one wan't to go there so fast -:) |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-12-28 : 06:34:29
|
| Here is one of the waysDeclare @n nvarchar(2500)Set @n='This is to test how many words this variable has'select len(@n)-len(replace(@n,' ',''))+1 as No_of_WordsMadhivananFailing to plan is Planning to fail |
 |
|
|
pelegk2
Aged Yak Warrior
723 Posts |
Posted - 2006-01-01 : 14:39:17
|
| nice trick:)Israel -the best place to live in aftr heaven 9but no one wan't to go there so fast -:) |
 |
|
|
pelegk2
Aged Yak Warrior
723 Posts |
Posted - 2006-01-03 : 08:35:03
|
| but what if you have 2 spaces? it will count more then needed!Israel -the best place to live in aftr heaven 9but no one wan't to go there so fast -:) |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-01-03 : 08:44:09
|
| [code]Declare @n nvarchar(2500)Set @n='This is to test how many words this variable has'set @n=Replace(Replace(Replace(@n,' ',' ^'),'^ ',''),'^','')select len(@n)-len(replace(@n,' ',''))+1 as No_of_Words[/code]MadhivananFailing to plan is Planning to fail |
 |
|
|
pelegk2
Aged Yak Warrior
723 Posts |
Posted - 2006-01-03 : 15:58:30
|
| can u explain what going on?Israel -the best place to live in aftr heaven 9but no one wan't to go there so fast -:) |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2006-01-03 : 18:01:28
|
Modified to allow for leading/trailing spaces and to add comments.declare @n varchar(8000)Set @n= ' This is to test how many ' + ' words this variable has '-- Replace all multiple spaces with a single space-- by nesting REPLACE functions.-- Works with strings of up to 8000 charactersset @n = replace(replace(replace(replace(replace( replace(ltrim(rtrim(@n)),replicate(' ',87),' ') ,' ',' '),' ',' '),' ',' ') ,' ',' '),' ',' ')-- Count the number of spaces in the stringselect len(@n)-len(replace(@n,' ',''))+1 as No_of_WordsCODO ERGO SUM |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-01-04 : 00:20:39
|
Well. Trailing spaces are automatically omitted. So only leading spaces are to be consideredHere is the new approachDeclare @n nvarchar(2500)Set @n= ' This is to test how many ' + ' words this variable has 'set @n=Replace(Replace(Replace(@n,' ',' ^'),'^ ',''),'^','')select len(@n)-len(replace(@n,' ',''))+case when left(@n,1)=' ' then 0 else 1 end as No_of_Words MadhivananFailing to plan is Planning to fail |
 |
|
|
pelegk2
Aged Yak Warrior
723 Posts |
Posted - 2006-01-04 : 01:06:37
|
| what does the ' ^' means?Israel -the best place to live in aftr heaven 9but no one wan't to go there so fast -:) |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-01-04 : 01:19:58
|
| >>what does the ' ^' means?It doesnt mean anything. It is just used as temporary character to replace space. You can use # or ~ as wellMadhivananFailing to plan is Planning to fail |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2006-01-04 : 10:36:49
|
quote: Originally posted by madhivanan Well. Trailing spaces are automatically omitted. So only leading spaces are to be consideredHere is the new approachDeclare @n nvarchar(2500)Set @n= ' This is to test how many ' + ' words this variable has 'set @n=Replace(Replace(Replace(@n,' ',' ^'),'^ ',''),'^','')select len(@n)-len(replace(@n,' ',''))+case when left(@n,1)=' ' then 0 else 1 end as No_of_Words MadhivananFailing to plan is Planning to fail
It does not work in all cases. The Replace(@n,' ',' ^') can exceed the length of the string and give the wrong results. See example below:Declare @n nvarchar(2500)Set @n= ' This is to test' +replicate(' ',2000)+ 'how many ' + ' words this variable has 'set @n=Replace(Replace(Replace(@n,' ',' ^'),'^ ',''),'^','')select len(@n)-len(replace(@n,' ',''))+case when left(@n,1)=' ' then 0 else 1 end as No_of_WordsNo_of_Words ----------- 4(1 row(s) affected)CODO ERGO SUM |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-01-05 : 01:16:43
|
Well. It works if you change nvarchar to varcharDeclare @n varchar(2500)Set @n= ' This is to test' +replicate(' ',2000)+ 'how many ' + ' words this variable has 'set @n=Replace(Replace(Replace(@n,' ',' ^'),'^ ',''),'^','')select len(@n)-len(replace(@n,' ',''))+case when left(@n,1)=' ' then 0 else 1 end as No_of_WordsBut still it doesnt work other cases like varchar(7500) and replicate(' ',7000)Yours is best approach MadhivananFailing to plan is Planning to fail |
 |
|
|
pelegk2
Aged Yak Warrior
723 Posts |
Posted - 2006-01-05 : 01:24:17
|
| why is there a diffrence between the cases of using varchar and nvarchar?Israel -the best place to live in aftr heaven 9but no one wan't to go there so fast -:) |
 |
|
|
|