SELECT Only If Value Is Numeric From Varchar Field
Oct 24, 2001
I have a field with State and Zip (ie; CA94526) which is a varchar field. I have lots of data that is invalid and need SELECT all records that the right(myfield,5) IS NOT Numeric. Can this be done?
how i convert varchar sal field to numeric in query select sum(sal) from emp1 error:the sum or average aggregate operation cannot take a varchar data type as an argument.
I have a field defined as varchar(8) but this field should not contain any letters, needs to be only numbers. How can I validate the data if it contains only numbers? Any ideas?
I am putting a SELECT statement together where I need to evaluate a results field, to determine how the color indicator will show on a SSRS report. I am running into a problem when I try to filter out any non-numeric values from a varchar field, using a nested CASE statement.
For example, this results field may contain values of '<1', '>=1', '1', '100', '500', '5000', etc. For one type of test, I need a value of 500 or less to be shown as a green indicator in a report, and any value over that would be flagged as a red. Another test might only allow a value of 10 or less before being flagged with a red.
This is why I setup a CASE statement for an IndicatorValue that will pass over to the report to determine the indicator color. Using CASE statements for this is easier to work with, and less taxing on the report server, if done in SQL Server instead of nested SSRS expressions, especially since a variety of tests have different result values that would be flagged as green or red.
I have a separate nested CASE statement that will handle any of the values that contain ">" or "<", so I am using the following to filter those out, and then convert it to an int value, to determine what the indicator value should be. Here is the line of the script that is erring out"
case when (RESULT not like '%<%') or (RESULT not like '%>%') then CASE WHEN (CONVERT(int, RESULT) between 0 and 500) THEN '2' ELSE '0'
The message I am getting is: Conversion failed when converting the varchar value '<1' to data type int.
I thought a "not like" statement would not include those values for converting to an int, but that does not seem to be working correctly. I did also try moving the not to show as "not RESULT like", and that did not change the message.
How I can filter out non-numeric values before converting the rest of the varchar field (RESULT) to int, so that it is only converting actual numbers?
I am trying to setup an indicator value for an SSRS report to show green and red values on a report, based on the NRESULT value. The problem I am facing is that I have several different CASE statements that have the same logic, and they are processing just fine. NRESULT is a decimal field, so no conversion should be necessary. I do not know why I am getting the "Arithmetic overflow error converting varchar to data type numeric." error message.
Below is the CASE statement where the error is occurring. It is in the part of the ELSE CASE. The first CASE works just fine when the ELSE CASE is commented out. If I also change the ELSE CASE statement to say "else case when LEFT(NRESULT,1) = '-' then '0'", then it processes fine, too, so it has to be something I am missing something in the check on negative values. I do need the two checks, one for positive and one for negative values, to take place.
case when LEFT(NRESULT,1) <> '-' then --This portion, for checking positive values, of the CASE statement works fine. CASE WHEN LEFT(ROUND(NRESULT,2),4) between 0.00 and 0.49 THEN '2' --Green ELSE CASE WHEN LEFT(ROUND(NRESULT,2),4) > 0.49 THEN '0' --Red ELSE '3' --White END END else case when LEFT(NRESULT,1) = '-' then --This portion, for checking negative values, of the CASE statement is producing the conversion error message.
[code]....
I checked the NRESULT field, and there are not any NULL values in there, either.
Hi all,I've set up a page with a gridview, and I'm trying to create a query based on three parameters and to display all records when the page loads.I have created the first two parameters no problem, but I'm having problems with the last one.The parameter is to be populated by a dropdownlist, where the selectedvalue is numeric. I have done a search and found that I can't use the '%' wildcard as it is for a string data type.I have read somewhere that to get around this I can use the CHAR() function to convert to a character, I have tried this without success.When creating the ControlParameter the datatype gets set to string, which I think is not working becuase the input has to be an integer for it to conver to char, am I right?This is the query, and it is the @Application parameter that I'm having this problem with. SELECT dbo.Issue.IssueID, dbo.Issue.ReportedBy, dbo.Issue.ShortDescription, dbo.Issue.DateReported, dbo.Issue.Status, dbo.Priority.Description AS Priority, dbo.Application.Application FROM dbo.Issue INNER JOIN dbo.Priority ON dbo.Issue.Priority = dbo.Priority.PriorityCode INNER JOIN dbo.Application ON dbo.Issue.Application = dbo.Application.ApplicationID WHERE (dbo.Issue.Status LIKE '%' + @Status) AND (dbo.Issue.AssignedTo = @AssignedTo) AND (CHAR(dbo.Application.ApplicationID) LIKE '%' + @Application) ORDER BY dbo.Priority.PriorityCodeHas anyone managed to do this another way?Thanks
I have imported data from excel file. When data came to SQL table, the typeof AMOUNT column was varchar. I tried to convert and cast amount type ofamount column to number type but it does not allow me to convert.What is the best way of importing data into SQL and type stays the same asit was in excel file ?Or anyone has any better solution, please let me.Thanks.
Hello All, I am querying from a table which has all varchar values. I want to display the result in graph and hence would need the varchar to be converted to Decimal. But I am getting the error : Error converting data type varchar to Numeric. Here is the SELECT code: Can you please help me do this? I'll post the script in the next post in 10 minutes....
I need create a field to store tax rate. I need only 2 decimal points. I defined the field as decimal, precision=5 and scale=2. Does it mean that it can hold value from 0.00 to 999.99?
i have a huge stored procedure abt 500 lines..and i am calling this sp from an asp.net page...thn i got this error - error converting varchar to numeric - and am trying to debug...is there any way we can find out where the error is coming from...like aproxly which line number..etcor do i have to go through each line manually and see where i am doing the conversion....
Not sure how I can convert varchar to numeric values in SQL Server? Trying to do so in Design and get error: Unable to modify table. ODBC error: [Microsoft][ODBC Server Driver][SQL Server] Error converting data type varchar to numeric. Any suggestions?
Im running a DTS package that converts data in a fixed width text file to SQL table. The package runs successfully, but when I go to dump 60,000+ records in the table I get this error:
"Error Converting data type varchar to numeric."
Is there a way that I can isolate where the offending values are located so I can manually correct them?
Hi again all,I have a small issue. Here's an example dataset :F1 F2 F31 0.58 Hi2 0.70 Hello3 Fail Bye4 <Null> HiWhen I write this statement :SELECT SUM(CONVERT(DECIMAL(16,8),F2)) MySumFROM T1WHERE IsNumeric(IsNull(F2,'X'))=1I get "Cannot convert a Varchar value to Numeric" error. From what Iunderstand, it somehow tries to convert to a decimal(16,8) BEFORE filteringthe nulls and the non-numeric out. (Keep in mind that the actual table hasover 1.5Million records).Any idea on how to get around that ?Thanks,Michel
I am trying to change a value in a table. For instance the value is now 3. If I try to change the value to say 2 or 4 I get and error converting data type varchar to numeric. So wazzagoingon?
I have a varchar(len=9) field that I want to cast as numeric(9,2). Some of the field values are null but some have valid values (ie 1.00 or .05).
I am selecting data from one table and then updating a different table.
I have a select statement within an insert statement and then an update statement. I've tried a cast statement but not successfully. I get the following error.
[Execute SQL Task] Error: Executing the query "Exec sp_ESTLoadPOData" failed with the following error: "Error converting data type varchar to numeric.". Possible failure reasons:
Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.
I have 2 varchar fields on MS 2005 table First field is date and format is 080118(YYMMDD) and second is salary field like 00002000(positive) and 00002000- (negative) how can I move them to date and numeric fields on another table....
DB is SQL 2000 and I'm trying to convert a field datatype from varchar to numeric where the data is hours:minutes:seconds (05:52:12), but I run into errors with EM and scripts that the db cannot convert those datatypes. I've tried the cast function with same results ... any solutions?? I need to run SUM(), AVG(), etc, against the data and can't with VARCHAR type. Thanks all -Greg
I have a sql code that I am having some difficulty with. All I'm trying to do is get each department and sum the amount by month based on the date in the table. I am getting "error converting data type varchar to numeric".
Code: select DEPARTMENT, CASE WHEN ((DATE >= '06/01/2014') AND (DATE <= '06/30/2014')) THEN (cast(sum(Amount) as decimal(10,2))) ELSE '' END AS 'JUNE', CASE WHEN ((DATE >= '07/01/2014') AND (DATE <= '07/30/2014')) THEN (cast(sum(Amount) as decimal(10,2))) ELSE '' END AS 'JULY' from L27_PHAROS_DETAIL_DATA WHERE Department = 'TECHNOLOGY SERVICES' GROUP BY DEPARTMENT, DATE ORDER BY DEPARTMENT
I try to JOin to tables: Enterprise has a decimal(5,3) and COHIER has varchar 5. All I am getting is an error on converting data. How can I fix this problem.
SELECT a.Security_Value, b.Enterprise_Number, b.Enterprise_Description FROM dbo.COHIER a INNER JOIN dbo.Enterprise b ON a.Security_Value = b.Enterprise_Number
Yes again a topic like this, I couldn't get an answer for my problem so I started a new topic.
Im getting this error for these lines:
$registreer = "INSERT INTO Gebruikerstelefoon (gebruikersnaam, telefoonnummer, volgnr) VALUES ('".$_SESSION['gebruikersnaam']."', '".$_POST["telefoonnummer"]."', '')"; mssql_query($registreer) or die("Fout bij toewijzen telefoonnummer.");
'telefoonnummer' (phonenumber) is nummeric, users can insert their phonenumber in a input field (text) and register along.. ( this is a second query for the column phonennumber) though this doesn't work..
I am having difficulty with my sql and I am not sure what do to. I am new to this. Any help you can give would be helpful.
Here is the statement that is giving me trouble. If I take this out of my Sql query everything runs fine.
CASE clm_att1 WHEN 'NG' THEN (clm_sppo)*(clio_fee04/100) WHEN 'NA' THEN '0.00' WHEN 'AF' THEN (clm_sppo)*(clio_fee04/100)*.65 ELSE '' END as AccessFeeFinal,
CLM_H30 = Case When CLM_H30 = 0.00 Then clm_sppo else clm_H30 End,
clm_prod = CASE WHEN CLMS_sku = 'NEGO' THEN 'NEG' WHEN CLMS_sku = '% OF CHGS' THEN 'NEG' ELSE clm_prod End,
"clm_nego" = CASE WHEN CLM_ATT1 = 'NA' THEN 0.00 WHEN CLM_ATT2 = 'NA' THEN 0.00 WHEN CLM_ATT3 = 'NA' THEN 0.00 WHEN CLM_ATT4 = 'NA' THEN 0.00 ELSE clm_nego END,
"clm_sppo" = CASE WHEN CLM_ATT1 = 'NA' THEN 0.00 WHEN CLM_ATT2 = 'NA' THEN 0.00 WHEN CLM_ATT3 = 'NA' THEN 0.00 WHEN CLM_ATT4 = 'NA' THEN 0.00 ELSE clm_sppo END,
clm_1e, clm.CLM_ATT1 as Note,
CASE clm_att1 WHEN 'NG' THEN (clm_sppo)*(clio_fee04/100) WHEN 'NA' THEN '0.00' WHEN 'AF' THEN (clm_sppo)*(clio_fee04/100)*.65 ELSE '' END as AccessFeeFinal,
CLM_ATT2, CLM_ATT3, clio_fee04 as "ACCESSFEEIMPACT",
"(clm_sppo/CLM_TCHG) * 100" = CASE WHEN CLM_ATT1 = 'NA' THEN 0.00 WHEN CLM_ATT2 = 'NA' THEN 0.00 WHEN CLM_ATT3 = 'NA' THEN 0.00 WHEN CLM_ATT4 = 'NA' THEN 0.00 ELSE (clm_sppo/CLM_TCHG) * 100 END,
clio_wrk1 as MAS90#
FROM dbo.clm clm Join dbo.cli cli ON clm.clm_clir = cli.cli_id1 JOIN dbo.clip clip ON cli.cli_id1 = clip.clip_id1 JOIN dbo.clio clio ON cli.cli_id1 = clio.clio_id1 INNER JOIN dbo.clms clms ON clms.clms_id = clm.clm_id1
WHERE (clip.clip_prd IN ('NEG', 'ADV')) AND (clm_prod = 'ADV' OR clms_sku = 'NEGO' OR clms_sku = '% OF CHGS')AND (CLM_STADES IN ('DONE','DUPO','DUPL'))and clm_adjto = '' AND CLM_TCHG > 0. and (clio.clio_type = 'AC')
I am having problems with a statment that I am writing. The prop_uac is a alpha field and prop_disc is a numeric field. What I was trying to do is when the alpha field is not populated automatically pull from the numeric field. Then I wanted to put all of the results into one column. Can this be done? below is my statement. I hope you can help me.
Prop_uac = CASE WHEN prop_uac = '' THEN prop_disc else prop_uac End
hello everyone... i have some problem to with my trigger.. after execute the trigger.. i got this errro "Msg 8114, Level 16, State 5, Procedure trg_InsertPVReadingMeter, Line 14 Error converting data type varchar to numeric."
below is my command sql table PVReadingMeter
quote:USE [PVMC Database] GO /****** Object: Table [dbo].[PVReadingMeter] Script Date: 03/25/2008 01:18:06 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[PVReadingMeter]( [PV_reading_id] [uniqueidentifier] NOT NULL CONSTRAINT [DF_PVReadingMeter_PV_reading_id] DEFAULT (newid()), [PV_reading_date_time] [datetime] NOT NULL, [PV_reading_meter] [decimal](8, 2) NOT NULL, [PV_normalized_monthly_yield] [float] NOT NULL, [PV_normalized_monthly_energy_yield] [float] NOT NULL, [PV_application_id] [uniqueidentifier] NOT NULL CONSTRAINT [DF_PVReadingMeter_PV_application_id] DEFAULT (newid()), CONSTRAINT [PK_PVReadingMeter] PRIMARY KEY CLUSTERED ( [PV_reading_id] ASC )WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY] ) ON [PRIMARY]
GO USE [PVMC Database] GO ALTER TABLE [dbo].[PVReadingMeter] WITH CHECK ADD CONSTRAINT [FK_PVReadingMeter_Photovoltaic] FOREIGN KEY([PV_application_id]) REFERENCES [dbo].[Photovoltaic] ([PV_application_id]) ON UPDATE CASCADE ON DELETE CASCADE
below is my trigger
quote:set ANSI_NULLS ON set QUOTED_IDENTIFIER ON go
CREATE Trigger [trg_InsertPVReadingMeter] on [dbo].[PVReadingMeter] for insert as begin
SELECT @PVReadingMeter = (SELECT PV_reading_meter + '' FROM Inserted) SELECT @PVNormalizedMonthlyYield = (SELECT PV_normalized_monthly_yield + '' FROM Inserted)
-- Print the name of the new author
PRINT 'The new photovoltaic reading meter"' + @PVReadingMeter +'" is added.' PRINT 'The photovoltaic normalized monthly yield is "' + @PVNormalizedMonthlyYield +'"'
end;
now, this is command to insert the values into PVReadingMeter table using trigger that has been created....