0

I am writing a simple query to display the names of columns in my table.

This is the SQL I used:

select column_name from cols where table_name='StudentInfo';

For some reason, this query results in an empty result table even though my StudentInfo table does indeed have columns.

4
  • Try select distinct table_name from cols and check if "StudentInfo" shines up that way. Probably table names are stored in capital letters or in any other slightly different way. Commented Mar 1, 2017 at 7:01
  • Boom. You just solved my problem. Any idea why oracle has such sharp case sensitivity? Commented Mar 1, 2017 at 7:05
  • will definitely give an upvote and a check mark for that one. Thank you! Really appreciate it Commented Mar 1, 2017 at 7:08
  • see edited answer concerning case sensitivity. Commented Mar 1, 2017 at 7:12

2 Answers 2

2

Table names might be stored in capital letters such that a condition table_name='StudentInfo' fails. Note that Oracle (and most other RDBMS I know) compare strings in a case sensitive manner. For a case insensitive comparison, use UPPER (or LOWER) on both arguments. So the following query should work safely:

select column_name from cols where upper(table_name)=upper('StudentInfo')

There are other ways of turning string comparison into case insensitive, like altering session parameters NLS_COMP and NLS_SORT (cf., for example, https://stackoverflow.com/a/5391234/2630032 and upvote this reference if applicable). By using UPPER, however, you make your query independent of such settings.

Sign up to request clarification or add additional context in comments.

Comments

1

You can go for this:

select column_name from cols where lower(table_name) like '%studentinfo%'

2 Comments

@StephanLechner Yes! It would display all records containg the string 'studentinfo'. But if we only want records which has the required sign then % should be removed from the query.
Note that like is also case sensitive (depending on settings). See stackoverflow.com/a/5391234/2630032

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.