dbTalk Databases Forums  

Assessing data type

comp.databases.pick comp.databases.pick


Discuss Assessing data type in the comp.databases.pick forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Brian Speirs
 
Posts: n/a

Default Assessing data type - 06-14-2005 , 06:53 PM






Hi,

I want to assess the data type of a string into 3 basic categories:
numeric, date, and text.

I wrote a user defined function to do this (test program shown below)
which works OK in most circumstances. However, when it is passed the
string "1/2 SCOTTS" it assesses the string to be of type DATE - ie 1/2/05.

Does anyone have any ideas for a test for a date string that doesn't
fall into this trap?

Platform = mvBASE.

Thanks,

Brian Speirs.

Program DT.TEST

FUNCTION DATA.TYPE(DATA.STRING)
DT = "T"; * Text (default)
IF DATA.STRING NE "" THEN
IF NUM(DATA.STRING) THEN DT = "N"; * Numeric
IF DT = "T" THEN
IF ICONV(DATA.STRING, 'D') NE "" THEN DT = "D"
END
END
RETURNING DT

LOOP
CRT "Enter data: ":
INPUT TESTDATA:
UNTIL TESTDATA = "X" OR TESTDATA = "x" DO
DATATYPE = @DATA.TYPE(TESTDATA)
CRT " Data type = "ATATYPE
REPEAT
STOP
END

Quote:
RUN BP DT.TEST
Enter data: ?HELLO Data type = T

Enter data: ?125 Data type = N

Enter data: ?125.123 Data type = N

Enter data: ?31/5/05 Data type = D

Enter data: ?1/2 Data type = D

Enter data: ?1/2 DOZ Data type = D

Enter data: ?X

--
************************************************** *
Brian Speirs
h: (04) 479 9032 c: (021) 265 5906
e: bss59REMOVETHIS (AT) paradise (DOT) net.nz


Reply With Quote
  #2  
Old   
Dave Weaver
 
Posts: n/a

Default Re: Assessing data type - 06-14-2005 , 07:52 PM






Brian,

I don't have mvBASE up and running so I needed to do this with a
subroutine instead of a function. The following code may help solve
your problem. Note the use of the OCONV for MCA which has to be Text.

*Program DATATYPE
* 06/17/05 Dave Weaver, Weaver Consulting
*==============================================
PROMPT ''
LOOP
CRT "Enter data: ":
INPUT TESTDATA:
UNTIL TESTDATA = "X" OR TESTDATA = "x" DO
GOSUB 100
CRT " Data type = "ATATYPE
REPEAT
STOP
*
100 * Develop the DATATYPE
BEGIN CASE
CASE TESTDATA = ''
DATATYPE = 'T'
CASE INDEX(TESTDATA,' ',1)
DATATYPE = 'T'
CASE OCONV(TESTDATA,'MCA') # ''
DATATYPE = 'T'
CASE NUM(TESTDATA)
DATATYPE = 'N'
CASE ICONV(TESTDATA,'D') # ''
DATATYPE = 'D'
CASE 1
DATATYPE = 'ERR'
END CASE
RETURN
*
END


Brian Speirs wrote:
Quote:
Hi,

I want to assess the data type of a string into 3 basic categories:
numeric, date, and text.

I wrote a user defined function to do this (test program shown below)
which works OK in most circumstances. However, when it is passed the
string "1/2 SCOTTS" it assesses the string to be of type DATE - ie 1/2/05.

Does anyone have any ideas for a test for a date string that doesn't
fall into this trap?

Platform = mvBASE.

Thanks,

Brian Speirs.

Program DT.TEST

FUNCTION DATA.TYPE(DATA.STRING)
DT = "T"; * Text (default)
IF DATA.STRING NE "" THEN
IF NUM(DATA.STRING) THEN DT = "N"; * Numeric
IF DT = "T" THEN
IF ICONV(DATA.STRING, 'D') NE "" THEN DT = "D"
END
END
RETURNING DT

LOOP
CRT "Enter data: ":
INPUT TESTDATA:
UNTIL TESTDATA = "X" OR TESTDATA = "x" DO
DATATYPE = @DATA.TYPE(TESTDATA)
CRT " Data type = "ATATYPE
REPEAT
STOP
END

RUN BP DT.TEST

Enter data: ?HELLO Data type = T

Enter data: ?125 Data type = N

Enter data: ?125.123 Data type = N

Enter data: ?31/5/05 Data type = D

Enter data: ?1/2 Data type = D

Enter data: ?1/2 DOZ Data type = D

Enter data: ?X

