I need to run a query on a massive (650 MB) data table in MS Access 2010. All the data types for the fields are text, but some fields contain all numbers, even though the data type is text. I therefore have to convert from text to long int in order to select, for example, all the records who have a value greater than 10 in a specified field. But whenever I do this, I get a type mismatch error, presumably due to the comparison of a numerical value of a text field. Here is the SQL for the query which should select from fields that are all of the text data type.
SELECT [Organization legal name], [Number of Group Practice members], City, State
FROM massivetable
WHERE Clng([Number of Group Practice members])>10
AND State='CT';
I have tried to convert the datatype of the field to long integer in design view, but the operation fails with a message saying there is not enough memory to perform the operation.
Can anyone show me how to fix the code above so that it selects all the records while values greater than 10, without throwing a type conversion error?
Use IsNumeric function:
SELECT [Organization legal name], [Number of Group Practice members], City, State
FROM massivetable
WHERE Iif(IsNumeric([Number of Group Practice members]), Clng([Number of Group Practice members]), 0) >10
AND State='CT';
Related
I want to format amounts to salary format, e.g. 10000 becomes 10,000, so I use to_char(amount, '99,999,99')
SELECT SUM(DECODE(e.element_name,'Basic Salary',to_char(v.screen_entry_value,'99,999,99'),0)) Salary,
SUM(DECODE(e.element_name,'Transportation Allowance',to_char(v.screen_entry_value,'99,999,99'),0)) Transportation,
SUM(DECODE(e.element_name,'GOSI Processing',to_char(v.screen_entry_value,'99,999,99'),0)) GOSI,
SUM(DECODE(e.element_name,'Housing Allowance',to_char(v.screen_entry_value,'99,999,99'),0)) Housing
FROM values v,
values_types vt,
elements e
WHERE vt.value_type = 'Amount'
this gives error invalid number because not all values are numbers until value_type is equal to Amount but I guess decode check all values anyway although what I know is that the execution begins with from then where then select, what's going wrong here?
You said you added decode(...), but it looks like you might have actually added sum(decode(...)).
You are converting your values to strings with to_char(v.screen_entry_value,'99,999,99'), so your decode() generates a string - the default 0 will be converted to '0' - giving you a value like '1,234,56'. Then you are aggregating those, so sum() has to implicitly convert those strings to numbers - and it is throwing the error when it tries to do that:
select to_number('1,234,56') from dual
will also get "ORA-01722: invalid number", unless you supply a similar format mask so it knows how to interpret it. You could do that, e.g.:
SUM(to_number(DECODE(e.element_name,'Basic Salary',to_char(v.screen_entry_value,'99,999,99'),0),'99,999,99'))
... but it's maybe more obvious that something is strange, and even if you did, you would end up with a number, not a formatted string.
So instead of doing:
SUM(DECODE(e.element_name,'Basic Salary',to_char(v.screen_entry_value,'99,999,99'),0))
you should format the result after aggregating:
to_char(SUM(DECODE(e.element_name,'Basic Salary',v.screen_entry_value,0)),'99,999,99')
fiddle with dummy tables, data and joins.
One of the client input'd large JSON PAYLOAD. 1000+ JSON PAYLOAD's ReceiptID field contains "NULL"/"some other word" instead of valid Blank/AlphaNumeric/Numeric.
Right now using, the following COALESCE & ISNULL based two query to narrow down for smaller subset. But what is the best quick approach to filter out these to new dirtyRowTable which would help to ask client to replay the same.
Using the following two query to find exact row that have bad data.
--Below SQL using ISNULL, returns all 1000 rows
Select top 1000 EventStoreId,
isnull(JSON_VALUE(payload,'$.ReceiptId'),0) ReceiptId
from dbo.EventStore order by 1 desc
--another SQL below using COALESCE, returns only 512 rows and error'd since 513th have value 'NULL'.
(Error: Msg 245, Level 16, State 1, Line 16
Conversion failed when converting the nvarchar value 'NULL' to data type int.)
Select top 1000 EventStoreId,
COALESCE(JSON_VALUE(payload,'$.ReceiptId'),0) ReceiptId
from dbo.EventStore order by 1 desc
COALESCE() -- although standard -- has the downfall that it evaluates the first argument twice. So when the first argument is non-trivial, ISNULL() is the better approach in SQL Server.
That said, the type of the ISNULL() expression is the type of the first expression. So, it is returning a string. The better way to write the code avoids implicit type conversions:
ISNULL(JSON_VALUE(payload, '$.ReceiptId'), N'0') ReceiptId
An old dbf have been succesfully ODBC-connected to Excel (2010). The following SQL query (through Microsoft Query) works as expected giving a single result.
Microsoft Query (Excel 2010) code
SELECT c.NDELNUM AS deliverynote, SUM(c.NQTY*a.CPREDEF2) AS Total
FROM DelCusL AS c, Article AS a
WHERE c.CREF = a.CREF AND ((c.NDELNUM=?))
GROUP BY c.NDELNUM
I am interested in getting also single value, but the following retuns NULL:
SELECT c.NDELNUM AS deliverynote, SUM(c.NQTY*(a.CPREDEF2+c.CPROP2)) AS Total
FROM DelCusL AS c, Article AS a
WHERE c.CREF = a.CREF AND ((c.NDELNUM=?))
GROUP BY c.NDELNUM
I guess this does not work because empty values are encountered in either a.CPREDEF2 or c.CPROP2. When an empty value is encounterd, I'd like it to be treated as 0. I have tried casting value functions available in Microsoft Query to not much avail.
Any idea on converting EMPTY values to 0s so that the operation is successful?
NQTY is a number and always non-empty. CPREDEF2 is treated as VARCHAR and can be EMPTY: when EMPTY it seems to be treated as NULL from other tests; CPROP2 is an alternative value to CPREDEF2 and can also be EMPTY, if filled it can be understood as number(like CPREDEF2). Both CPREDEF and CPROP2 are treated as VARCHAR rather than numerical values but when on their own are correctly multiplied and aggregated as numbers. (It fails when I try to add them together before the aggregate function).
Try to CAST before you add the values:
... * (CAST(a.CPREDEF2 AS DECIMAL(10,5))+CAST(c.CPROP2 AS DECIMAL(10,5))) ...
I'm a newbie learning my way around T-SQL using the AdventureWorks2012 database. I'm using SQL Server 2014, though a solution that would also work with 2008 would be great. I've been given the below exercise:
Write a query using the Sales.SpecialOffer table. Display the difference between the MinQty and MaxQty columns along with the SpecialOfferID and Description columns.
Thing is, MaxQty allows for null values, so I'm trying to come up with a real world solution for an output that doesn't involve leaving nulls in there. However, when I try to use coalesce to return 'No Max' (yes, I get that I could just leave NULL in there but I'm trying to see if I can figure this out), I get the message that the varchar value 'No Max' couldn't be converted to data type int. I'm assuming this is because MaxQty - MinQty as an int takes precedence?
select
specialofferid
, description
, coalesce((maxqty - minqty),'No Max') 'Qty_Difference'
from
sales.specialoffer;
Error:
Msg 245, Level 16, State 1, Line 135
Conversion failed when converting the varchar value 'No max' to data type int.
I thought about just returning a nonsense integer (0 or a negative) but that doesn't seem perfect - if return 0 I'm obscuring situations where the result is actually zero, etc.
Thoughts?
You just need to make sure that all the parameters of the COALESCE function call have consistent data types. Because you can't get around the fact No Max is a string, then you have to make sure that the maxqty - minqty part is also treated as a string by casting the expression.
select specialofferid
, description
, coalesce(cast(maxqty - minqty as varchar),'No Max') 'Qty_Difference'
from sales.specialoffer;
EDIT: A few more details on the cause of the error
Without the explicit cast, the reason why the COALESCE function attempts to convert the No Max string to an int can be explained by the following documented rule:
Data type determination of the resulting expression is different. ISNULL uses the data type of the first parameter, COALESCE follows the CASE expression rules and returns the data type of value with the highest precedence.
And if you check the precedence of the different types, as documented here, then you will see that int has higher precedence than varchar.
So as soon as you have a mix of data types in the call to COALESCE, SQL Server will try to convert all mismatching parameters to the data type with highest precedence, in this case int. To override that default behavior, explicit type casting is required.
I would use a case statement to so you can do stuff you want.
select specialofferid
, description
, CASE
WHEN maxqty is null THEN 'No Max'
ELSE (maxqty - minqty) 'Qty_Difference'
END
from sales.specialoffer;
SELECT logicalTime, traceValue, unitType, entName
FROM vwSimProjAgentTrace
WHERE valueType = 10
AND agentName ='AtisMesafesi'
AND ( entName = 'Hawk-1')
AND simName IN ('TipSenaryo1_0')
AND logicalTime IN (
SELECT logicalTime
FROM vwSimProjAgentTrace
WHERE valueType = 10 AND agentName ='AtisIrtifasi'
AND ( entName = 'Hawk-1')
AND simName IN ('TipSenaryo1_0')
AND CONVERT(FLOAT , traceValue) > 123
) ORDER BY simName, logicalTime
This is my sql command and table is a view table...
each time i put "convert(float...) part " i get
Msg 8114, Level 16, State 5, Line 1
Error converting data type nvarchar to float.
this error...
One (or more) of the rows has data in the traceValue field that cannot be converted to a float.
Make sure you've used the right combination of dots and commas to signal floating point values, as well as making sure you don't have pure invalid data (text for instance) in that field.
You can try this SQL to find the invalid rows, but there might be cases it won't handle:
SELECT * FROM vwSimProjAgentTrace WHERE NOT ISNUMERIC(traceValue)
You can find the documentation of ISNUMERIC here.
If you look in BoL (books online) at the convert command, you see that a nvarchar conversion to float is an implicit conversion. This means that only "float"-able values can be converted into a float. So, every numeric value (that is within the float range) can be converted. A non-numeric value can not be converted, which is quite logical.
Probably you have some non numeric values in your column. You might see them when you run your query without the convert. Look for something like comma vs dot. In a test scenario a comma instead of a dot gave me some problems.
For an example of isnumeric, look at this sqlfiddle