Solved

Receiving error message: Error converting data type nvarchar to float.

Posted on 2006-11-16
16
541 Views
Last Modified: 2008-01-09
I have a .NET application (SQL Server backend). I am using a SQL statement to retrieve data and then display it on my web page.

I'm working with a field that is nvarchar. I need to be able to display an N/A when there is no data for that particular field. Here's the portion of the SQL Statement that I have so far:

ISNULL(pmoApps.dbo.ss_PDMMAST.acthours, N'N/A') as SEHours

When I run it, I'm receiving the error: Error converting data type nvarchar to float. What do I need to add to it to make it display N/A.

Thanks!

0
Comment
Question by:khawkins96
  • 6
  • 4
  • 2
  • +3
16 Comments
 
LVL 9

Expert Comment

by:kraffay
ID: 17957865
Can you try putting this on your SQL:

SEHours = CASE pmoApps.dbo.ss_PDMMAST.acthours
         WHEN pmoApps.dbo.ss_PDMMAST.acthours ISNULL 'N/A'
          ELSE pmoApps.dbo.ss_PDMMAST.acthours
        END
0
 

Author Comment

by:khawkins96
ID: 17957973
Thanks for the suggestion. I tried it and I'm getting another error message that says: Incorrect syntax near 'ISNULL'
0
 
LVL 11

Expert Comment

by:ethoths
ID: 17958020
Basically you cannot mix datatypes in this way. If there is data it's a float, it there is not it's a string.

You'll need to catch this is the place that displays it not at the source. The exact method you choose depends on what you're doing with the data.
0
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
LVL 9

Expert Comment

by:kraffay
ID: 17958043
Sorry, can you try this:

SEHours = CASE pmoApps.dbo.ss_PDMMAST.acthours
         WHEN ISNULL(pmoApps.dbo.ss_PDMMAST.acthours)  'N/A'
          ELSE pmoApps.dbo.ss_PDMMAST.acthours
        END
0
 
LVL 11

Expert Comment

by:ethoths
ID: 17958059
Sorry, you sid the datatype is an nvarchar,  is this what you are doing?

select ISNULL(pmoApps.dbo.ss_PDMMAST.acthours, N'N/A') as SEHours from whatever
0
 

Author Comment

by:khawkins96
ID: 17958231
kraffay,

I tried that and received a new error: The isnull function requires 2 arguments

ethoths - this is what I was working with: ISNULL(pmoApps.dbo.ss_PDMMAST.acthours, N'N/A') as SEHours
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17958384
That previous attempt was close, so try this:

       SEHours = CASE pmoApps.dbo.ss_PDMMAST.acthours
         WHEN pmoApps.dbo.ss_PDMMAST.acthours IS NULL 'N/A'
          ELSE pmoApps.dbo.ss_PDMMAST.acthours
        END

Another Transact-SQL statement that you can use is COALESCE:

COALESCE
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_ca-co_9dph.asp

Returns the first non-null expression among its arguments.

Bob
0
 

Author Comment

by:khawkins96
ID: 17958463
TheLearnedOne - I tried the code you listed there and I"m getting an error:

Incorrect syntax near 'IS NULL'

I haven't tried your other suggestion yet. I will do so. If you have another other suggestion regarding the code you listed and the error I"m receiving, please let me know.

Thanks!
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17958522
Man, I always forget that IS NULL can't be used in a CASE:

Here is an example of using the ISNULL function to your benefit, without the CASE:
    SELECT ISNULL(userid, 'N/A') AS userid FROM profile

The 2nd argument is the replacement string if the value is null.

Bob

0
 

Author Comment

by:khawkins96
ID: 17958554
The LearnedOne,

That's basically what I previously had:

ISNULL(pmoApps.dbo.ss_PDMMAST.acthours, N'N/A') as SEHours

I was getting an error that said it couldn't convert data type nvarchar to float. I'm working with an existing table that I didn't create and the data type for this field is nvarchar.

I need to display N/A when the field is null. That's a requirement of the user - I don't think COALESCE will work for me.
0
 
LVL 28

Expert Comment

by:strickdd
ID: 17958569
try this:

SEHours = case pmoApps.dbo.ss_PDMMAST.acthours when null then 'N/A' else pmoApps.dbo.ss_PDMMAST.acthours end


This will not work in an order by though
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17958599
Aaah, I get it, pmoApps.dbo.ss_PDMMAST.acthours is a float, and you are trying to get a nvarchar value N'N/A'.

Do you need the float values on the front end, or could you cast to nvarchar?

Bob
0
 

Author Comment

by:khawkins96
ID: 17958690
strickdd  - that brought me a lot closer to the solution! I didn't receive an error message this time and my record came up on my web page, but the field that was null was empty. It should have had N/A in it. Any ideas?

The LearnedOne - the field is nvarchar in the table. I'm trying to get it to display N/A but it says it can't convert nvarchar to float. Not sure what that message means since I didn't have a convert statement in my sql statement.
0
 
LVL 21

Accepted Solution

by:
Dale Burrell earned 350 total points
ID: 17962514
Are you absolutely sure that pmoApps.dbo.ss_PDMMAST.acthours is an nvarchar? The fact that you are storing a number (i.e. number of hours) makes that unlikely. And if it is that piece of SQL that is causing the problem then it certainly points towards pmoApps.dbo.ss_PDMMAST.acthours being a float because the isnull function will always return the type of the first parameter which means that the second parameter must be convertable to the datatype of the first parameter so that it can return the same type in all cases.

Personally, assuming that is the case, I'd return the null because then in the code you can a) format your number into a string as you desire and b) replace the null with 'N/A' thereby leaving the formating to the correct layer of code.

Otherwise if this is being used directly and you need the returned recordset to be as it will display then convert the pmoApps.dbo.ss_PDMMAST.acthours into a varchar first and e.g.

isnull(convert(nvarchar, pmoApps.dbo.ss_PDMMAST.acthours), 'N/A')

And if nothing else trying this convert will determine whether the problem lies here or elsewhere.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17964344
I am starting to question where the problem really is.  We are looking very closely at a specific piece of an SQL statement, but what is telling you that it is the one causing the problems.  The syntax and the error don't match.

Bob
0
 

Author Comment

by:khawkins96
ID: 17966212
Dale,

It worked! isnull(convert(nvarchar, pmoApps.dbo.ss_PDMMAST.acthours), 'N/A')

Thank you so much!
0

Featured Post

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.

Question has a verified solution.

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

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

822 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