Results 1 to 5 of 5

Thread: Querying But Considering the Cases

  1. #1

    Thread Starter
    Addicted Member charmedcharmer's Avatar
    Join Date
    Sep 2003
    Posts
    211

    Querying But Considering the Cases

    Hi guys,

    I was hoping you could help me. I have a problem regarding querying records but I need to consider the cases of the characters.

    For example, the 'text_msg' field has 'Hello' and 'hello' on it.

    I want to query something like this:
    select * from <table> where text_msg = 'Hello'
    (which returns records with 'Hello' and exclude 'hello' records

    and
    select * from <table> where text_msg = 'hello'
    (which returns records with 'hello and exclude 'Hello' records.

    Thanks in advance
    C++ Programming is overwhelming.

    Dont let it overwhelm you or you'll fall into the oblivion of its perfection

  2. #2
    Hyperactive Member
    Join Date
    Jan 2008
    Location
    Merseyside
    Posts
    456

    Re: Querying But Considering the Cases

    Using T-SQL (based on SQL Server) i would use a combination of the LEFT function and the ASCII funtion to get the ASCII value of the first character.

    Then you can evalute what to do. See example below

    Code:
    DECLARE @Test1 AS VARCHAR(10)
    
    SET @Test1 = 'Hello'
    
    SELECT ASCII(LEFT(@Test1,1))
    
    IF ASCII(LEFT(@Test1,1)) BETWEEN 65 AND 90
    BEGIN 
    SELECT 'Uppercase'
    -- You would place SELECT code in here for UPPERCASE search
    END
    
    IF ASCII(LEFT(@Test1,1)) BETWEEN 97 AND 122
    BEGIN
    SELECT 'Lowercase'
    -- You would place SELECT code in here for LOWER search
    END
    
    
    -- Examples wherer you would use it in the WHERE clause
    -------------------------------------------------------
    
    -- Where the first Character it NOT Lower Case
    SELECT @Test1
    WHERE NOT(ASCII(LEFT(@Test1,1)) BETWEEN 97 AND 122)
    
    -- Where the first Character it NOT Upper Case
    SELECT @Test1
    WHERE NOT(ASCII(LEFT(@Test1,1)) BETWEEN 65 AND 90)
    
    /*
    ASCII Ranges
    65 - 90 Upper Case
    97 - 122 Lower Case
    */
    Run all the code to see the results. Notice that if you change

    Code:
    SET @Test1 = 'Hello'
    to

    Code:
    SET @Test1 = 'hello'
    The results of the bottom two SELECT's change
    Last edited by kevchadders; May 23rd, 2008 at 03:40 AM.

  3. #3
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Querying But Considering the Cases

    Code:
    select * from <table> where LOWERCASE(text_msg) = 'hello'
    Not sure what DB language you use.
    Replace LOWERCASE() with equivalent function of your DB language, such as LCase() in Access.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Querying But Considering the Cases

    Use COLLATE.

    Code:
    WHERE text_msg = 'Hello' COLLATE SQL_Latin1_General_CP1_CS_AS

  5. #5

    Thread Starter
    Addicted Member charmedcharmer's Avatar
    Join Date
    Sep 2003
    Posts
    211

    Re: Querying But Considering the Cases

    Let me check on that now. Thanks. Ill go back with results. Thanks
    C++ Programming is overwhelming.

    Dont let it overwhelm you or you'll fall into the oblivion of its perfection

Posting Permissions

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



Click Here to Expand Forum to Full Width