1

I have my table named material

pcs_meter
100 m
80 m

How can I select the pcs_meter column? like:

pcs_meter
100
80
7
  • SELECT +SUBSTRING_INDEX(pcs_meter, ' ', 1) FROM material; ? Commented Feb 14, 2014 at 8:25
  • there are several ways you could achieve this... I would suggest storing the values in an array, and affecting those values through php sting functions to truncate out the measurement unit. Commented Feb 14, 2014 at 8:25
  • 4
    You should normalize your database. One of the steps says, keep values atomic. Means, put 100 in one column and m in a second column. Commented Feb 14, 2014 at 8:25
  • @DanFromGermany I totaly agree Commented Feb 14, 2014 at 8:25
  • 1
    @AlmaDo it has, First normal form: en.wikipedia.org/wiki/First_normal_form A relation is in first normal form if the domain of each attribute contains only atomic values, and the value of each attribute contains only a single value from that domain. I'm working for a webshop company and that's why we always put currency and value in different columns. Commented Feb 14, 2014 at 8:28

2 Answers 2

5

You can achieve that with:

SELECT CAST(pcs_meter AS UNSIGNED) FROM material

That will work since your value goes first (and MySQL will truncate non-significant symbols from the right). If your field can contain negative values, change UNSIGNED to SIGNED

Sign up to request clarification or add additional context in comments.

2 Comments

Must have been me, by accident?! Can you hit edit question I'll turn this into an upvote.
Doubt (it was after some certain time) - but ok, improved.
0

Use this:

SELECT SUBSTRING_INDEX(pcs_meter,' ', 1) FROM material;

2 Comments

this would require every entry to have a space before the measurement unit. If that can be verified as true - than, this would work perfectly.
Isnt that how you will be storing the data?

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.