0

Nice easy one for this morning.

Ok, here's my little sql statement

SELECT SUBSTR(quote,1,20) FROM b_quotes WHERE id='74'

This is returning an empty result which is confusing because if I call upon any other part of that record (the customers email address for example) it returns it perfectly. I've tried variations and it always seems to be the SUBSTR part that is failing.

could anyone shed some light on this?

Thanks Shane

5
  • 1
    Could you show us some sample data from: SELECT quote, SUBSTR(quote,1,20) FROM b_quotes WHERE id='74'? Commented Feb 19, 2011 at 8:13
  • 1
    @a'r Using the code you just posted, I get the entire sting as a result, not what I'd like (the first 20) Commented Feb 19, 2011 at 8:16
  • Try SELECT SUBSTR(quote,1,20) AS q FROM b_quotes WHERE id='74' Commented Feb 19, 2011 at 8:20
  • Does the following work: SELECT SUBSTR(quote,1,LEN(quote)) FROM b_quotes WHERE id='74' ??? Commented Feb 19, 2011 at 8:20
  • @xzyfer Thanks, this works perfectly. And to everyone else, thanks also for your time. Commented Feb 19, 2011 at 8:33

3 Answers 3

1

Since you haven't indicated which data type is used for the quote column, try this:

SELECT SUBSTR(CAST(quote as CHAR),1,20) FROM b_quotes WHERE id='74'
Sign up to request clarification or add additional context in comments.

Comments

0

Try SELECT SUBSTR(quote,1,20) AS q FROM b_quotes WHERE id='74'

2 Comments

Actually, while a reader passing by might think that there's a need to add a column alias name, this statement is totally equivalent to the one posted in the question, which is already correct. I believe that this works for @shane because of the way he/she is accessing the data returned from database. So the error is probably in the application code and NOT in the SQL statement.
@Alessandro - That's exactly what's happening. I'm accessing the information in this manner $value['q'] whilst in a foreach loop
0

What's the datatype of the quote column? If it's a CHAR or VARCHAR, what's its length? What code are you using to access the data returned from the database?

Your SQL statement is correct, so if you want to avoid workarounds and just want to know why your query doesn't seem to work (as you asked) you have to research problems in your application code.

Comments

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.