Hello. At performance of the labs, concerning line functions it faced the interesting task. There is an example:

SELECT COUNT(passport_series) WHERE POSITION('0301' IN 'passport_series') > 0 FROM students; 

The essence of the task is to count the number of students whose passport series is equal to "0301". My code caused doubts. After all, the function "POSITION" returns the symbol number (occurrences) of it in the string, well, or the number of the substring entry in the string, right? He asked the teacher, and he says that everything is correct. Naturally knocks an error - another and did not expect. The question is, how can you count the number of students with this series of passports, with the help of some line function, and in general is there such? Thank.

  • And what error do you have? - Vlad from Moscow
  • I think you need to remove the quotes from passport_series. WHERE POSITION ('0301' IN passport_series)> 0 - Vlad from Moscow
  • @MuscledBoy, why not WHERE passport_series = '0301' ? - Visman
  • @Mike, there are only 4 digits in the series. - Visman
  • 2
    And why you have 301 in the field, this can not be if you correctly inserted (modified) the record, enclosing the string value '0301' in quotes. This could happen only if you wrote update/insert set series=0301 i.e. without quotes. If the database contains valid data, then search by the usual equality where series='0301' - Mike

2 answers 2

In principle, the code from the example was correct, except for the moment with FROM (it is placed after COUNT) and the ">" sign is apparently copied from there:

 SELECT COUNT(passport_series) FROM students WHERE POSITION('0301' IN passport_series); 

The main error was with the input data: as answered above, the series are entered using '' .

    WHERE can be used with syntax only after FROM .

    • keep that in mind. times - Kryshtop
    • thanks for attention - Muscled Boy