mardi 6 août 2019

SQL IF Statement based on an external variable length [duplicate]

This question already has an answer here:

I'm trying to create a query that depending on an external variable length will run a query with a different WHERE clause.

psuedo code:

bnum is a value entered by a user

IF bnum is 4 characters long 
SELECT Col1 from table Where Col2 = bnum
ELSE bnum is 5 characters long
SELECT Col1 from table where Col3 = bnum

At this point it would be ideal to be able to judge the length and run the appropriate query but I'm open to other options as my approach may be totally off.

I've tried a basic select statement with two where clauses but get invalid column. Tried using EXISTS but it returns all records. It seems that the LEN() function has to be a result of a query, so that doesn't seem to be an option.

Aucun commentaire:

Enregistrer un commentaire