Solved

Check if SQL field NULL

Posted on 2004-04-19
17
1,577 Views
Last Modified: 2007-12-19
I need to check if the SQL field is null...this code deos not work, says NULL value is not acceptable for this field...Please help!!


If IsNull(adoMRS.Fields("ClearName")) Then

strCD = "      "
strCN = "  "

strT1 = strCD & " " & strCN & " " & adoMRS.Fields("Code") & " " &adoMRS.Fields("Description")
ElseIf adoMRS.Fields("ClearName") <> "" Then
     
      strT1 = Format(adoMRS.Fields("ClearDate"), "mm/dd/yy") & " " & Left(adoMRS.Fields("ClearName"), 2) & " " & adoMRS.Fields("Code") & " " & adoMRS.Fields("Description")
End If  
0
Comment
Question by:lexo
  • 6
  • 4
  • 3
  • +3
17 Comments
 
LVL 50

Assisted Solution

by:Lowfatspread
Lowfatspread earned 50 total points
ID: 10862072
where does it say null is not acceptable...

is the column nullable on the table?

what is your SQL Statement...
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 150 total points
ID: 10862076
Please followup on this very old open question:
http://www.experts-exchange.com/Web/Q_20798684.html
0
 
LVL 10

Assisted Solution

by:Shailesh15
Shailesh15 earned 150 total points
ID: 10862078
You can use Nullif in your original sql statement to eliminate null Value from the query itself.

If myfield has null value.. Returns ' '.

Select isnull(myfield,' ') form table

On your question about above code ..Are you sure that error occurs at "If IsNull(adoMRS.Fields("ClearName")) Then"
It could be any field (adoMRS.Fields("ClearDate")) with null value.
0
 
LVL 10

Assisted Solution

by:Shailesh15
Shailesh15 earned 150 total points
ID: 10862085
Oops mistake... "You can use Nullif in your "... should be "You can use isNull in your"...
0
 

Author Comment

by:lexo
ID: 10862089
Yes the column is "nullable"

Set adoMRS = New ADODB.Recordset
   With adoMRS
      strSql = "Select * from UATracking_file  Where Loan_id= '" & strLoanID & "' AND RemoveCond='" & strCond & "' Order by Code"
      .Open strSql, adoMCN, , , 1
   End With

   
   intReg = 8800
   
   While Not adoMRS.EOF
0
 

Author Comment

by:lexo
ID: 10862104
I need the string t equal a certain value IF the field is Null, and another value if the field is not Null...I don't want to elimintate the null results.
0
 
LVL 4

Assisted Solution

by:chenkewind
chenkewind earned 50 total points
ID: 10862243
You can try to look at the length of the field. The length will equal 0 if the field is NULL or empty:

If len(adoMRS.Fields("ClearName"))<=0 Then
   strCD = "      "
   strCN = "  "
   strT1 = strCD & " " & strCN & " " & adoMRS.Fields("Code") & " " &adoMRS.Fields("Description")
Else
      strT1 = Format(adoMRS.Fields("ClearDate"), "mm/dd/yy") & " " & Left(adoMRS.Fields("ClearName"), 2) & " " & adoMRS.Fields("Code") & " " & adoMRS.Fields("Description")
End If  
0
 

Author Comment

by:lexo
ID: 10862399
"Please followup on this very old open question:
http://www.experts-exchange.com/Web/Q_20798684.html"

THis is OLD, I don't care about this question anymore...plesae answer the current question, it's 500 points, the xml question is 50.
0
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)

 
LVL 75

Assisted Solution

by:Anthony Perkins
Anthony Perkins earned 150 total points
ID: 10862493
>>THis is OLD, I don't care about this question anymore<<
Exactly.  Please post a message in Community Support to have closed.

Any question over 21 days is considerd technically abandoned:
What happens if I don't close my question?
http://www.experts-exchange.com/help.jsp#hi78
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 10862517
chenkewind,

>>The length will equal 0 if the field is NULL or empty:<<
Nope.
Len(Null) = Null
0
 
LVL 7

Assisted Solution

by:ChrisFretwell
ChrisFretwell earned 100 total points
ID: 10862521
I think you mixed up the use of the isnull and is null and made things more difficult than you had to

isnull isnt a check to see if something is null. For that you would do this:

If (adoMRS.Fields("ClearName"))  is NULL Then

strCD = "      "
strCN = "  "

strT1 = strCD & " " & strCN & " " & adoMRS.Fields("Code") & " " &adoMRS.Fields("Description")
ElseIf adoMRS.Fields("ClearName") <> "" Then
     
      strT1 = Format(adoMRS.Fields("ClearDate"), "mm/dd/yy") & " " & Left(adoMRS.Fields("ClearName"), 2) & " " & adoMRS.Fields("Code") & " " & adoMRS.Fields("Description")
End If  


If you all you want to do is assign a different value if a field is null, then you can do it in a single line
select isnull(fieldtocheck,valueifnull)  
this will return your original field if the value isnt null, or what you want it to be if it is null

Subsequently, if you want multiple options, one of which is null, you can do a case statement

str1 = case
when adoMRS.Fields("ClearName")  is null then strCD & " " & strCN & " " & adoMRS.Fields("Code") & " " &adoMRS.Fields("Description")
when adoMRS.Fields("ClearName") = "" then Format(adoMRS.Fields("ClearDate"), "mm/dd/yy") & " " & Left(adoMRS.Fields("ClearName"), 2) & " " & adoMRS.Fields("Code")
else "what todo when not null"
end
0
 
LVL 75

Assisted Solution

by:Anthony Perkins
Anthony Perkins earned 150 total points
ID: 10862672
ChrisFretwell,

>>isnull isnt a check to see if something is null<<
I suspect you are confusing T-SQL with VBScript.  In VBScript IsNull is used to see if a value is Null

<quote>
IsNull Function
Returns a Boolean value that indicates whether an expression contains no valid data ( Null).
</quote>
0
 
LVL 7

Assisted Solution

by:ChrisFretwell
ChrisFretwell earned 100 total points
ID: 10862706
Ah, so having this in the sql forum doesnt mean it was a sql error. My mistake. Forget what I said.
0
 
LVL 10

Assisted Solution

by:Shailesh15
Shailesh15 earned 150 total points
ID: 10862731
>>  >>isnull isnt a check to see if something is null<<
>>  I suspect you are confusing T-SQL with VBScript.  In VBScript IsNull is used to see if a value is Null


"Set adoMRS = New ADODB.Recordset"
I think he is using VB!!!... Guess.. everyone is confused!
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 10862761
ChrisFretwell,

>>Ah, so having this in the sql forum doesnt mean it was a sql error.<<
Right.  This question would be better served in the Visual Basic or VB Databases TA's

>>I think he is using VB!!!<<
I know.  It is the same function in VBScript.
0
 

Author Comment

by:lexo
ID: 10865153
Yeah, VB...

Umm so, I guess I should place this in another section to get it answered...
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 10868622
It would help.  But please close this question, even if you do not care about it any more.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

708 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now