Solved

Check if SQL field NULL

Posted on 2004-04-19
17
1,585 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
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
 
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

808 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