--
************************************************** *
Brian Speirs
h: (04) 479 9032 c: (021) 265 5906
e: bss59REMOVETHIS (AT) paradise (DOT) net.nz


Reply With Quote
  #3  
Old   
Luke Webber
 
Posts: n/a

Default Re: Assessing data type - 06-14-2005 , 08:03 PM



I see some problems here. Take the following sample run...

Enter data: 1/1*!$# Data type = D
Enter data: Data type = T
Enter data: *!@ Data type = ERR

Hmmm. <g>

Luke

Dave Weaver wrote:
Quote:
Brian,

I don't have mvBASE up and running so I needed to do this with a
subroutine instead of a function. The following code may help solve
your problem. Note the use of the OCONV for MCA which has to be Text.

*Program DATATYPE
* 06/17/05 Dave Weaver, Weaver Consulting
*==============================================
PROMPT ''
LOOP
CRT "Enter data: ":
INPUT TESTDATA:
UNTIL TESTDATA = "X" OR TESTDATA = "x" DO
GOSUB 100
CRT " Data type = "ATATYPE
REPEAT
STOP
*
100 * Develop the DATATYPE
BEGIN CASE
CASE TESTDATA = ''
DATATYPE = 'T'
CASE INDEX(TESTDATA,' ',1)
DATATYPE = 'T'
CASE OCONV(TESTDATA,'MCA') # ''
DATATYPE = 'T'
CASE NUM(TESTDATA)
DATATYPE = 'N'
CASE ICONV(TESTDATA,'D') # ''
DATATYPE = 'D'
CASE 1
DATATYPE = 'ERR'
END CASE
RETURN
*
END


Brian Speirs wrote:

Hi,

I want to assess the data type of a string into 3 basic categories:
numeric, date, and text.

I wrote a user defined function to do this (test program shown below)
which works OK in most circumstances. However, when it is passed the
string "1/2 SCOTTS" it assesses the string to be of type DATE - ie 1/2/05.

Does anyone have any ideas for a test for a date string that doesn't
fall into this trap?

Platform = mvBASE.

Thanks,

Brian Speirs.

Program DT.TEST

FUNCTION DATA.TYPE(DATA.STRING)
DT = "T"; * Text (default)
IF DATA.STRING NE "" THEN
IF NUM(DATA.STRING) THEN DT = "N"; * Numeric
IF DT = "T" THEN
IF ICONV(DATA.STRING, 'D') NE "" THEN DT = "D"
END
END
RETURNING DT

LOOP
CRT "Enter data: ":
INPUT TESTDATA:
UNTIL TESTDATA = "X" OR TESTDATA = "x" DO
DATATYPE = @DATA.TYPE(TESTDATA)
CRT " Data type = "ATATYPE
REPEAT
STOP
END

RUN BP DT.TEST

Enter data: ?HELLO Data type = T

Enter data: ?125 Data type = N

Enter data: ?125.123 Data type = N

Enter data: ?31/5/05 Data type = D

Enter data: ?1/2 Data type = D

Enter data: ?1/2 DOZ Data type = D

Enter data: ?X

--
************************************************** *
Brian Speirs
h: (04) 479 9032 c: (021) 265 5906
e: bss59REMOVETHIS (AT) paradise (DOT) net.nz



Reply With Quote
  #4  
Old   
Dave Weaver
 
Posts: n/a

Default Re: Assessing data type - 06-14-2005 , 08:15 PM



Good eye, Luke!
I also found some problems with my original code.
THE REVISED VERSION FOLLOWS.
Note that ICONV of something not null is NOT a valid date test!
Once ICONVed, the ICONVed value must be OCONVed to NOT NULL to be a
valid date!
Note this code:

*Program DATATYPE
* 06/17/05 Dave Weaver, Weaver Consulting
*==============================================
PROMPT ''
LOOP
CRT "Enter data: ":
INPUT TESTDATA:
UNTIL TESTDATA = "X" OR TESTDATA = "x" DO
GOSUB 100
CRT " Data type = "ATATYPE
REPEAT
STOP
*
100 * Develop the DATATYPE
DATETEST = ICONV(TESTDATA,'D') ; DATETEST = OCONV(DATETEST,"D2/")
BEGIN CASE
CASE TESTDATA = '' ;* NULL is Text
DATATYPE = 'T'
CASE INDEX(TESTDATA,' ',1) ;* At least one space
DATATYPE = 'T'
CASE OCONV(TESTDATA,'MCA') # '' ;* Alpha is not null
DATATYPE = 'T'
CASE NUM(TESTDATA) ;* Is numeric
DATATYPE = 'N'
CASE DATETEST # '' ;* OCONV DATE is not null
DATATYPE = 'D'
CASE 1
DATATYPE = 'ERR'
END CASE
RETURN
*
END


