How can I validate a numeric data from database against this format
YYYYMMDD?

I have this data in database 19990120 and I'd like to make sure that the
four digit represents the year and the preceding 2 digits represents the
month and .....

Thanks

Re: YYYYMMDD by Bob

Bob
Tue Mar 15 14:02:14 CST 2005

RayAll wrote:
> How can I validate a numeric data from database against this format
> YYYYMMDD?
>
> I have this data in database 19990120 and I'd like to make sure that
> the four digit represents the year and the preceding 2 digits
> represents the month and .....
>
> Thanks

function LongIsDate(pValue)
dim yr,mth,d, tstDate
LongIsDate = true
yr=left(pValue,4)
mth=mid(pValue,5,2)
d=right(pValue,2)
on error resume next
tstDate=DateSerial(cint(yr), cint(mth), cint(d))
if err <> 0 then
LongIsDate = false
else
'look up dateserial in documentation to see why this is needed
if year(tstDate) <> cint(yr) OR month(tstDate) <> cint(mth) _
OR day(tstDate) <> cint(d) then
LongIsDate=false
end if
end if
end function

test=LongIsDate(19991345)


Vbscript documentation is available here:
http://www.microsoft.com/downloads/details.aspx?FamilyID=01592c48-207d-4be1-8a76-1c4099d7bbb9&DisplayLang=en


Bob Barrows
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.



Re: YYYYMMDD by RayAll

RayAll
Tue Mar 15 14:13:52 CST 2005

but it dosen't tell you if the date is in proper format or not ,,dose it?

Thanks for your help
"Bob Barrows [MVP]" <reb01501@NOyahoo.SPAMcom> wrote in message
news:uF4WjnZKFHA.2800@TK2MSFTNGP10.phx.gbl...
> RayAll wrote:
>> How can I validate a numeric data from database against this format
>> YYYYMMDD?
>>
>> I have this data in database 19990120 and I'd like to make sure that
>> the four digit represents the year and the preceding 2 digits
>> represents the month and .....
>>
>> Thanks
>
> function LongIsDate(pValue)
> dim yr,mth,d, tstDate
> LongIsDate = true
> yr=left(pValue,4)
> mth=mid(pValue,5,2)
> d=right(pValue,2)
> on error resume next
> tstDate=DateSerial(cint(yr), cint(mth), cint(d))
> if err <> 0 then
> LongIsDate = false
> else
> 'look up dateserial in documentation to see why this is needed
> if year(tstDate) <> cint(yr) OR month(tstDate) <> cint(mth) _
> OR day(tstDate) <> cint(d) then
> LongIsDate=false
> end if
> end if
> end function
>
> test=LongIsDate(19991345)
>
>
> Vbscript documentation is available here:
> http://www.microsoft.com/downloads/details.aspx?FamilyID=01592c48-207d-4be1-8a76-1c4099d7bbb9&DisplayLang=en
>
>
> Bob Barrows
> --
> Microsoft MVP -- ASP/ASP.NET
> Please reply to the newsgroup. The email account listed in my From
> header is my spam trap, so I don't check it very often. You will get a
> quicker response by posting to the newsgroup.
>
>



Re: YYYYMMDD by McKirahan

McKirahan
Tue Mar 15 14:27:56 CST 2005

"RayAll" <RayAll@microsft.com> wrote in message
news:uxVFmUZKFHA.904@tk2msftngp13.phx.gbl...
> How can I validate a numeric data from database against this format
> YYYYMMDD?
>
> I have this data in database 19990120 and I'd like to make sure that the
> four digit represents the year and the preceding 2 digits represents the
> month and .....
>
> Thanks

Here's what I was working on before I saw Bob's solution.

Option Explicit
'*
Const cVBS = "ccyymmdd.vbs"
Const cYMD = "19990120"
'*
If DateOkay(cYMD) Then
WScript.Echo cYMD & " is a valid date."
Else
WScript.Echo cYMD & " is not a valid date."
End If

