1

I am having an exam on SQL which I have rarely ever worked on. While going through the study material I encountered this example:

DELETE FROM table_name WHERE
CAST (SUBSTRING (attribute_name from x for y) AS INTEGER) =z;

Now, I am guessing that this would delete a specific line, where an attribute name would be specified in the code, but am unsure.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Nobody
  • 33
  • 8
  • From [How do I ask and answer homework questions?](https://meta.stackoverflow.com/q/334822/10676716): *Make a good faith attempt to solve the problem yourself first.* – GMB Feb 29 '20 at 18:10
  • as said, I don't have much clue about SQL overall, and I did give my guess as I clearly have no clue what else it could do. Will check the link tho, thanks for the heads-up – Nobody Feb 29 '20 at 18:13
  • fixed the question as I've found that the code here was faulty... back to start? – Nobody Feb 29 '20 at 18:21
  • 1
    The part after _EDIT_ seems to be a separate question. Please only ask one question per question on stack overflow. I'll edit out the second part, if you want to ask another question, post a new question. – Mark Rotteveel Feb 29 '20 at 19:49

1 Answers1

3

The substring() function extracts part of string (from the column attribute_name) from the position numbered x for y characters (or until position x + y - 1). For instance 1 to 3 would be the first three characters.

This is then converted to an integer and compared to another value.

Rows where the comparison returns "true" are deleted.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • so in case that numbers would be x=2, y=1 and z=3, that would mean..? – Nobody Feb 29 '20 at 18:10
  • my bad, I have incorrectly written down the code, it seems to be `... FROM x FOR y ...`. Does that mean in any way a similar answer? – Nobody Feb 29 '20 at 18:20
  • 2
    @Nobody The `FROM .. TO ..` syntax does not exist. But `FROM x FOR y` means from position x for z characters, so `FROM 1 FOR 3` means the first three characters (characters 1, 2, 3), `FROM 2 FOR 3` mean characters 2, 3, 4, etc. See https://firebirdsql.org/file/documentation/reference_manuals/fblangref25-en/html/fblangref25-functions-scalarfuncs.html#fblangref25-functions-scalarfuncs-substring – Mark Rotteveel Feb 29 '20 at 19:53
  • so I guess I mark this answer as correct, even though, the comment in it really explained it? – Nobody Mar 01 '20 at 11:01