Luke Webber wrote:
Quote:
I see some problems here. Take the following sample run...

Enter data: 1/1*!$# Data type = D
Enter data: Data type = T
Enter data: *!@ Data type = ERR

Hmmm. <g

Luke

Dave Weaver wrote:
Brian,

I don't have mvBASE up and running so I needed to do this with a
subroutine instead of a function. The following code may help solve
your problem. Note the use of the OCONV for MCA which has to be Text.

*Program DATATYPE
* 06/17/05 Dave Weaver, Weaver Consulting
*==============================================
PROMPT ''
LOOP
CRT "Enter data: ":
INPUT TESTDATA:
UNTIL TESTDATA = "X" OR TESTDATA = "x" DO
GOSUB 100
CRT " Data type = "ATATYPE
REPEAT
STOP
*
100 * Develop the DATATYPE
BEGIN CASE
CASE TESTDATA = ''
DATATYPE = 'T'
CASE INDEX(TESTDATA,' ',1)
DATATYPE = 'T'
CASE OCONV(TESTDATA,'MCA') # ''
DATATYPE = 'T'
CASE NUM(TESTDATA)
DATATYPE = 'N'
CASE ICONV(TESTDATA,'D') # ''
DATATYPE = 'D'
CASE 1
DATATYPE = 'ERR'
END CASE
RETURN
*
END


Brian Speirs wrote:

Hi,

I want to assess the data type of a string into 3 basic categories:
numeric, date, and text.

I wrote a user defined function to do this (test program shown below)
which works OK in most circumstances. However, when it is passed the
string "1/2 SCOTTS" it assesses the string to be of type DATE - ie 1/2/05.

Does anyone have any ideas for a test for a date string that doesn't
fall into this trap?

Platform = mvBASE.

Thanks,

Brian Speirs.

Program DT.TEST

FUNCTION DATA.TYPE(DATA.STRING)
DT = "T"; * Text (default)
IF DATA.STRING NE "" THEN
IF NUM(DATA.STRING) THEN DT = "N"; * Numeric
IF DT = "T" THEN
IF ICONV(DATA.STRING, 'D') NE "" THEN DT = "D"
END
END
RETURNING DT

LOOP
CRT "Enter data: ":
INPUT TESTDATA:
UNTIL TESTDATA = "X" OR TESTDATA = "x" DO
DATATYPE = @DATA.TYPE(TESTDATA)
CRT " Data type = "ATATYPE
REPEAT
STOP
END

RUN BP DT.TEST

Enter data: ?HELLO Data type = T

Enter data: ?125 Data type = N

Enter data: ?125.123 Data type = N

Enter data: ?31/5/05 Data type = D

Enter data: ?1/2 Data type = D

Enter data: ?1/2 DOZ Data type = D

Enter data: ?X

--
************************************************** *
Brian Speirs
h: (04) 479 9032 c: (021) 265 5906
e: bss59REMOVETHIS (AT) paradise (DOT) net.nz




Reply With Quote
  #5  
Old   
Dave Weaver
 
Posts: n/a

Default Re: Assessing data type - 06-14-2005 , 08:22 PM



One more change and I think we have it!

In the CASE 1 section, change 'ERR' to 'T' for text. Now, I can't make
it fail!

Dave Weaver

Dave Weaver wrote:
Quote:
Good eye, Luke!
I also found some problems with my original code.
THE REVISED VERSION FOLLOWS.
Note that ICONV of something not null is NOT a valid date test!
Once ICONVed, the ICONVed value must be OCONVed to NOT NULL to be a
valid date!
Note this code:

*Program DATATYPE
* 06/17/05 Dave Weaver, Weaver Consulting
*==============================================
PROMPT ''
LOOP
CRT "Enter data: ":
INPUT TESTDATA:
UNTIL TESTDATA = "X" OR TESTDATA = "x" DO
GOSUB 100
CRT " Data type = "ATATYPE
REPEAT
STOP
*
100 * Develop the DATATYPE
DATETEST = ICONV(TESTDATA,'D') ; DATETEST = OCONV(DATETEST,"D2/")
BEGIN CASE
CASE TESTDATA = '' ;* NULL is Text
DATATYPE = 'T'
CASE INDEX(TESTDATA,' ',1) ;* At least one space
DATATYPE = 'T'
CASE OCONV(TESTDATA,'MCA') # '' ;* Alpha is not null
DATATYPE = 'T'
CASE NUM(TESTDATA) ;* Is numeric
DATATYPE = 'N'
CASE DATETEST # '' ;* OCONV DATE is not null
DATATYPE = 'D'
CASE 1
DATATYPE = 'ERR'
END CASE
RETURN
*
END


