Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Error converting data type varchar to float - MS SQL Server

Posted on 2013-12-06
6
7,863 Views
Last Modified: 2013-12-09
I am facing data type conversation error while executing below select statement. Please advise how to fix below error.

SELECT 
CONVERT(VARCHAR(10), #SVC.ID1) As ID1,
'Batch,' + 'ID1,' + CONVERT(VARCHAR(10), #SVC.ID1) + ',SVC' AS Batch,
'Urgent request,' + 'ID1,' + CONVERT(VARCHAR(10), #SVC.ID1) + ',SVC' AS [Urgent request],
'Total of Lines,' + 'ID1,' + CONVERT(VARCHAR(10), #SVC.ID1) + ',SVC' AS [Total of Lines],
'Total quote value,' + 'ID1,' +CONVERT(VARCHAR(10), #SVC.ID1) + ',SVC' AS [Total quote value],
'PO Value,' + 'ID1,' + CONVERT(VARCHAR(10), #SVC.ID1) + ',SVC' AS [PO Value]
INTO #SVCSummary
FROM #SVC
GROUP BY [#SVC].[ID1],
#SVC.[Total of Lines],
#SVC.[Total quote value]
ORDER BY 
#SVC.[ID1] ASC;
SELECT Batch,
[Urgent Request],
[Total of Lines],
[Total Quote Value],
[PO Value]
FROM #HW
UNION 
SELECT Batch,
[Urgent Request],
[Total of Lines],
[Total Quote Value],
[PO Value]
FROM #SVCSummary;

Open in new window

Data type details:
a. Batch column (float,null)
b. ID1 column (float,null)
c. [Urgent Request] column (bit not null)
d. [Total of Lines] column (float null)
e. [Total Quote Value] column (money null)
f. [PO Value] column  (float null)

Error:
Error converting data type varchar to float
0
Comment
Question by:sqldba2013
  • 3
  • 2
6 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 125 total points
ID: 39701406
>SELECT  CONVERT(VARCHAR(10), #SVC.ID1) As ID1,
> 'Batch,' + 'ID1,' + CONVERT(VARCHAR(10), #SVC.ID1) + ',SVC' AS Batch,
fyi the #SVC.ID1 references will be as whatever data type ID1 is, and not the above-converted varchar.
 
Looking at the second SELECT that has the UNION, the entire set will be based on the data types of the first part of the UNION ( #HW ), which means that the columns in the second part of the UNION ( #SVCSummary ) must be the same type.  Eyeball these tables to see if there are any data type differences.
0
 
LVL 32

Assisted Solution

by:Daniel Wilson
Daniel Wilson earned 125 total points
ID: 39701411
Which select statement is giving you the error?  the one starting on line 1 or on line 15?

Are floats coming out of #HW for [Total of Lines] or [Total Quote Value]?  If so, wrap them in CONVERT statements like you did for many values in the first query.
0
 

Author Comment

by:sqldba2013
ID: 39702645
Line 15 is giving error.

I tried to convert columns to float but still I am getting data type conversion error.

Pls advise.
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.

 

Author Comment

by:sqldba2013
ID: 39702764
I am getting error in line 22 to 26.

Please correct my mistake in above sql query.
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39703103
>I am getting error in line 22 to 26.
Eyeball the column data types in tables #HW and #SVCSummary, and tell us if for any column the data type is different.

>Error converting data type varchar to float
I'm thinking that for a column #HW is a float, and for #SVCSummary it's a varchar.
0
 

Author Closing Comment

by:sqldba2013
ID: 39705779
--
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

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.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

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