Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How to test a variable to determine if it is all digits.

Posted on 2007-07-24
2
Medium Priority
?
243 Views
Last Modified: 2008-07-03
Say i've got a variable v_myvariable which is varchar(20) and equal to '01234tt567'.

In pl/pgsql, I would like to see a test that will determine if the variable is all digits.

100 points for each right answer.

-g
0
Comment
Question by:gmanpert
2 Comments
 
LVL 10

Accepted Solution

by:
ivanovn earned 2000 total points
ID: 19566550
CREATE OR REPLACE FUNCTION isdigit(text) RETURNS boolean as '

-- by Ron St.Pierre (rstpierre@syscor.com)
-- licensed under the GPL
--
-- determines whether or not a value is numeric
--
-- required fields: string or number, single quoted
-- returns: true - if input is numeric, false otherwise
--
DECLARE
inputText ALIAS FOR $1;
tempChar text;
isNumeric boolean;
BEGIN
isNumeric = true;
FOR i IN 1..length(inputText) LOOP
tempChar := substr(inputText, i, 1);
IF tempChar ~ ''[0-9]'' THEN
-- do nothing
ELSE
return FALSE;
END IF;
END LOOP;
return isNumeric;
END;
' LANGUAGE 'plpgsql';

Source: http://www.thescripts.com/forum/thread174228.html
0
 

Author Comment

by:gmanpert
ID: 19566827
Yeah, and I figured out these work as well...

select '1234GG567' ~ '^[0-9]+$';
select '1234567' ~ '^[0-9]+$';

if v_var_data ~ '^[0-9]{9}$'
 then
  ... good ..
 else
  .. bad ..
 
 end if;
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many developers have database experience, but are new to PostgreSQL. It has some truly inspiring capabilities. I have several years' experience with Microsoft's SQL Server. When I began working with MySQL, I wanted a quick-reference to MySQL (htt…
By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
Steps to create a PostgreSQL RDS instance in the Amazon cloud. We will cover some of the default settings and show how to connect to the instance once it is up and running.
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Suggested Courses

569 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question