Luke Webber wrote:
I see some problems here. Take the following sample run...

Enter data: 1/1*!$# Data type = D
Enter data: Data type = T
Enter data: *!@ Data type = ERR

Hmmm. <g

Luke

Dave Weaver wrote:
Brian,

I don't have mvBASE up and running so I needed to do this with a
subroutine instead of a function. The following code may help solve
your problem. Note the use of the OCONV for MCA which has to be Text.

*Program DATATYPE
* 06/17/05 Dave Weaver, Weaver Consulting
*==============================================
PROMPT ''
LOOP
CRT "Enter data: ":
INPUT TESTDATA:
UNTIL TESTDATA = "X" OR TESTDATA = "x" DO
GOSUB 100
CRT " Data type = "ATATYPE
REPEAT
STOP
*
100 * Develop the DATATYPE
BEGIN CASE
CASE TESTDATA = ''
DATATYPE = 'T'
CASE INDEX(TESTDATA,' ',1)
DATATYPE = 'T'
CASE OCONV(TESTDATA,'MCA') # ''
DATATYPE = 'T'
CASE NUM(TESTDATA)
DATATYPE = 'N'
CASE ICONV(TESTDATA,'D') # ''
DATATYPE = 'D'
CASE 1
DATATYPE = 'ERR'
END CASE
RETURN
*
END


Brian Speirs wrote:

Hi,

I want to assess the data type of a string into 3 basic categories:
numeric, date, and text.

I wrote a user defined function to do this (test program shown below)
which works OK in most circumstances. However, when it is passed the
string "1/2 SCOTTS" it assesses the string to be of type DATE - ie 1/2/05.

Does anyone have any ideas for a test for a date string that doesn't
fall into this trap?

Platform = mvBASE.

Thanks,

Brian Speirs.

Program DT.TEST

FUNCTION DATA.TYPE(DATA.STRING)
DT = "T"; * Text (default)
IF DATA.STRING NE "" THEN
IF NUM(DATA.STRING) THEN DT = "N"; * Numeric
IF DT = "T" THEN
IF ICONV(DATA.STRING, 'D') NE "" THEN DT = "D"
END
END
RETURNING DT

LOOP
CRT "Enter data: ":
INPUT TESTDATA:
UNTIL TESTDATA = "X" OR TESTDATA = "x" DO
DATATYPE = @DATA.TYPE(TESTDATA)
CRT " Data type = "ATATYPE
REPEAT
STOP
END

RUN BP DT.TEST

Enter data: ?HELLO Data type = T

Enter data: ?125 Data type = N

Enter data: ?125.123 Data type = N

Enter data: ?31/5/05 Data type = D

Enter data: ?1/2 Data type = D

Enter data: ?1/2 DOZ Data type = D

Enter data: ?X

--
************************************************** *
Brian Speirs
h: (04) 479 9032 c: (021) 265 5906
e: bss59REMOVETHIS (AT) paradise (DOT) net.nz




Reply With Quote
  #6  
Old   
Brian Speirs
 
Posts: n/a

Default Re: Assessing data type - 06-14-2005 , 09:53 PM



Thanks for this.

This solves my immediate problem of making sure that text is correctly
displayed in an html report.

But I did note that strings such as 1!2 or 1!2!05 are counted as dates.
And that 01 JUN 2005 is counted as a string!

I'm sure that whatever rules we come up with, there will be cases when
dates and strings will be confused!

Cheers,

Brian


Dave Weaver wrote:
Quote:
One more change and I think we have it!

In the CASE 1 section, change 'ERR' to 'T' for text. Now, I can't make
it fail!

Dave Weaver

Dave Weaver wrote:

Good eye, Luke!
I also found some problems with my original code.
THE REVISED VERSION FOLLOWS.
Note that ICONV of something not null is NOT a valid date test!
Once ICONVed, the ICONVed value must be OCONVed to NOT NULL to be a
valid date!
Note this code:

*Program DATATYPE
* 06/17/05 Dave Weaver, Weaver Consulting
*==============================================
PROMPT ''
LOOP
CRT "Enter data: ":
INPUT TESTDATA:
UNTIL TESTDATA = "X" OR TESTDATA = "x" DO
GOSUB 100
CRT " Data type = "ATATYPE
REPEAT
STOP
*
100 * Develop the DATATYPE
DATETEST = ICONV(TESTDATA,'D') ; DATETEST = OCONV(DATETEST,"D2/")
BEGIN CASE
CASE TESTDATA = '' ;* NULL is Text
DATATYPE = 'T'
CASE INDEX(TESTDATA,' ',1) ;* At least one space
DATATYPE = 'T'
CASE OCONV(TESTDATA,'MCA') # '' ;* Alpha is not null
DATATYPE = 'T'
CASE NUM(TESTDATA) ;* Is numeric
DATATYPE = 'N'
CASE DATETEST # '' ;* OCONV DATE is not null
DATATYPE = 'D'
CASE 1
DATATYPE = 'ERR'
END CASE
RETURN
*
END


Luke Webber wrote:

I see some problems here. Take the following sample run...

Enter data: 1/1*!$# Data type = D
Enter data: Data type = T
Enter data: *!@ Data type = ERR

Hmmm. <g

Luke

Dave Weaver wrote:

Brian,

I don't have mvBASE up and running so I needed to do this with a
subroutine instead of a function. The following code may help solve
your problem. Note the use of the OCONV for MCA which has to be Text.

*Program DATATYPE
* 06/17/05 Dave Weaver, Weaver Consulting
*==============================================
PROMPT ''
LOOP
CRT "Enter data: ":
INPUT TESTDATA:
UNTIL TESTDATA = "X" OR TESTDATA = "x" DO
GOSUB 100
CRT " Data type = "ATATYPE
REPEAT
STOP
*
100 * Develop the DATATYPE
BEGIN CASE
CASE TESTDATA = ''
DATATYPE = 'T'
CASE INDEX(TESTDATA,' ',1)
DATATYPE = 'T'
CASE OCONV(TESTDATA,'MCA') # ''
DATATYPE = 'T'
CASE NUM(TESTDATA)
DATATYPE = 'N'
CASE ICONV(TESTDATA,'D') # ''
DATATYPE = 'D'
CASE 1
DATATYPE = 'ERR'
END CASE
RETURN
*
END


Brian Speirs wrote:


Hi,

I want to assess the data type of a string into 3 basic categories:
numeric, date, and text.

I wrote a user defined function to do this (test program shown below)
which works OK in most circumstances. However, when it is passed the
string "1/2 SCOTTS" it assesses the string to be of type DATE - ie 1/2/05.

Does anyone have any ideas for a test for a date string that doesn't
fall into this trap?

Platform = mvBASE.

Thanks,

Brian Speirs.

Program DT.TEST

FUNCTION DATA.TYPE(DATA.STRING)
DT = "T"; * Text (default)
IF DATA.STRING NE "" THEN
IF NUM(DATA.STRING) THEN DT = "N"; * Numeric
IF DT = "T" THEN
IF ICONV(DATA.STRING, 'D') NE "" THEN DT = "D"
END
END
RETURNING DT

LOOP
CRT "Enter data: ":
INPUT TESTDATA:
UNTIL TESTDATA = "X" OR TESTDATA = "x" DO
DATATYPE = @DATA.TYPE(TESTDATA)
CRT " Data type = "ATATYPE
REPEAT
STOP
END


RUN BP DT.TEST

Enter data: ?HELLO Data type = T

Enter data: ?125 Data type = N

Enter data: ?125.123 Data type = N

Enter data: ?31/5/05 Data type = D

Enter data: ?1/2 Data type = D

Enter data: ?1/2 DOZ Data type = D

Enter data: ?X

--
************************************************** *
Brian Speirs
h: (04) 479 9032 c: (021) 265 5906
e: bss59REMOVETHIS (AT) paradise (DOT) net.nz



--
************************************************** *
Brian Speirs
h: (04) 479 9032 c: (021) 265 5906
e: bss59REMOVETHIS (AT) paradise (DOT) net.nz


Reply With Quote
  #7  
Old   
Bruce Nichol
 
Posts: n/a

Default Re: Assessing data type - 06-15-2005 , 12:00 AM



Goo'day, Briian,

I've got no way of testing this under your rules, but I'd be very
tempted to change your"ICONV" line to

IF OCONV(ICONV(DATA,'D'),'D') = DATA THEN DT = 'D'

in other words convert in and then convert that out and if that's the
same as the original you're laughing.....


On Wed, 15 Jun 2005 11:53:56 +1200, Brian Speirs
<bss59REMOVE_THIS (AT) paradise (DOT) net.nz> wrote:

Quote:
Hi,

I want to assess the data type of a string into 3 basic categories:
numeric, date, and text.

I wrote a user defined function to do this (test program shown below)
which works OK in most circumstances. However, when it is passed the
string "1/2 SCOTTS" it assesses the string to be of type DATE - ie 1/2/05.

Does anyone have any ideas for a test for a date string that doesn't
fall into this trap?

Platform = mvBASE.

Thanks,

Brian Speirs.

Program DT.TEST

FUNCTION DATA.TYPE(DATA.STRING)
DT = "T"; * Text (default)
IF DATA.STRING NE "" THEN
IF NUM(DATA.STRING) THEN DT = "N"; * Numeric
IF DT = "T" THEN
IF ICONV(DATA.STRING, 'D') NE "" THEN DT = "D"
END
END
RETURNING DT

LOOP
CRT "Enter data: ":
INPUT TESTDATA:
UNTIL TESTDATA = "X" OR TESTDATA = "x" DO
DATATYPE = @DATA.TYPE(TESTDATA)
CRT " Data type = "ATATYPE
REPEAT
STOP
END

RUN BP DT.TEST

Enter data: ?HELLO Data type = T

Enter data: ?125 Data type = N

Enter data: ?125.123 Data type = N

Enter data: ?31/5/05 Data type = D

Enter data: ?1/2 Data type = D

Enter data: ?1/2 DOZ Data type = D

Enter data: ?X
Regards,

Bruce Nichol
Talon Computer Services
ALBURY NSW Australia

http://www.taloncs.com.au

If it ain't broke, fix it until it is....


Reply With Quote
  #8  
Old   
iakovos@gmail.com
 
Posts: n/a

Default Re: Assessing data type - 06-15-2005 , 12:30 AM



What I would do...
TESTDATA = OCONV(OCONV(INDATA,'MC/A'),'MC/N')
In this way you will end up with only the delimiters, invalid chars
etc.
If you check the length of what's in TESTDATA then and its other than 1
character long, it will mean that its a string, not a date...

try it, MC/ got me out of trouble often

jak
Bruce Nichol wrote:
Quote:
Goo'day, Briian,

I've got no way of testing this under your rules, but I'd be very
tempted to change your"ICONV" line to

IF OCONV(ICONV(DATA,'D'),'D') = DATA THEN DT = 'D'

in other words convert in and then convert that out and if that's the
same as the original you're laughing.....


On Wed, 15 Jun 2005 11:53:56 +1200, Brian Speirs
bss59REMOVE_THIS (AT) paradise (DOT) net.nz> wrote:

Hi,

I want to assess the data type of a string into 3 basic categories:
numeric, date, and text.

I wrote a user defined function to do this (test program shown below)
which works OK in most circumstances. However, when it is passed the
string "1/2 SCOTTS" it assesses the string to be of type DATE - ie 1/2/05.

Does anyone have any ideas for a test for a date string that doesn't
fall into this trap?

Platform = mvBASE.

Thanks,

Brian Speirs.

Program DT.TEST

FUNCTION DATA.TYPE(DATA.STRING)
DT = "T"; * Text (default)
IF DATA.STRING NE "" THEN
IF NUM(DATA.STRING) THEN DT = "N"; * Numeric
IF DT = "T" THEN
IF ICONV(DATA.STRING, 'D') NE "" THEN DT = "D"
END
END
RETURNING DT

LOOP
CRT "Enter data: ":
INPUT TESTDATA:
UNTIL TESTDATA = "X" OR TESTDATA = "x" DO
DATATYPE = @DATA.TYPE(TESTDATA)
CRT " Data type = "ATATYPE
REPEAT
STOP
END

RUN BP DT.TEST

Enter data: ?HELLO Data type = T

Enter data: ?125 Data type = N

Enter data: ?125.123 Data type = N

Enter data: ?31/5/05 Data type = D

Enter data: ?1/2 Data type = D

Enter data: ?1/2 DOZ Data type = D

Enter data: ?X

Regards,

Bruce Nichol
Talon Computer Services
ALBURY NSW Australia

http://www.taloncs.com.au

If it ain't broke, fix it until it is....


Reply With Quote
  #9  
Old   
murthi
 
Posts: n/a

Default Re: Assessing data type - 06-15-2005 , 10:01 AM



Quote:
I'm sure that whatever rules we come up with, there will be cases when
dates and strings will be confused!

no:
1- check for null first- it's a T (it will pass NUM); you may want to check
for only "+" and "-" which some Picks pass as NUM..are they T or N?
2- number check next;
3-Since Date is a special type of string, the date check MUST come next, not
after checking for blanks etc.
4- all other checks

