Adding numbers from a nvarchar field

Ask a Question related to ASP Database, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. 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...
    2. 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...
    3. 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...
    4. 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...
    5. 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...
  3. #2

    Default Re: Adding numbers from a nvarchar field

    JayG wrote:
    > 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,
    >
    Use a CASE expression in your query (Don't do it in vbscript - very
    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

  4. #3

    Default 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

  5. #4

    Default Re: Adding numbers from a nvarchar field

    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 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

  6. #5

    Default Re: Adding numbers from a nvarchar field


    "Bob Barrows" <reb01501@NOyahoo.SPAMcom> wrote in message
    news:%23ZkDhU33DHA.556@TK2MSFTNGP11.phx.gbl...
    > 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
    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.
    > --

    Problem solved - thanks very much. Used the dynamic SQL query method in the
    end.


    Thanks,


    Jay


    JayG Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139