Link to home
Start Free TrialLog in
Avatar of baxtalo
baxtalo

asked on

SQL Server datetime displayed on classic ASP page

Hi Experts,
I'm trying to display the time on my classic ASP page. When the data is NULL the code below displays the word "Sunday".

<%If IsNull(rs("SunIn")) or rs("SunIn")="" Then %>
Sunday
<%else%>
<%=FormatDateTime(rs("SunIn"),vbshorttime)%>
<%end if%>

The problem is that when the entry is updated the NULL becomes "1900-01-01 00:00:00.000" in the database.

I would still like the word "Sunday" displayed.
How could I change the code in order to achieve this? I tried
rs("SunIn")="1900-01-01 00:00:00.000"
but it didn't help.

I would really appreciate your help.
Avatar of Barry62
Barry62
Flag of United States of America image

What is the value of SunIn?
Avatar of baxtalo
baxtalo

ASKER

SunIn is datetime and when it's NULL it displays the word Sunday. When it's not null but it has an actual time value, it displays the time, e.g. 13:50
When I update the entry the NULL becomes "1900-01-01 00:00:00.000"
The word Sunday is not displayed anymore, instead it's displayed as 00:00

When I asked the question I meant "How can I display "1900-01-01 00:00:00.000" as Sunday on a classic ASP page.

I tried:

<%If IsNull(rs("SunIn")) or (rs("SunIn")="1900-01-01 00:00:00.000") Then %>
Sunday
<%else%>
<%=FormatDateTime(rs("SunIn"),vbshorttime)%>
<%end if%>

but still "00:00" was displayed instead of the word "Sunday".
I think it's because you are actually comparing a datetime value (SunIn) to a string ("1900-01-01 00:00:00.000").  You will have to convert the string to a date:

<%If IsNull(rs("SunIn")) or (rs("SunIn")=CDate("1900-01-01 00:00:00.000")) Then %>

Open in new window

Avatar of baxtalo

ASKER

Maybe I missed something, but this gave me the following error message:

Microsoft VBScript runtime error '800a000d'
Type mismatch: 'CDate'
I think the problem is that when you update the entry, you may be inserting something other than a NULL in the SunIn field if there is no actual time.   A datetime field will always default to 1900-01-01 00:00:00.000 if an invalid datetime is inserted.

Check your table structure and make sure your SunIn field will accept NULL. Then check your Update statement to make sure you are inserting a NULL when there is no real datetime value.
Avatar of baxtalo

ASKER

I was thinking of an alternative method. I've just placed an update button on the page. When users click it, the 1900-01-01 00:00:00.000 will be set back to NULL:

UPDATE ATOBidNew SET SunIn = NULL WHERE SunIn = '1900-01-01 00:00:00.000'

It's not too user friendly, but I can't think of anything better. This is my update page, how could I modify it in order to insert nulls instead of '1900-01-01 00:00:00.000'?

<%
myId = Request.form("ID")
mySunIn = request.form("SunIn")
StrSQL = "Update MyTable SET SunIn= '" & mySunIn & "' Where ID="&myId&""
conn.Execute StrSQL
%>

Thank you so much for your time.
All the best,
Baxtalo
ASKER CERTIFIED SOLUTION
Avatar of Barry62
Barry62
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of baxtalo

ASKER

Thank you very much!
You're very welcome!