Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PostgreSQL – I have a Syntax error in my SUBSTRING query

I’m trying to make use of the SUBSTRING() function to extract a substring from vm.location_path, starting at the second character and ending at the position of the ‘]’ character, minus two.

I want to extract the text between the square brackets ([]) in vm.location_path but I’m hitting a syntax error.

SELECT
    'UPDATE vm SET dstore_moref = ''' || datastore_inv.moref || ''' WHERE id = ''' || vm.id || ''';'
FROM
    vm
INNER JOIN vapp_vm ON vapp_vm.svm_id = vm.id
INNER JOIN vm_inv ON vm_inv.moref = vm.moref
INNER JOIN datastore_inv ON datastore_inv.vc_display_name =(
        SUBSTRING(
            vm.location_path,
            2,
            POSITION(']',
            vm.location_path) - 2
        )
    )
WHERE
    vm.dstore_moref IS NULL AND vm_inv.is_deleted IS FALSE
GROUP BY datastore_inv.moref, vm.id;
SQL Error [42601]: ERROR: syntax error at or near ","
  Position: 370


Error position: line: 11 pos: 369

It’s between the comma at the end of

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

POSITION(']',

and

vm.location_path) - 2

What am I not seeing?

This is for vCloud Director. I am trying to get a print out of all VMs that are NULL.

>Solution :

The syntax is POSITION(search_string in main_string) with IN Keyowrd instead of ,

SELECT
    'UPDATE vm SET dstore_moref = ''' || datastore_inv.moref || ''' WHERE id = ''' || vm.id || ''';'
FROM
    vm
INNER JOIN vapp_vm ON vapp_vm.svm_id = vm.id
INNER JOIN vm_inv ON vm_inv.moref = vm.moref
INNER JOIN datastore_inv ON datastore_inv.vc_display_name =(
        SUBSTRING(
            vm.location_path,
            2,
            POSITION(']' IN vm.location_path) - 2
        )
    )
WHERE
    vm.dstore_moref IS NULL AND vm_inv.is_deleted IS FALSE
GROUP BY datastore_inv.moref, vm.id;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading