Reputation: 43
I am trying to insert a float value from one table into another, but am getting the error in the subject line of this post:
Error converting data type varchar to float.
Both fields in both tables are set to float. Values are latitudes and longitudes for zip codes. An example of a latitude/longitude would be:
45.895698 -72.365896
Here is a few snippets of my code:
declare @v_latitude float
declare @v_longitude float
-- Grab Latitude & Longitude --
if @v_zip_code IS NOT NULL
SELECT @v_latitude=z_latitude, @v_longitude=z_longitude FROM tbl_ZipCodes WHERE z_zip_code = @v_zip_code
set @strValues = @strValues + '''' + @v_city + ''', ''' + @v_state + ''', ''' + @v_zip_code + ''', ''' + @v_daytime_phone + ''', ''' + @v_contact_name + ''', '''
+ @v_email_address + ''', ' + cast(@u_id as varchar) + ', ' + cast(@d_id as varchar) + ', 1, ''' + cast(CURRENT_TIMESTAMP as varchar) + ''', 1 , ''' + cast(@v_latitude as float) + ''', ''' + cast(@v_longitude as float) + ''', '''
+ cast(CURRENT_TIMESTAMP as varchar) + ''', dft.df_t_id, (SELECT CASE WHEN dft.df_t_v_price <> 0 THEN dft.df_t_v_price WHEN dft.df_t_v_internet_price <> 0 THEN dft.df_t_v_internet_price WHEN dft.df_t_v_other_price <> 0 THEN dft.df_t_v_other_price ELSE 0 END AS v_search_price)'
The part of the code that uses the @v_latitude and @v_longitude variables is here:
... , ''' + cast(@v_latitude as float) + ''', ''' + cast(@v_longitude as float) + ''', ...
Ignore the "..." as I am demonstrating there is additional code that surrounds those values.
Any help would be appreciated!
Edit: Here is additional code I am using:
-- Check For User Profile --
SELECT @counter = COUNT(*) FROM tbl_Customers WHERE u_id = @u_id
if @counter > 0
SELECT @v_city=c_city, @v_state =c_state, @v_zip_code=c_zip_code,@v_daytime_phone =c_phone_number FROM tbl_Customers WHERE u_id = @u_id
else
SELECT @v_city=d_city, @v_state =d_state, @v_zip_code=d_zip_code,@v_daytime_phone =d_phone_number FROM tbl_Dealers WHERE d_id = @d_id
-- Grab Contact Information --
SELECT @v_contact_name = u_name, @v_email_address = u_email_address FROM tbl_Users WHERE u_id = @u_id
-- Grab Latitude & Longitude --
if @v_zip_code IS NOT NULL
SELECT @v_latitude=z_latitude, @v_longitude=z_longitude FROM tbl_ZipCodes WHERE z_zip_code = @v_zip_code
-- Add Additional Fields --
set @strFields = @strFields + 'v_city, v_state, v_zip_code, v_daytime_phone, v_contact_name, v_email_address, u_id, d_id, v_processed, v_processed_date, v_active, ,v_latitude, v_longitude, v_last_activity, v_dealerfeed, v_search_price'
set @strJoin = @strJoin + ' LEFT JOIN tbl_Vehicles v ON v.v_stock_number = dft.df_t_v_stock_number'
set @strValues = @strValues + '''' + @v_city + ''', ''' + @v_state + ''', ''' + @v_zip_code + ''', ''' + @v_daytime_phone + ''', ''' + @v_contact_name + ''', '''
+ @v_email_address + ''', ' + cast(@u_id as varchar) + ', ' + cast(@d_id as varchar) + ', 1, ''' + cast(CURRENT_TIMESTAMP as varchar) + ''', 1 , ''' + @v_latitude + ''', ''' + @v_longitude + ''', '''
+ cast(CURRENT_TIMESTAMP as varchar) + ''', dft.df_t_id, (SELECT CASE WHEN dft.df_t_v_price <> 0 THEN dft.df_t_v_price WHEN dft.df_t_v_internet_price <> 0 THEN dft.df_t_v_internet_price WHEN dft.df_t_v_other_price <> 0 THEN dft.df_t_v_other_price ELSE 0 END AS v_search_price)'
-- Insert Records Into Vehicle Table (but not twice!) --
set @strSQL = 'INSERT INTO tbl_Vehicles (' + @strFields + ') SELECT ' + @strValues + ' FROM tbl_DealerFeed_temp dft ' + @strJoin + ' WHERE dft.df_t_processed = 0 AND dft.df_d_id = ' + cast(@df_d_id as varchar)
set @strSQL = @strSQL + ' AND dft.df_t_v_stock_number NOT IN ( SELECT v.v_stock_number FROM tbl_Vehicles v WHERE v.d_id = ' + cast(@d_id as varchar) + ')'
print @strSQL
EXEC (@strSQL)
Upvotes: 3
Views: 2922
Reputation: 1496
The problem probably is the fact that your dynamic sql is putting single quotes around the float.
Try changing
, ''' + cast(@v_latitude as float) + ''', ''' + cast(@v_longitude as float) + ''', ..
into
, ' + STR(@v_latitude,<length>,<decimals>) + ', ' + STR(@v_longitude,<length>,<decimals>) + ', ..
See STR () for reference
Upvotes: 2
Reputation: 2111
As you're doing dynamic SQL you force everything down to a string type.
But it's the single quotes that are tripping you up. If the column you're inserting into is a float you don't want your float value on the insert wrapped in quotes.
Also I find CONVERT
is a little more flexible (with dates mostly) than CAST
(I've only done CONVERT for diversity):
so you want get rid of those triple quotes:
...+ CONVERT(varchar(20), @v_latitude) +',' + CONVERT(varchar(20), @v_longitude) +...
Edited to add: your fixed code should look like this:
set @strValues = @strValues + '''' + @v_city + ''', ''' + @v_state + ''', ''' + @v_zip_code + ''', ''' + @v_daytime_phone + ''', ''' + @v_contact_name + ''', '''
+ @v_email_address + ''', ' + cast(@u_id as varchar) + ', ' + cast(@d_id as varchar) + ', 1, ''' + cast(CURRENT_TIMESTAMP as varchar) + ''', 1 , ' + Convert(varchar(20), @v_latitude) + ', ' + Convert(varchar(20), @v_longitude) + ', '''
+ cast(CURRENT_TIMESTAMP as varchar) + ''', dft.df_t_id, (SELECT CASE WHEN dft.df_t_v_price <> 0 THEN dft.df_t_v_price WHEN dft.df_t_v_internet_price <> 0 THEN dft.df_t_v_internet_price WHEN dft.df_t_v_other_price <> 0 THEN dft.df_t_v_other_price ELSE 0 END AS v_search_price)'
Execute your SQL without the EXEC (@strSQL)
and have a look at the printed SQL
Edited again; to fix your Float being limited to being rounded to 4 decimal places (this is down to it being a Float) but you can get around it by converting it to a decimal first or using STR
, this'll help you understand what's happening:
declare @v_latitude float = -45.12345678
print @v_latitude
print 'Cast = '+ cast( @v_latitude as varchar)
print 'Convert = '+ convert(varchar(20), @v_latitude)
print 'Cast via decimal = '+ cast( cast(@v_latitude as decimal(18,9)) as varchar)
print 'Convert via decimal = '+ convert( varchar(20), convert(decimal(18,9), @v_latitude))
print 'str = '+ str(@v_latitude,18,9)
==
-45.1235
Cast = -45.1235
Convert = -45.1235
Cast via decimal = -45.123456780
Convert via decimal = -45.123456780
str = -45.123456780
Upvotes: 1
Reputation: 5458
you can try
''' + cast( @v_latitude as varchar(20))
+ ''', ''' + cast((@v_longitude as varchar(20))+ '''
Upvotes: 0