Function DateOkay(sYMD)
DateOkay = False
'*
If Len(sYMD) <> 8 _
Or Not IsNumeric(sYMD) Then Exit Function
'*
Dim arrYMD(2)
arrYMD(0) = Int(Mid(sYMD,1,4))
arrYMD(1) = Int(Mid(sYMD,5,2))
arrYMD(2) = Int(Mid(sYMD,7,2))
Dim strYMD
strYMD = DateSerial(arrYMD(0), arrYMD(1), arrYMD(2))
'*
If arrYMD(0) <> DatePart("yyyy",strYMD) _
Or arrYMD(1) <> DatePart("m",strYMD) _
Or arrYMD(2) <> DatePart("d",strYMD) Then Exit Function
'*
DateOkay = True
End Function




Re: YYYYMMDD by Bob

Bob
Tue Mar 15 14:31:16 CST 2005

Uuuumm, yes it does. Didn't you try it? It's a function that returns true if
the long can be converted into a date using dateserial. It returns false,
otherwise.

test=LongIsDate(19991345)
msgbox test

HTH,
Bob Barrows

RayAll wrote:
> but it dosen't tell you if the date is in proper format or not ,,dose
> it?
>
> Thanks for your help
> "Bob Barrows [MVP]" <reb01501@NOyahoo.SPAMcom> wrote in message
> news:uF4WjnZKFHA.2800@TK2MSFTNGP10.phx.gbl...
>> RayAll wrote:
>>> How can I validate a numeric data from database against this format
>>> YYYYMMDD?
>>>
>>> I have this data in database 19990120 and I'd like to make sure that
>>> the four digit represents the year and the preceding 2 digits
>>> represents the month and .....
>>>
>>> Thanks
>>
>> function LongIsDate(pValue)
>> dim yr,mth,d, tstDate
>> LongIsDate = true
>> yr=left(pValue,4)
>> mth=mid(pValue,5,2)
>> d=right(pValue,2)
>> on error resume next
>> tstDate=DateSerial(cint(yr), cint(mth), cint(d))
>> if err <> 0 then
>> LongIsDate = false
>> else
>> 'look up dateserial in documentation to see why this is needed
>> if year(tstDate) <> cint(yr) OR month(tstDate) <> cint(mth) _
>> OR day(tstDate) <> cint(d) then
>> LongIsDate=false
>> end if
>> end if
>> end function
>>
>> test=LongIsDate(19991345)
>>
>>
>> Vbscript documentation is available here:
>>
http://www.microsoft.com/downloads/details.aspx?FamilyID=01592c48-207d-4be1-8a76-1c4099d7bbb9&DisplayLang=en
>>
>>
>> Bob Barrows
>> --
>> Microsoft MVP -- ASP/ASP.NET
>> Please reply to the newsgroup. The email account listed in my From
>> header is my spam trap, so I don't check it very often. You will get
>> a quicker response by posting to the newsgroup.

--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.



Re: YYYYMMDD by RayAll

RayAll
Tue Mar 15 14:52:58 CST 2005

I'd like to try it out.I have microsoft scipt debugger installed on my
computer,should I copy that one into a file there? I did it ,I set the
breakpoint but it dosen't stop it at the breakpoint ,should I add speciall
tags in the beginning and the end?