ICONV will accept any delimiter to separate data parts, so 1$2*05 is a valid
date.

If you want to cull only / dates, you would check for only that:

CASE testdata = ''; type = "T"
CASE NUM(testdata); type = 'N'
CASE ICONV(testdata,'D') # '' ;* this is sufficient check for date, but
limit it:
IF testdata MATCH '0N/0N/0N' OR testdata MATCH '0N 0A 0N' THEN
type = "D"
END ELSE GOSUB otherchecks
CASE 1; GOSUB otherchecks
END CASE

testing on uv 10: 1/1/05, 1 jun 05 1 Jun 05 all return D; 1/1, 1 Junx 05
return T.

Chandru

"Brian Speirs" <bss59REMOVE_THIS (AT) paradise (DOT) net.nz> wrote

Quote:
Thanks for this.

This solves my immediate problem of making sure that text is correctly
displayed in an html report.

But I did note that strings such as 1!2 or 1!2!05 are counted as dates.
And that 01 JUN 2005 is counted as a string!

Cheers,

Brian


Dave Weaver wrote:
One more change and I think we have it!

In the CASE 1 section, change 'ERR' to 'T' for text. Now, I can't make
it fail!

Dave Weaver

Dave Weaver wrote:

Good eye, Luke!
I also found some problems with my original code.
THE REVISED VERSION FOLLOWS.
Note that ICONV of something not null is NOT a valid date test!
Once ICONVed, the ICONVed value must be OCONVed to NOT NULL to be a
valid date!
Note this code:

*Program DATATYPE
* 06/17/05 Dave Weaver, Weaver Consulting
*==============================================
PROMPT ''
LOOP
CRT "Enter data: ":
INPUT TESTDATA:
UNTIL TESTDATA = "X" OR TESTDATA = "x" DO
GOSUB 100
CRT " Data type = "ATATYPE
REPEAT
STOP
*
100 * Develop the DATATYPE
DATETEST = ICONV(TESTDATA,'D') ; DATETEST = OCONV(DATETEST,"D2/")
BEGIN CASE
CASE TESTDATA = '' ;* NULL is Text
DATATYPE = 'T'
CASE INDEX(TESTDATA,' ',1) ;* At least one space
DATATYPE = 'T'
CASE OCONV(TESTDATA,'MCA') # '' ;* Alpha is not null
DATATYPE = 'T'
CASE NUM(TESTDATA) ;* Is numeric
DATATYPE = 'N'
CASE DATETEST # '' ;* OCONV DATE is not null
DATATYPE = 'D'
CASE 1
DATATYPE = 'ERR'
END CASE
RETURN
*
END


Luke Webber wrote:

I see some problems here. Take the following sample run...

Enter data: 1/1*!$# Data type = D
Enter data: Data type = T
Enter data: *!@ Data type = ERR

Hmmm. <g

Luke

Dave Weaver wrote:

Brian,

I don't have mvBASE up and running so I needed to do this with a
subroutine instead of a function. The following code may help solve
your problem. Note the use of the OCONV for MCA which has to be Text.

*Program DATATYPE
* 06/17/05 Dave Weaver, Weaver Consulting
*==============================================
PROMPT ''
LOOP
CRT "Enter data: ":
INPUT TESTDATA:
UNTIL TESTDATA = "X" OR TESTDATA = "x" DO
GOSUB 100
CRT " Data type = "ATATYPE
REPEAT
STOP
*
100 * Develop the DATATYPE
BEGIN CASE
CASE TESTDATA = ''
DATATYPE = 'T'
CASE INDEX(TESTDATA,' ',1)
DATATYPE = 'T'
CASE OCONV(TESTDATA,'MCA') # ''
DATATYPE = 'T'
CASE NUM(TESTDATA)
DATATYPE = 'N'
CASE ICONV(TESTDATA,'D') # ''
DATATYPE = 'D'
CASE 1
DATATYPE = 'ERR'
END CASE
RETURN
*
END


Brian Speirs wrote:


Hi,

I want to assess the data type of a string into 3 basic categories:
numeric, date, and text.

I wrote a user defined function to do this (test program shown below)
which works OK in most circumstances. However, when it is passed the
string "1/2 SCOTTS" it assesses the string to be of type DATE - ie
1/2/05.

Does anyone have any ideas for a test for a date string that doesn't
fall into this trap?

Platform = mvBASE.

Thanks,

Brian Speirs.

Program DT.TEST

