Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
hbadministrator
Posting Yak Master
120 Posts |
Posted - 2013-07-03 : 10:33:52
|
My code worked last week but now all of the sudden things stopped working and I need help. I keep getting this Conversion Failed when converting the varchar value '488.8 to data type int.SELECT [sa-hist].[cust-no], CASE WHEN CAST((sa.[sa-fields__1]) AS INT) = '' THEN '0' ELSE CAST((sa.[sa-fields__1]) AS INT) END AS Fuel,CASE WHEN CAST((sa.[sa-fields__2]) AS INT) = '' THEN '0' ELSE CAST((sa.[sa-fields__2]) AS INT) END AS LPFuelFROM sa AS sa INNER JOIN[sa-hist] AS [sa-hist] ON sa.[sa-no] = [sa-hist].[sa-no]WHERE ([sa-hist].[sa-type] LIKE 'F%')I know the fields (Fuel and LPFuel) holds other values like dates and numbers and the Varchar is (220)Cust-no,Fuel, LPFuel010336 891.8 0010526 428.5 0011036 658.5 0011335 509.5 0011343 789.5 44.6011531 1171 0011631 862.7 0012640 557.5 0012985 944.6 0013018 728.7 0013080 426.2 0013101 565.6 0013237 1038.3 0014446 556.8 0014605 1064.1 0014767 395 167.9014904 215.6 0015051 612.1 0015228 1288.8 0016775 469.6 0017902 328.5 0019073 591.8 0020460 594.6 0020796 725.9 0020861 475.7 0022235 668.7 0022349 462.5 0022437 892.2 0022581 871.7 0022583 445.2 0022598 846.7 0022607 448.4 0022724 876 0023143 457.4 0024307 734.1 0024312 382.5 0024471 760.9 0024569 468.5 0024901 1350.1 0025573 410.3 0025586 433.4 0025670 693.7 0025742 788.6 0026766 494.8 0027287 505 0027440 592.5 0027800 636.3 0027819 946.8 0028062 417 0028113 731.8 0028313 701.3 0028349 497.1 0030012 678.7 0030195 619.2 0030251 621 0030838 750.2 0030891 316.4 0030907 445.8 0031545 300.6 0031659 279.4 0031661 457.3 0032092 404.6 0032443 311.6 0032588 621.4 0032676 607 0032744 696.5 0032780 340.7 0032996 237.8 0033356 391.5 0033566 1189.4 0034173 839.2 0034195 1487.7 0034338 675.7 0034512 531.3 0034753 510.8 0035390 940 0035394 417.7 0035605 846 0035732 352 0036192 515.9 0036240 746.7 0036669 610 0037437 926.9 0038828 919.7 0040346 883 0040797 488.5 0040865 779.7 0041100 192.7 0041613 500 0041972 968.9 0042000 697.6 0042266 911.7 0042639 539 0042855 869.9 0043234 841.1 0043242 189 0043537 750.6 0043895 428.9 0044256 492.9 0044657 611.2 0044678 1243.5 0044716 383.5 0044763 625.2 0045271 846 0045317 909.4 0045673 415.9 0045677 626.8 0045778 867.8 0046042 633.3 0046300 1082.7 0046314 687.2 0046325 1126.8 0046365 850.8 378.7046378 747.4 0046382 800 0046461 693.3 0046470 423.6 0046623 405.8 0046639 552.2 0046641 637.8 0046643 706.4 0049416 580.3 0052124 583.5 0052798 418.9 0057069 435.7 0058343 377.7 0058805 415.3 0058816 896.4 0063060 743.9 0063061 529.8 0063147 532.2 0063180 826.1 0063282 733.8 0063339 1016.4 0063341 587.5 0063414 617.9 0063519 447.4 0063572 849.6 0063621 660.2 0075059 520.9 0075417 648.1 0075499 863.3 0100803 699.5 0101872 570.3 0102226 1571 0102434 477.1 0103824 571.6 0104480 328.9 0104670 699.8 0104829 728.1 0200638 366.7 0201053 594.4 0201206 1009.4 0201362 733.2 0201480 734.6 0202209 410.2 0202934 605.5 0204325 619.3 0204474 628.8 0204877 521.2 0205208 728.2 0300404 724.9 0301204 457.1 0301751 629.4 0302116 873.7 0302245 402.4 0302303 638.1 0303038 872 0304139 643.8 0304437 573 0305866 926.5 0400278 673 0400506 1106.7 0400867 169.1 0402404 848.6 0402445 386.7 0402506 610.5 0403403 727.9 0403456 694.7 0035021 214.6 0036368 297.1 0049154 425.7 0S10434 913.3 0S10308 656.7 0S10747 154.9 0100257 250 0H12361 750 0063098 514.3 0S10364 465.9 0H12450 450 0063435 683 0031194 550 0S10844 759.6 0034425 567.9 0H11679 178.2 0010534 384 0063213 349.6 0063402 532.60 0043109 702.2 0063389 702.2 0 |
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-07-03 : 10:55:52
|
Do something like this:SELECT CAST(CAST('488.8' AS FLOAT) AS INT); Also, if you must, change those case expressions like in the example below, but it is not really required, you can just use the double-casting as I showed above without the case expression:CASE WHEN sa.[sa-fields__1] = '' THEN 0 ELSE CAST(CAST(( sa.[sa-fields__1] ) AS FLOAT) AS INT)END AS Fuel , |
|
|
|
|
|