Thanks
"Bob Barrows [MVP]" <reb01501@NOyahoo.SPAMcom> wrote in message
news:enuox3ZKFHA.1308@TK2MSFTNGP15.phx.gbl...
> Uuuumm, yes it does. Didn't you try it? It's a function that returns true
> if
> the long can be converted into a date using dateserial. It returns false,
> otherwise.
>
> test=LongIsDate(19991345)
> msgbox test
>
> HTH,
> Bob Barrows
>
> RayAll wrote:
>> but it dosen't tell you if the date is in proper format or not ,,dose
>> it?
>>
>> Thanks for your help
>> "Bob Barrows [MVP]" <reb01501@NOyahoo.SPAMcom> wrote in message
>> news:uF4WjnZKFHA.2800@TK2MSFTNGP10.phx.gbl...
>>> RayAll wrote:
>>>> How can I validate a numeric data from database against this format
>>>> YYYYMMDD?
>>>>
>>>> I have this data in database 19990120 and I'd like to make sure that
>>>> the four digit represents the year and the preceding 2 digits
>>>> represents the month and .....
>>>>
>>>> Thanks
>>>
>>> function LongIsDate(pValue)
>>> dim yr,mth,d, tstDate
>>> LongIsDate = true
>>> yr=left(pValue,4)
>>> mth=mid(pValue,5,2)
>>> d=right(pValue,2)
>>> on error resume next
>>> tstDate=DateSerial(cint(yr), cint(mth), cint(d))
>>> if err <> 0 then
>>> LongIsDate = false
>>> else
>>> 'look up dateserial in documentation to see why this is needed
>>> if year(tstDate) <> cint(yr) OR month(tstDate) <> cint(mth) _
>>> OR day(tstDate) <> cint(d) then
>>> LongIsDate=false
>>> end if
>>> end if
>>> end function
>>>
>>> test=LongIsDate(19991345)
>>>
>>>
>>> Vbscript documentation is available here:
>>>
> http://www.microsoft.com/downloads/details.aspx?FamilyID=01592c48-207d-4be1-8a76-1c4099d7bbb9&DisplayLang=en
>>>
>>>
>>> Bob Barrows
>>> --
>>> Microsoft MVP -- ASP/ASP.NET
>>> Please reply to the newsgroup. The email account listed in my From
>>> header is my spam trap, so I don't check it very often. You will get
>>> a quicker response by posting to the newsgroup.
>
> --
> Microsoft MVP -- ASP/ASP.NET
> Please reply to the newsgroup. The email account listed in my From
> header is my spam trap, so I don't check it very often. You will get a
> quicker response by posting to the newsgroup.
>
>



Re: YYYYMMDD by McKirahan

McKirahan
Tue Mar 15 14:59:24 CST 2005

"RayAll" <RayAll@microsft.com> wrote in message
news:udhO5DaKFHA.568@TK2MSFTNGP09.phx.gbl...
> I'd like to try it out.I have microsoft scipt debugger installed on my
> computer,should I copy that one into a file there? I did it ,I set the
> breakpoint but it dosen't stop it at the breakpoint ,should I add speciall
> tags in the beginning and the end?

[snip]

"msgbox test" is the key; Bob didn't have it in his first post.



Re: YYYYMMDD by Dr

Dr
Wed Mar 16 11:32:46 CST 2005

JRS: In article <uF4WjnZKFHA.2800@TK2MSFTNGP10.phx.gbl>, dated Tue, 15
Mar 2005 15:02:14, seen in news:microsoft.public.scripting.vbscript, Bob
Barrows [MVP] <reb01501@NOyahoo.SPAMcom> posted :

> 'look up dateserial in documentation to see why this is needed
> if year(tstDate) <> cint(yr) OR month(tstDate) <> cint(mth) _
> OR day(tstDate) <> cint(d) then
> LongIsDate=false

ISTM that careful thought is likely to show that all of that is not
needed, and that any two of the tests suffice. news:c.l.j has agreed
that for the corresponding javascript; probably optimum to test D first
then M.

Your code accepts a string of any number of ones exceeding four; it's
not clear whether the OP wanted, or should have wanted, to cover other
than 8 digits.


Assuming that it should be YYYYMMDD, one can, as shown, determine that
it represents an Actual Gregorian Date. Though the OP does mention four
year digits "and the preceding 2 digits represents the month and ....."

The following appears adequate, though I've not checked the spec of
CDate(string) :

function TrueDate(pValue)
dim y, m, d
y = left(pValue, 4)
m = mid(pValue, 5, 2)
d = right(pValue, 2)
on error resume next
TrueDate = CDate(y & "/" & m & "/" & d)
TrueDate = (err=0)
end function

Variables y m d are not really needed, if the string functions are put
in the CDate string directly. The number of digits is not checked. It
is assumed that, as in javascript, yyyy/mm/dd is understood in all
localisations.

<URL:http://www.merlyn.demon.co.uk/vb-dates.htm>

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

Re: YYYYMMDD by Bob

Bob
Wed Mar 16 05:58:37 CST 2005

Just to add to McKirahan's reply:
You should study McKirahan's example as well as mine. I wrote mine in a
hurry and missed a couple steps that McKirahan's script addresses. My
function, while it will definitely work if fed an 8-digit integer, will
likely return incorrect results if fed an integer with a different number of
digits.

Bob Barrows

RayAll wrote:
> I'd like to try it out.I have microsoft scipt debugger installed on my
> computer,should I copy that one into a file there? I did it ,I set the
> breakpoint but it dosen't stop it at the breakpoint ,should I add
> speciall tags in the beginning and the end?
>
> Thanks
> "Bob Barrows [MVP]" <reb01501@NOyahoo.SPAMcom> wrote in message
> news:enuox3ZKFHA.1308@TK2MSFTNGP15.phx.gbl...
>> Uuuumm, yes it does. Didn't you try it? It's a function that returns
>> true if
>> the long can be converted into a date using dateserial. It returns
>> false, otherwise.
>>
>> test=LongIsDate(19991345)
>> msgbox test
>>
>> HTH,
>> Bob Barrows
>>
>> RayAll wrote:
>>> but it dosen't tell you if the date is in proper format or not
>>> ,,dose it?
>>>
>>> Thanks for your help
>>> "Bob Barrows [MVP]" <reb01501@NOyahoo.SPAMcom> wrote in message
>>> news:uF4WjnZKFHA.2800@TK2MSFTNGP10.phx.gbl...
>>>> RayAll wrote:
>>>>> How can I validate a numeric data from database against this
>>>>> format YYYYMMDD?
>>>>>
>>>>> I have this data in database 19990120 and I'd like to make sure
>>>>> that the four digit represents the year and the preceding 2 digits
>>>>> represents the month and .....
>>>>>
>>>>> Thanks
>>>>
>>>> function LongIsDate(pValue)
>>>> dim yr,mth,d, tstDate
>>>> LongIsDate = true
>>>> yr=left(pValue,4)
>>>> mth=mid(pValue,5,2)
>>>> d=right(pValue,2)
>>>> on error resume next
>>>> tstDate=DateSerial(cint(yr), cint(mth), cint(d))
>>>> if err <> 0 then
>>>> LongIsDate = false
>>>> else
>>>> 'look up dateserial in documentation to see why this is needed
>>>> if year(tstDate) <> cint(yr) OR month(tstDate) <> cint(mth) _
>>>> OR day(tstDate) <> cint(d) then
>>>> LongIsDate=false
>>>> end if
>>>> end if
>>>> end function
>>>>
>>>> test=LongIsDate(19991345)
>>>>
>>>>
>>>> Vbscript documentation is available here:
>>>>
>> http://www.microsoft.com/downloads/details.aspx?FamilyID=01592c48-207d-4be1-8a76-1c4099d7bbb9&DisplayLang=en
>>>>
>>>>
>>>> Bob Barrows
>>>> --
>>>> Microsoft MVP -- ASP/ASP.NET
>>>> Please reply to the newsgroup. The email account listed in my From
>>>> header is my spam trap, so I don't check it very often. You will
>>>> get a quicker response by posting to the newsgroup.
>>
>> --
>> Microsoft MVP -- ASP/ASP.NET
>> Please reply to the newsgroup. The email account listed in my From
>> header is my spam trap, so I don't check it very often. You will get
>> a quicker response by posting to the newsgroup.

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"



Re: YYYYMMDD by RayAll

RayAll
Thu Mar 17 10:53:50 CST 2005

Thanks everyone
"RayAll" <RayAll@microsft.com> wrote in message
news:uxVFmUZKFHA.904@tk2msftngp13.phx.gbl...
> How can I validate a numeric data from database against this format
> YYYYMMDD?
>
> I have this data in database 19990120 and I'd like to make sure that the
> four digit represents the year and the preceding 2 digits represents the
> month and .....
>
> Thanks
>