Solved

SQL Query

Posted on 2013-01-29
8
292 Views
Last Modified: 2013-01-29
SQL.txtI wonder if there is a more effective way to set the values of the '1900-01-01 00:00:00.000' records to NULL. Can this be resolved with one single query?
Thanks for your help.
0
Comment
Question by:baxtalo
  • 4
  • 2
  • 2
8 Comments
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 38832409
What you have is fine.  Is this a one time thing?  If dates are being updated with 1900-01-01 00:00:00.000 as an insert, the more efficient thing to do from the start is insert a NULL value if there is no date.  

<%
newDate="NULL"
If request("date")<>"" then
   if isdate(request("date") then
        newDate=request("date")
   end if
end if

sql="insert ..... datefield='"&newDate&"'"

%>
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 38832484
I think you will find that even if you set those values to NULL, T-SQL will return '1900-01-01 00:00:00.000' as a default value.  In all the postings I've seen, you can't get it to return NULL, just the default date of '1900-01-01 00:00:00.000'.
0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 38832564
Dave, I know at least in ms sql I set dates to NULL if  they are not available at time of creating the record (such as a birthdate) and they remain null.  However, when displaying the record for editing or searching, I set the sql to something like

ISNULL(CAST(BirthDate AS varchar(20)), '')  or ISNULL(CAST(BirthDate AS varchar(20)), 'No Date Avail')
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 38832703
It's a two part question.  If you set it to NULL on INSERT, what does a basic SELECT return?  And if it is actually NULL as opposed to a default value, how do you detect that?
0
 

Author Comment

by:baxtalo
ID: 38832706
This is not a one time thing. I load the schedules from Excel to SQL Server, and at that point all days off show as NULL. But whenever users updated the records the NULLs change to '1900-01-01 00:00:00.000'. When I'm paging the records I use the following HTML to display e.g. the Tuesday column:

<%If IsNull(rs("TueIn")) or rs("TueIn")="" Then %>
<td align="center" colspan="2" bgcolor="#ffffff">
<font color="#cc0000" style="font-size:80%">TUE</font></td><%else%>
<td align="center"><small><small><%=FormatDateTime(rs("TueIn"),vbshorttime)%></small></small></td>
<td align="center"><small><small><%=FormatDateTime(rs("TueOut"),vbshorttime)%></small></small></td><%end if%>

When the TueIn record is NULL the letters 'TUE' are displayed but after a record is updated the NULL in the database becomes '1900-01-01 00:00:00.000' and instead of 'TUE' the record shows 00:00 for both TueIn and TueOut. That's why I added the Update SQL attached. Whenever the user updates a record the same submission resets the values to NULLs. I was just wondering if there is a better way to do it. Maybe by doing something with the if statement in the HTML. I tried ***or rs("TueIn")="1900-01-01 00:00:00.000"*** but it didn't work. Your previous answer was useful, I just didn't know how to insert it in my HTML to also display the Out time in the same column.
0
 
LVL 52

Accepted Solution

by:
Scott Fell,  EE MVE earned 500 total points
ID: 38832788
Like I said, when you create your recordset to display the data, you can use

ISNULL(CAST(SunIn AS varchar(20)),'SUN')

Then in your asp

if rs("SunIn")<>"SUN" then
   response.write formatdatetime(rs("SunIn"),2)
  else
  response.write "SUN" ' or rs("SunIn")
end if

However, a good reason to leave the data as 1/1/1900 instead of null in the actual database is to search.   "....Where SunIn >='1/1/1900'....      

Using NULL's in the db is a good debate.   The key is being consistant so you know what to expect.
0
 

Author Closing Comment

by:baxtalo
ID: 38832882
Thank you so much!
0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 38833005
On another note for efficiency,  why do you have different fields for each day of the week?  Couldn't you just use one date, then use vb or sql to extract or search based on the weekday?    Check out the weekday and weekdayname functions  http://www.w3schools.com/vbscript/vbscript_ref_functions.asp#date
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

773 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