Sorry it’s a long explanation but I’ve tried to cover everything I’ve tried so far.
I’m trying to do a comparison between values in two fields (they’re supposed to match up), but one field is defined float(8), and the other varchar(50).
Using the code here (I’ve stripped out all unnecessary fields etc.), I get the error listed below.
SELECT lcd.ClaimNo, tf.WorkSourceRef
FROM lawCallDataRaw lcd
INNER JOIN tFile tf ON
lcd.ClaimNo = tf.WorkSourceRef
-- Where lcd.ClaimNo is the float(8) and tf.WorkSourceRef is the varchar(50)
Server: Msg 8114, Level 16, State 5, Line n
Error converting data type varchar to float.
So I tried to convert the float field to be varchar using CAST
SELECT lcd.ClaimNo, tf.WorkSourceRef
FROM lawCallDataRaw lcd
INNER JOIN tFile tf ON
CAST(lcd.ClaimNo AS VARCHAR(50)) = tf.WorkSourceRef
This works except the ClaimNo displays as "483120.0", so need to try & drop the " .0 ".
Reading the SQL Help I decided to try STR instead of CAST.
SELECT lcd.ClaimNo, tf.WorkSourceRef
FROM lawCallDataRaw lcd
INNER JOIN tFile tf ON
STR(lcd.ClaimNo, 50, 0) = tf.WorkSourceRef
This doesn't fail the syntax check, but doesn't extract any data. I did a separate SQL with only the lawCallDataRaw file to check the STR results and found that it extracted a field that is fixed 50 characters wide with 44 spaces before the 6 actual characters.
I also read in the SQL Help that it’s advisable to …
“Note To convert to Unicode data, use STR inside a CONVERT or CAST conversion function.”
So, I’ve tried a variety of code looking something like this …
SELECT lcd.ClaimNo, tf.WorkSourceRef
FROM lawCallDataRaw lcd
INNER JOIN tFile tf ON
CAST((STR(lcd.ClaimNo, 50, 0)) AS VARCHAR(50)) = tf.WorkSourceRef
(this produced the same fixed 50 result as the STR on it’s own).
Variations on the attempts below produce Syntax Errors, so I have no idea what’s wrong …
CAST AS (STR(lcd.ClaimNo, 50, 0)) = tf.WorkSourceRef
CAST(lcd.ClaimNo AS VARCHAR(50)) AS (STR(lcd.ClaimNo, 50, 0)) = tf.WorkSourceRef
… there are no examples & I can’t get it to work … and since I’m pretty much a novice at SQL, then maybe it’s not the best way to do this after all.
Help would be very much appreciated.
I’m trying to do a comparison between values in two fields (they’re supposed to match up), but one field is defined float(8), and the other varchar(50).
Using the code here (I’ve stripped out all unnecessary fields etc.), I get the error listed below.
SELECT lcd.ClaimNo, tf.WorkSourceRef
FROM lawCallDataRaw lcd
INNER JOIN tFile tf ON
lcd.ClaimNo = tf.WorkSourceRef
-- Where lcd.ClaimNo is the float(8) and tf.WorkSourceRef is the varchar(50)
Server: Msg 8114, Level 16, State 5, Line n
Error converting data type varchar to float.
So I tried to convert the float field to be varchar using CAST
SELECT lcd.ClaimNo, tf.WorkSourceRef
FROM lawCallDataRaw lcd
INNER JOIN tFile tf ON
CAST(lcd.ClaimNo AS VARCHAR(50)) = tf.WorkSourceRef
This works except the ClaimNo displays as "483120.0", so need to try & drop the " .0 ".
Reading the SQL Help I decided to try STR instead of CAST.
SELECT lcd.ClaimNo, tf.WorkSourceRef
FROM lawCallDataRaw lcd
INNER JOIN tFile tf ON
STR(lcd.ClaimNo, 50, 0) = tf.WorkSourceRef
This doesn't fail the syntax check, but doesn't extract any data. I did a separate SQL with only the lawCallDataRaw file to check the STR results and found that it extracted a field that is fixed 50 characters wide with 44 spaces before the 6 actual characters.
I also read in the SQL Help that it’s advisable to …
“Note To convert to Unicode data, use STR inside a CONVERT or CAST conversion function.”
So, I’ve tried a variety of code looking something like this …
SELECT lcd.ClaimNo, tf.WorkSourceRef
FROM lawCallDataRaw lcd
INNER JOIN tFile tf ON
CAST((STR(lcd.ClaimNo, 50, 0)) AS VARCHAR(50)) = tf.WorkSourceRef
(this produced the same fixed 50 result as the STR on it’s own).
Variations on the attempts below produce Syntax Errors, so I have no idea what’s wrong …
CAST AS (STR(lcd.ClaimNo, 50, 0)) = tf.WorkSourceRef
CAST(lcd.ClaimNo AS VARCHAR(50)) AS (STR(lcd.ClaimNo, 50, 0)) = tf.WorkSourceRef
… there are no examples & I can’t get it to work … and since I’m pretty much a novice at SQL, then maybe it’s not the best way to do this after all.
Help would be very much appreciated.