Ask a Question related to ASP Database, Design and Development.
-
JayG #1
Adding numbers from a nvarchar field
Hi all,
Would really appreciate any help on a problem I'm having with an SQL
database and web 'front end' used to display the information.
Basically I have a field in an SQL db which data gets fed into
automatically. It is called 'NumberOfUsers'. This field can contain
integers or a set text so it was initially set to be nvarchar type and
everything was fine.
Now it is required that the numbers from all these fields be added
together to form a total. Does anyone know how I could go about, in my
ASP script (VBScript), adding all the numbers up from each recordset but
IGNORE the text that is displayed?
Apologies if this sounds a bit vague - please let me know if more
detailed information is required.
Many thanks,
JayG
*** Sent via Developersdex [url]http://www.developersdex.com[/url] ***
Don't just participate in USENET...get rewarded for it!
JayG Guest
-
Adding Page Numbers to Printed Repetitions
Greetings, I have an odd situation that requires me to print consecutive page numbers on printed documents, but not in the usual sense. We need to... -
Adding numbers
Hi Guys, I can use your help here, how can I calculate the value entered by the user on the given fields of a form and at the end give me the total... -
adding the numbers
Hi Guys, I can use your help here, how can I calculate the value entered by the user on the given fields of a form, and at the end give me the... -
SQL: Order by text & numbers in same field
Hi I have a problem, don't think there's an easy solution but here goes: I have a db text field that contains text and numbers. I need to sort my... -
Adding physical pages numbers with prefixes to PDF
I'm using Acrobat 6.0 Professional on Windows XP. I need to add prefixed page numbers physically to PDFs created from scanned legal documents (for... -
Bob Barrows #2
Re: Adding numbers from a nvarchar field
JayG wrote:
Use a CASE expression in your query (Don't do it in vbscript - very> Hi all,
>
> Would really appreciate any help on a problem I'm having with an SQL
> database and web 'front end' used to display the information.
>
> Basically I have a field in an SQL db which data gets fed into
> automatically. It is called 'NumberOfUsers'. This field can contain
> integers or a set text so it was initially set to be nvarchar type and
> everything was fine.
>
> Now it is required that the numbers from all these fields be added
> together to form a total. Does anyone know how I could go about, in
> my ASP script (VBScript), adding all the numbers up from each
> recordset but IGNORE the text that is displayed?
>
> Apologies if this sounds a bit vague - please let me know if more
> detailed information is required.
>
> Many thanks,
>
inefficient - always use SQL to do your aggregations). I can't get too
specific because you did not provide a few rows of sample data but something
like this:
select sum(case NumberOfUsers when '<text to be ignored>' then 0
else NumberOfUsers end) UserTotal ...
HTH,
Bob Barrows
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Bob Barrows Guest
-
JayG #3
Re: Adding numbers from a nvarchar field
Thanks for the reply. I think I'm almost there now - but the thing is
there are 2 seperate instances of text to check for. At the moment I
have:
strQ = "SELECT sum(case when NumberOfUsers = 'Not Set' then 0 else
NumberOfUsers end) as UserTotal FROM Main "
Set objRS = objConn.Execute(strQ)
But I get an error back because some records have the text 'Unlimited'
in the NumberOfUsers field. How do I check for this as well?
Thanks,
Jay
*** Sent via Developersdex [url]http://www.developersdex.com[/url] ***
Don't just participate in USENET...get rewarded for it!
JayG Guest
-
Bob Barrows #4
Re: Adding numbers from a nvarchar field
JayG wrote:
I goofed. You cannot use CASE for this. The problem is, CASE does not use> Thanks for the reply. I think I'm almost there now - but the thing is
> there are 2 seperate instances of text to check for. At the moment I
> have:
>
> strQ = "SELECT sum(case when NumberOfUsers = 'Not Set' then 0 else
> NumberOfUsers end) as UserTotal FROM Main "
>
> Set objRS = objConn.Execute(strQ)
>
>
> But I get an error back because some records have the text 'Unlimited'
> in the NumberOfUsers field. How do I check for this as well?
>
short-circuit evaluation. It evaluates both result expressions for every
row, throwing an error on the rows containing the alpha characters.
You need to explicitly cast the data into a numeric datatype, using the
WHERE clause to prevent errors. Like this:
strQ = "SELECT sum(CAST(NumberOfUsers as int)) as UserTotal " & _
"FROM Main WHERE NumberOfUsers != 'Not Set' AND " & _
"NumberOfUsers != 'Unlimited'
In this case, you could get away with this:
strQ = "SELECT sum(CAST(NumberOfUsers as int)) as UserTotal " & _
"FROM Main WHERE ISNUMERIC(NumberOfUsers) = 1"
Better yet, if there is an index on NumberOfUsers, this will perform better
(the criteria in this case is SARGable:
strQ = "SELECT sum(CAST(NumberOfUsers as int)) as UserTotal " & _
"FROM Main WHERE NumberOfUsers) < 'a'"
I prefer using stored procedures vs. dynamic sql:
CREATE PROCEDURE GetUserCount AS
SELECT sum(CAST(NumberOfUsers AS int)) as UserTotal
FROM Main
WHERE NumberOfUsers < 'a'
In ASP:
Set objRS = createobject("adodb.recordset")
objConn.GetUserCount objRS
HTH,
Bob Barrows
PS. Why is this column nvarchar? You don't seem to be storing any
internation characters in it. You could probably save space by making it a
varchar column.
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
Bob Barrows Guest
-
JayG #5
Re: Adding numbers from a nvarchar field
"Bob Barrows" <reb01501@NOyahoo.SPAMcom> wrote in message
news:%23ZkDhU33DHA.556@TK2MSFTNGP11.phx.gbl...better> JayG wrote:>> > Thanks for the reply. I think I'm almost there now - but the thing is
> > there are 2 seperate instances of text to check for. At the moment I
> > have:
> >
> > strQ = "SELECT sum(case when NumberOfUsers = 'Not Set' then 0 else
> > NumberOfUsers end) as UserTotal FROM Main "
> >
> > Set objRS = objConn.Execute(strQ)
> >
> >
> > But I get an error back because some records have the text 'Unlimited'
> > in the NumberOfUsers field. How do I check for this as well?
> >
> I goofed. You cannot use CASE for this. The problem is, CASE does not use
> short-circuit evaluation. It evaluates both result expressions for every
> row, throwing an error on the rows containing the alpha characters.
>
> You need to explicitly cast the data into a numeric datatype, using the
> WHERE clause to prevent errors. Like this:
>
> strQ = "SELECT sum(CAST(NumberOfUsers as int)) as UserTotal " & _
> "FROM Main WHERE NumberOfUsers != 'Not Set' AND " & _
> "NumberOfUsers != 'Unlimited'
>
> In this case, you could get away with this:
> strQ = "SELECT sum(CAST(NumberOfUsers as int)) as UserTotal " & _
> "FROM Main WHERE ISNUMERIC(NumberOfUsers) = 1"
>
>
> Better yet, if there is an index on NumberOfUsers, this will perform> (the criteria in this case is SARGable:
>
> strQ = "SELECT sum(CAST(NumberOfUsers as int)) as UserTotal " & _
> "FROM Main WHERE NumberOfUsers) < 'a'"
>
> I prefer using stored procedures vs. dynamic sql:
>
> CREATE PROCEDURE GetUserCount AS
> SELECT sum(CAST(NumberOfUsers AS int)) as UserTotal
> FROM Main
> WHERE NumberOfUsers < 'a'
>
> In ASP:
>
> Set objRS = createobject("adodb.recordset")
> objConn.GetUserCount objRS
>
> HTH,
> Bob Barrows
> PS. Why is this column nvarchar? You don't seem to be storing any
> internation characters in it. You could probably save space by making it a
> varchar column.
> --
Problem solved - thanks very much. Used the dynamic SQL query method in the
end.
Thanks,
Jay
JayG Guest



Reply With Quote

