Results 1 to 12 of 12
  1. #1
    Vaporhead is offline Novice
    Windows 10 Office 365
    Join Date
    Feb 2024
    Posts
    13

    Count first character of string

    Good morning.

    You guys/gals have been most helpful to me as I try and setup my database.

    I am trying to count a field, but only count the first character of the values.

    Example:

    The values in my field are entered like:

    3, (II,C)
    3, (III,B)
    5, (IV,C)
    5, (IV,D)




    I would like my query to count just the first character, in this example, '3' and '5', so the count return would be 2x '3' and 2x '5' without including anything after the first character.

    Mark

  2. #2
    madpiet is online now Competent Performer
    Windows 10 Office 365
    Join Date
    Feb 2023
    Posts
    156
    SELECT fc.FirstChar, COUNT(*)
    FROM
    ( SELECT LEFT$([ColumnName],1) AS FirstChar FROM MyTable ) fc
    GROUP BY fc.FirstChar;

  3. #3
    Join Date
    Apr 2017
    Posts
    1,681
    Something like
    Code:
    SELECT Left(YourField,1) As FirstChar, COUNT( Left(YourField,1)) As FirstCharCnt 
    FROM YourTable 
    GROUP BY Left(YourField,1)

  4. #4
    Vaporhead is offline Novice
    Windows 10 Office 365
    Join Date
    Feb 2024
    Posts
    13
    Perfecto! Thanks All!

  5. #5
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Don't think need to repeat Left() expression in Count(), just Count(*).
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,803
    Val would be a lot simpler (assuming the field is never Null)?
    EDIT - I meant vs Left or similar, just to get the first character, which appears to always be a number)
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  7. #7
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Good point. What should happen if 'first character' could actually end up being more than 1 digit? Probably this data should be split into 3 fields.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  8. #8
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,803
    Val reads the string from left to right and stops when it finds anything that does not resemble a number. So if that was 123, CXXIII you'd get 123. Should not be a problem then?
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  9. #9
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Not if user really wants all digits to be considered. Assume that is the case until hear different. And as you indicated, errors on Null so may need Nz() as well.

    Does not stop for period which is read as a decimal (or maybe a comma depending on regional settings).

    Also assumes first character will always be numeric, as shown in example data.

    Real solution is discrete data in own fields.
    Last edited by June7; 04-03-2024 at 01:31 AM.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  10. #10
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,803
    Does not stop for period which is read as a decimal
    Good point Val(.456) returns 0.456
    All depends on the data, but yes, discrete data is usually the best approach.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  11. #11
    Join Date
    Apr 2017
    Posts
    1,681
    In his/her opening post, OP clearly declared, that the count must be based on 1st character of entries in column - not the numeric part at left of entry, or not all left at dot there;
    The examples of data given there had a number as leftmost character, but there was nowhere declared, that this applied to all data in column;
    So using LEFT(YourField,1) was giving what OP asked for, whatever the entries in column were.

  12. #12
    Bulzie is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    Nov 2015
    Posts
    1,474
    Vapor are the only values possible for that first character 0-9 (only 1 character in length?)?

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Convert a single character to string
    By cesarone82 in forum Access
    Replies: 3
    Last Post: 09-08-2023, 03:37 AM
  2. Replies: 3
    Last Post: 07-20-2020, 01:04 PM
  3. Character Replacement by Position in String
    By J Bhujanga in forum Queries
    Replies: 5
    Last Post: 07-08-2017, 05:16 PM
  4. DLookup 2nd character in a text string
    By DubCap01 in forum Forms
    Replies: 5
    Last Post: 03-06-2015, 02:37 PM
  5. How do I return last character in a string?
    By SSgtBarry in forum Queries
    Replies: 4
    Last Post: 06-22-2014, 08:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums