| Author |
Topic |
|
werhardt
Constraint Violating Yak Guru
270 Posts |
Posted - 2008-09-30 : 08:38:31
|
| I have this query and I am getting an error message, I am not sure how to fix it. I thought it was the decimal place, but it is not. See below.The error message I am getting is "Error converting data type varchar to numeric".selectprin_diag ,case when substring(prin_diag,1,1) in ('E','V') thensubstring(prin_diag,1,1) + '' + cast(round(cast (substring(prin_diag,2,10) as decimal(10,2)),3) as varchar) else cast (round(cast ( prin_diag as decimal(10,2)),2) as varchar) endfromdbo.CLAIMHEADERTEMP group by prin_diag |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-09-30 : 08:40:35
|
As varchar(what)? E 12°55'05.63"N 56°04'39.26" |
 |
|
|
werhardt
Constraint Violating Yak Guru
270 Posts |
Posted - 2008-09-30 : 08:42:04
|
| I don't understand what you are asking? |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-09-30 : 08:45:13
|
CAST AS VARCHAR (without size) can truncate your result unexpectedly.Set a size for the cast. E 12°55'05.63"N 56°04'39.26" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-09-30 : 08:48:36
|
[code]SELECT prin_diag, CASE WHEN prin_diag LIKE '[EV]%' THEN SUBSTRING(prin_diag, 1, 1) + CAST(ROUND(CAST(SUBSTRING(prin_diag, 2, 10) AS DECIMAL(10,2)), 3) AS VARCHAR(12)) ELSE CAST(ROUND(CAST(prin_diag AS DECIMAL(10,2)), 2) AS VARCHAR(12)) ENDFROM dbo.CLAIMHEADERTEMP GROUP BY prin_diag[/code] E 12°55'05.63"N 56°04'39.26" |
 |
|
|
werhardt
Constraint Violating Yak Guru
270 Posts |
Posted - 2008-09-30 : 09:16:05
|
| I am still getting an error. Do you think it has to do with (10,2)),3) selectprin_diag ,case when substring(prin_diag,1,1) in ('E','V') thensubstring(prin_diag,1,1) + '' + cast(round(cast (substring(prin_diag,2,10) as decimal(10,2)),3) as varchar) else cast (round(cast ( prin_diag as decimal(10,2)),2) as varchar (12)) endfromdbo.CLAIMHEADERTEMP |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-09-30 : 09:20:52
|
No need for extra round, since cast DECIMAL(10, 2) already have rounded your result.SELECT prin_diag, CASE WHEN prin_diag LIKE '[EV]%' THEN SUBSTRING(prin_diag, 1, 1) + CAST(CONVERT(DECIMAL(10,2), SUBSTRING(prin_diag, 2, 10)) AS VARCHAR(12)) ELSE CAST(CONVERT(DECIMAL(10,2), prin_diag) AS VARCHAR(12)) ENDFROM dbo.CLAIMHEADERTEMP GROUP BY prin_diag E 12°55'05.63"N 56°04'39.26" |
 |
|
|
werhardt
Constraint Violating Yak Guru
270 Posts |
Posted - 2008-09-30 : 09:31:00
|
| well, I just found out that the field that I am trying to convert has alpha in it as well. This field is a varchar 6. I think that is the reason I am getting the error. I have some date that says V701.12 |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-09-30 : 09:34:30
|
So, what was the output from my suggested query? E 12°55'05.63"N 56°04'39.26" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-09-30 : 09:38:30
|
[code]DECLARE @Sample TABLE ( Data VARCHAR(10) )INSERT @SampleSELECT 'V701.12' UNION ALLSELECT 'E666.81' UNION ALLSELECT '123.45'SELECT Data, SUM(CONVERT(DECIMAL(10, 2), SUBSTRING(Data, PATINDEX('%[0-9]%', Data), 10)))FROM @SampleGROUP BY Data[/code] E 12°55'05.63"N 56°04'39.26" |
 |
|
|
werhardt
Constraint Violating Yak Guru
270 Posts |
Posted - 2008-09-30 : 09:41:17
|
I was still getting Error converting data type varchar to numeric".The last statment you gave me worked. Thanks! |
 |
|
|
atulmar
Starting Member
7 Posts |
Posted - 2008-09-30 : 11:46:29
|
| Why dont you run your query with select, without doing this conversion to decimal or numeric.This must be a data issue in your table.ThanksAtulThanksAtul |
 |
|
|
atulmar
Starting Member
7 Posts |
Posted - 2008-09-30 : 11:48:34
|
| Additionally you check for ISNUMERIC function, if your data is convertible. Otherwise deselect the data that is not convertible by filtering on ISNUMERIC(<Value>) = 0ThanksAtul |
 |
|
|
werhardt
Constraint Violating Yak Guru
270 Posts |
Posted - 2008-09-30 : 15:39:24
|
| This does work, but it is taking my V and E out of the results. Has V701.12 then result is 701.12DECLARE @Sample TABLE ( Data VARCHAR(10) )INSERT @SampleSELECT 'V701.12' UNION ALLSELECT 'E666.81' UNION ALLSELECT '123.45'SELECT Data, SUM(CONVERT(DECIMAL(10, 2), SUBSTRING(Data, PATINDEX('%[0-9]%', Data), 10)))FROM @SampleGROUP BY Data |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-09-30 : 15:54:30
|
Maybe you should tell us the purpose of the query, beacuse the query makes no sense.You can aggregate strings with MIN and MAX, not SUM.Is this what you want?DECLARE @Sample TABLE ( Data VARCHAR(10) )INSERT @SampleSELECT 'V701.12' UNION ALLSELECT 'E666.81' UNION ALLSELECT '123.45'SELECT Data, LTRIM(STUFF(Data, PATINDEX('%[0-9]%', Data), 0, ' '))FROM @SampleIf it is not, please read, understand and follow the advice given herehttp://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspx E 12°55'05.63"N 56°04'39.26" |
 |
|
|
|