November 2, 2006 at 3:27 pm
I have a field in a database that looks like this:
7104 7106 7112 7107 7132 7108 7150 7105 7110 7109 7111 7104 7106 7112 7107 7132 7108 7150 7105 7110 7109 7111 2
The careful observer will notice that the value 7104 appears twice.
Is there T-SQL that can parse that field for the number of times that 7104 shows up?
Thanks in advance?
November 2, 2006 at 3:38 pm
declare @col varchar(2000)
select @col ='7104 7106 7112 7107 7132 7108 7150 7105 7110 7109 7111 7104 7106 7112 7107 7132 7108 7150 7105 7110 7109 7111 2'
select (len(@col)-len(replace(@col,'7104','')))/len('7104')
Cheers,
* Noel
November 2, 2006 at 3:41 pm
Noel, thanks for the prompt response. I should have mentioned that the string of numbers is variable, will this still work?
For example the very next entry might be
7104 7106 7112 7107 7132 7108 7150 7105 7110 7109 7111 2
November 2, 2006 at 3:44 pm
Sure!
declare @col varchar(2000), @valueToSearchFor varchar(200)
select @col ='7104 7106 7112 7107 7132 7108 7150 7105 7110 7109 7111 7104 7106 7112 7107 7132 7108 7150 7105 7110 7109 7111 2'
,@valueToSearchFor ='7104'
select (len(@col)-len(replace(@col,@valueToSearchFor,'')))/len(@valueToSearchFor)
just don't use a zero length @valueToSearchFor
Cheers,
* Noel
November 2, 2006 at 3:51 pm
Please tolerate the newbie! The column in question is called "ivrs"
When I run this:
select (len(ivrs)-len(replace(ivrs,'7104','')))/len('7104')
where projectid = 2223
and systemid = 6
and calldatetime between
'2006-10-01 00:00:00' and '2006-10-31 23:59:59'
and attresult in (2)
I get this error message set:
Msg 207, Level 16, State 1, Line 2
Invalid column name 'projectid'.
Msg 207, Level 16, State 1, Line 3
Invalid column name 'systemid'.
Msg 207, Level 16, State 1, Line 4
Invalid column name 'calldatetime'.
Msg 207, Level 16, State 1, Line 4
Invalid column name 'calldatetime'.
Msg 207, Level 16, State 1, Line 6
Invalid column name 'attresult'.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'ivrs'.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'ivrs'.
November 2, 2006 at 4:12 pm
You should not.
What you have is not data and storage of it is not a database.
To make it data convert all those strings into a column in some table (you may find necessary "split" function on this forum) and than manage data with database tools.
_____________
Code for TallyGenerator
November 2, 2006 at 4:19 pm
I'd love to Sergiy but it's not my data model. Is there a way to parse the string for the number of occurances?
November 2, 2006 at 4:38 pm
Just curious, but I don't see a FROM in the SELECT, is something missing?
November 2, 2006 at 4:45 pm
There is no data model. Neither yours nor anybody's else. Noone just biult one.
I answered your question, you just did not bother to read.
Lets try again:
convert string into single column table (you may find necessary "split" function on this forum) and than manage data with database tools.
_____________
Code for TallyGenerator
November 2, 2006 at 5:41 pm
thank you (boy I need to get some sleep!) and thank you for a kind response to a newbie
November 2, 2006 at 5:44 pm
Here is your select statement:
select
(len(ivrs)-len(replace(ivrs,'7104','')))/len('7104')
where
projectid = 2223
and systemid = 6
and calldatetime between '2006-10-01 00:00:00' and '2006-10-31 23:59:59'
and attresult in (2)
Based on this, there should be a FROM clause with the TABLE name where the columns
ivrs, projectid, systemid, calldatetime, and attresult reside, like this:
select
(len(ivrs)-len(replace(ivrs,'7104','')))/len('7104')
from
dbo.sometable
where
projectid = 2223
and systemid = 6
and calldatetime between '2006-10-01 00:00:00' and '2006-10-31 23:59:59'
and attresult in (2)
If this is not the case, where is the data coming from, and where are you trying to put it?
November 2, 2006 at 5:55 pm
Hi
Try this out (No Split/ No Parse)
DECLARE @col VARCHAR(2000)
DECLARE @FF VARCHAR(10)
SELECT @FF = '7104'
SELECT @col ='7104 7106 7112 7107 7132 7108 7150 7105 7110 7109 7111 7104 7106 7112 7107 7132 7108 7150 7105 7110 7109 7111 2'
SELECT CAST(DATALENGTH(@Col)-DATALENGTH(REPLACE(@Col,@FF,'')) AS INT) / LEN(@FF)
Ram
November 2, 2006 at 7:34 pm
Sriram, I'm afraid, value '7104' is not given, you must to retrive duplicated values from the string inside your script.
And it may be not a single duplication in the string. You must discover and eliminate them all.
_____________
Code for TallyGenerator
November 2, 2006 at 8:43 pm
Hi Guy!
I think the correct T-sql statement is:
select (len(ivrs)-len(replace(ivrs,'7104','')))/len('7104') from <table name>
where projectid = 2223
and systemid = 6
and calldatetime between
'2006-10-01 00:00:00' and '2006-10-31 23:59:59'
and attresult in (2)
You should give a table name in select statement, in which you have a where clause.
Please check that out and get back to me guy.
Thanks
November 3, 2006 at 5:35 am
In case you haven't found a split function amongst the zillion sitting out there here is one that should take care of your problem. Then all you need to do is
select sval,count(*)
from dbo.split('7104 7106 7112 7107 7132 7108 7150 7105 7110 7109 7111 7104 7106 7112 7107 7132 7108 7150 7105 7110 7109 7111 2')
group by sval
having count(*)>1
create function split(@s varchar(1000)) returns @STAB table(sval varchar(4))
as
begin
declare @start int
declare @end int
if len(@s)>0
begin
set @start=1
set @end=charindex(' ',@s)
while @end>0
begin
insert into @STAB values(substring(@s,@start,@end-@start))
set @start=@end+1
set @end=charindex(' ',@s,@end+1)
end
insert into @STAB values(substring(@s,@start,len(@s)-@start+1))
end
return
end
Viewing 15 posts - 1 through 15 (of 16 total)
You must be logged in to reply to this topic. Login to reply