SQL query to return only if capitalized

Asked

Viewed 923 times

1

I would like to make a query in a column and that the return is only the uppercase words that correspond to the searched criteria (user input), no matter if the criterion was typed in uppercase or lowercase.

CREATE PROCEDURE uspConsultaSobrenome

@Sobrenome nvarchar(MAX)

AS
   BEGIN

   SELECT

   SobrenomeID, 
     Sobrenome  
        FROM
            tbl_Sobrenomes
        WHERE 
        Sobrenome LIKE @Sobrenome + N'%'
END

I did so and the answer is both lowercase and uppercase.

  • https://msdn.microsoft.com/pt-br/library/ms180055.aspx use the Function UPPER

  • I would not like to convert, but rather that the query read only the words that are already capitalized in the column "Last name" and return these words and the others ignored.

  • Last name LIKE upper(@Last name + N'%') must resolve

  • @Motta, not solved. Return also the "last names" in lower case in the column "Last names".

  • Surname LIKE upper(@Surname + N'%' and Surname = upper(Surname)

  • @Motta, unfortunately not either. Keeps returning both words.

  • so I’m sorry I didn’t understand the problem.

  • @Motta, on the line there are several words. I want you to refer to return the word that meets the criteria, as long as it is capitalized.

  • Show examples in the definition please.

  • I put an example image @Motta

  • And in case your example is to return what ?

  • the surname in capital letters. Example: ADAMI.

  • https://docs.microsoft.com/en-us/sql/t-sql/functions/charindex-transact-sql try using FUNCTION CHARINDEX , maybe combining with SUBSTR to get only the part that matters.

Show 8 more comments

1 answer

2


By default the collation of the tables is case insensitive, to learn more about collation click here, so you have two alternatives: or change the collation of your table column

alter table Foo alter column Bar ntext collate Latin1_General_CS_AS

or run the query by specifying a case sensitive collation

select * from nomes where nome COLLATE Latin1_General_CS_AS ='fulano' 
  • Yes, you did. The result was as expected. Thanks for the tip and for the reply.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.