FUNCTION DATA.TYPE(DATA.STRING)
DT = "T"; * Text (default)
IF DATA.STRING NE "" THEN
IF NUM(DATA.STRING) THEN DT = "N"; * Numeric
IF DT = "T" THEN
IF ICONV(DATA.STRING, 'D') NE "" THEN DT = "D"
END
END
RETURNING DT

LOOP
CRT "Enter data: ":
INPUT TESTDATA:
UNTIL TESTDATA = "X" OR TESTDATA = "x" DO
DATATYPE = @DATA.TYPE(TESTDATA)
CRT " Data type = "ATATYPE
REPEAT
STOP
END


RUN BP DT.TEST

Enter data: ?HELLO Data type = T

Enter data: ?125 Data type = N

Enter data: ?125.123 Data type = N

Enter data: ?31/5/05 Data type = D

Enter data: ?1/2 Data type = D

Enter data: ?1/2 DOZ Data type = D

Enter data: ?X

--
************************************************** *
Brian Speirs
h: (04) 479 9032 c: (021) 265 5906
e: bss59REMOVETHIS (AT) paradise (DOT) net.nz




--
************************************************** *
Brian Speirs
h: (04) 479 9032 c: (021) 265 5906
e: bss59REMOVETHIS (AT) paradise (DOT) net.nz



Reply With Quote
  #10  
Old   
Dave Weaver
 
Posts: n/a

Default Re: Assessing data type - 06-15-2005 , 03:21 PM



Maybe, just maybe, after the dust has settled on this thread, I may
attempt a comprehensive routine that cannot fail to report the correct
data type.

What I have in mind, particularly for my own use are these data types:
X = Null, unknown type.
S = String. Or one could use A for Alpha.
N = Numeric
D = Date
T = Time

Of course, one must consider that the source data is in EXTERNAL
format. Other considerations must be taken into account:
0000123 does test as numeric, but
space space 123 does not test as numeric.
Dates such as 15 JUN 05 do not ICONV to an internal date value! In
fact, ICONV in some instances gives some interesting surprises.
Data typing Time should be an interesting problem.

Dave Weaver, Weaver Consulting

iakovos (AT) gmail (DOT) com wrote:
Quote:
What I would do...
TESTDATA = OCONV(OCONV(INDATA,'MC/A'),'MC/N')
In this way you will end up with only the delimiters, invalid chars
etc.
If you check the length of what's in TESTDATA then and its other than 1
character long, it will mean that its a string, not a date...

try it, MC/ got me out of trouble often

jak
Bruce Nichol wrote:
Goo'day, Briian,

I've got no way of testing this under your rules, but I'd be very
tempted to change your"ICONV" line to

IF OCONV(ICONV(DATA,'D'),'D') = DATA THEN DT = 'D'

in other words convert in and then convert that out and if that's the
same as the original you're laughing.....


On Wed, 15 Jun 2005 11:53:56 +1200, Brian Speirs
bss59REMOVE_THIS (AT) paradise (DOT) net.nz> wrote:

Hi,

I want to assess the data type of a string into 3 basic categories:
numeric, date, and text.

I wrote a user defined function to do this (test program shown below)
which works OK in most circumstances. However, when it is passed the
string "1/2 SCOTTS" it assesses the string to be of type DATE - ie 1/2/05.

Does anyone have any ideas for a test for a date string that doesn't
fall into this trap?

Platform = mvBASE.

Thanks,

Brian Speirs.

Program DT.TEST

FUNCTION DATA.TYPE(DATA.STRING)
DT = "T"; * Text (default)
IF DATA.STRING NE "" THEN
IF NUM(DATA.STRING) THEN DT = "N"; * Numeric
IF DT = "T" THEN
IF ICONV(DATA.STRING, 'D') NE "" THEN DT = "D"
END
END
RETURNING DT

LOOP
CRT "Enter data: ":
INPUT TESTDATA:
UNTIL TESTDATA = "X" OR TESTDATA = "x" DO
DATATYPE = @DATA.TYPE(TESTDATA)
CRT " Data type = "ATATYPE
REPEAT
STOP
END

RUN BP DT.TEST

Enter data: ?HELLO Data type = T

Enter data: ?125 Data type = N

Enter data: ?125.123 Data type = N

Enter data: ?31/5/05 Data type = D

Enter data: ?1/2 Data type = D

Enter data: ?1/2 DOZ Data type = D

Enter data: ?X

Regards,

Bruce Nichol
Talon Computer Services
ALBURY NSW Australia

http://www.taloncs.com.au

If it ain't broke, fix it until it is....


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.