SQL Server CE scipt error - vb.net

Can someone help me, what's here wrong?
My query:
SELECT *
FROM SZ_2017
REPLACE(CONVERT(nvarchar,CAST ((szamla_brutto) AS MONEY),1), '.00',''),
REPLACE(CONVERT(nvarchar,CAST ((szamla_netto) AS MONEY),1), '.00','')
The error:
Error Code: 80040E14
Message : There was an error parsing the query. [ Token line number = 2,Token line offset = 8,Token in error = ( ]
Minor Err.: 25501
Source : SQL Server Compact ADO.NET Data Provider
Num. Par. : 2
Num. Par. : 8
Err. Par. : (
Thanks!

I suspect what you are trying to do is:
SELECT *
, REPLACE(CONVERT(nvarchar,CAST ((szamla_brutto) AS MONEY),1), '.00','')
, REPLACE(CONVERT(nvarchar,CAST ((szamla_netto) AS MONEY),1), '.00','')
FROM SZ_2017
If you want to get rid of decimal values you can do it easier like:
CAST(CAST(szamla_brutto AS INT) AS MONEY)
Remember to store data in an appropriate datatype, if you store monetary values, store it as MONEY or DECIMAL, NEVER as (n)varchar!
Additionaly, since convert and cast are functionally synonyms, pick one and stick with it, don't mix them together.

Related

Getting Error related to number format (java.lang.ClassCastException: java.math.BigDecimal cannot be cast to java.lang.String )

I am having a query as below
select SHIPMENT_COST_SEQNO as SHIPMENT_COST_SEQNO from shipment_cost where shipment_gid = 'A1.53005'
which gives me error as 'java.lang.ClassCastException: java.math.BigDecimal cannot be cast to java.lang.String '
Though the SHIPMENT_COST_SEQNO is a 5 digit number but still gives me above error.
This works fine with SQL Interface but when i try execute from application it gives me above error .
Any pointers would be appreciable.
Thanks
seems you need casting as your used in entity sting
select cast(SHIPMENT_COST_SEQNO as varchar(50))
as SHIPMENT_COST_SEQNO from shipment_cost where shipment_gid = 'A1.53005'

Importing OLAP metadata in SQL Server via linked server results in out-of-range date

Currently, I am trying to extract metadata from an OLAP cube in SQL Server (via a linked server) using this simple query:
select *
into [dbo].[columns_metadata]
from openquery([LINKED_SERVER], '
select *
from $System.TMSCHEMA_COLUMNS
')
But in the result set, there is a column named RefreshedTime with values 31.12.1699 00:00:00.
Because of this value, the query gives this error message:
Msg 8114, Level 16, State 9, Line 1 Error converting data type (null)
to datetime.
The problem is that I need to run the query without specifying the columns in the SELECT statement.
Do you know a trick to avoid this error?
I know you wanted not to have to mention the columns explicitly, but in case nobody can suggest a way to have it handle the 1699-12-31 dates, then you can fallback to this:
select *
into [dbo].[columns_metadata]
from openquery([LINKED_SERVER], '
SELECT [ID]
,[TableID]
,[ExplicitName]
,[InferredName]
,[ExplicitDataType]
,[InferredDataType]
,[DataCategory]
,[Description]
,[IsHidden]
,[State]
,[IsUnique]
,[IsKey]
,[IsNullable]
,[Alignment]
,[TableDetailPosition]
,[IsDefaultLabel]
,[IsDefaultImage]
,[SummarizeBy]
,[ColumnStorageID]
,[Type]
,[SourceColumn]
,[ColumnOriginID]
,[Expression]
,[FormatString]
,[IsAvailableInMDX]
,[SortByColumnID]
,[AttributeHierarchyID]
,[ModifiedTime]
,[StructureModifiedTime]
,CStr([RefreshedTime]) as [RefreshedTime]
,[SystemFlags]
,[KeepUniqueRows]
,[DisplayOrdinal]
,[ErrorMessage]
,[SourceProviderType]
,[DisplayFolder]
from $System.TMSCHEMA_COLUMNS
')

Divide by zero error when converting Access to SQL Server query

I am trying to convert an Access query to one that works in SQL server. The original query in Access works perfectly well (just terribly slow).
I only changed things slightly to make it compatible with SQL server instead of Access, like changing "NOW()" to "GETDATE()" and we can no longer divide aliases.
Running this query in SQL Server:
SELECT batches.[price-group],
[development].verifier,
Count([development].company) AS SENT,
Sum([order] *- 1) AS ORDS,
Count([development].company) / Sum([order] *- 1) AS PCT
FROM [development]
INNER JOIN batches
ON [development].batch = batches.batch
WHERE (( ( [development].[mail-date] ) < Getdate() - 50 ))
GROUP BY batches.[price-group],
[development].verifier
HAVING (( ( batches.[price-group] ) = 'pgb' ))
ORDER BY batches.[price-group],
[development].verifier,
Count([development].company) DESC;
Returns this error:
Msg 8134, Level 16, State 1, Line 1 Divide by zero error encountered.
Only real change, was like I said, in Access we could do this
[ords] / [sent] AS PCT
Any help will be appreciated, I'm not sure exactly why it isn't working! Removing the converted line above, does work in SQL server without any errors.
Thank you!
Use NULLIF():
Count([development].company) / NULLIF(Sum([order] * -1), 0) AS PCT

Data conversion or data mapping error HY000 in multi row concatenation in sub select in view

I have a sub-select as part of a view that generates a concatenated list of attributes from sub table. When doing a select from the view the data displays fine, but if it do a select in the concatenated list field I sometimes get the error Select or omit error on field.
AS400: V7R1M0
Main enitity:
MSGOCCID : CHAR 20
other fields.....
The detail entity fields (MSDPF):
MSGOCCID : CHAR 20
OCC : Integer 4
FIELDVAL : VARCHAR 128
I got the code for the sub select from StackOverflow and modified it for my situation. (I have also tried the recursive select but it very slow. The XMLSERIALIZE route is executable.)
To try and solve the problem it have added and removed REPLACE,COALESCE and TRIM of the value. I have added CCSID 1208 to the cast, changed the CHAR cast to VARCHAR... not change. (I have change CCSID 1208 to CCSID 37 at one stage to see if that will not solve it.)
If I run the select:
select MSGOCCID, COALESCE(DETLIST,' ')
from WBVIEW MSGP
where 1=1
If returns all the rows with not errors.
Sample output:
MSGOCCID: 2019020443165590
MSGDLIST: 14620, 1, C20180914023575582, 4179792C, C20180914023575582, 4179792C, WIPSTOCK, REMOVE, ROU07561
But if I use:
select MSGOCCID, COALESCE(DETLIST,' ')
from WBVIEW MSGP where 1=1 and DETLIST like '%4179792C%'
It fails with on the IBM backend:
Data conversion or data mapping error.
Select or omit error on field MapXmlData(Cast(Concat(',
',Strip(Replace(Cast(P6MSDPF_4.FIELDVAL AS VarChar(128) CCSID
1208),'-'), Both,' ')) AS VarChar(260) CCSID 1208)) member
COMMSWKBL1. Select/omit error on member COMMSWKBL1.
Short version of the view:
CREATE VIEW WBVIEW (
MSGOCCID ,
DETLIST )
AS
(SELECT MSG.MSGOCCID,
FROM P6DEVCDB00.P6MSGPF MSG
LEFT OUTER JOIN LATERAL
(SELECT XMLSERIALIZE(XMLAGG(XMLTEXT(CONCAT(', ',TRIM(REPLACE(REPLACE(REPLACE(FIELDVAL,',','-'),'>','-'),'<','-'))))) AS VARCHAR(4096) CCSID 1208) AS DETLIST
FROM MSDPF MSGDET
WHERE COALESCE(MSGDET.MSGOCCID,' ') != ' ' and
MSG.MSGOCCID = MSGDET.MSGOCCID AND
MSGDET.FIELDVAL != ''
GROUP BY MSGDET.MSGOCCID)MSGDLIST ON 1=1);
Character concatenation should not be a problem (except if it gets confused somewhere with the CCSIDs). I replace the <>, form the detail for in case that may interfere with xml functions... I have catered for Null and '' values ....
I have search lots of threads on lots of different sites and have run out of ideas.
Any suggestions will be appreciated.
I'm just guessing a quick comment answer here. Those left outer joins can introduce nulls into the mix you can simply coalesce them in the where clause ...
select MSGOCCID, COALESCE(DETLIST,' ')
from WBVIEW MSGP where
coalesce(DETLIST,' ') like '%4179792C%'
IRL I would use locate because it just feels faster when it runs.
select MSGOCCID, COALESCE(DETLIST,' ')
from WBVIEW MSGP where
locate('4179792C' ,coalesce(DETLIST,' ')) <> 0
HY000 is a CLI (ODBC) General error, the explaination in the IBM documentation is:
An error occurred for which there is no specific SQLSTATE and for which no implementation defined SQLSTATE is defined. The error message returned by SQLError in the argument szErrorMsg describes the error and its cause.
Since this is probably not something you are coding by hand, you probably do not have the opportunity to issue SQLError, but I suspect that the message you quoted in your question is the result of some internal process retrieving that error. so the problem should be:
Data conversion or data mapping error.
Select or omit error on field MapXmlData(Cast(Concat(',
',Strip(Replace(Cast(P6MSDPF_4.FIELDVAL AS VarChar(128) CCSID 1208),'-'), Both,' ')) AS VarChar(260) CCSID 1208)) member COMMSWKBL1. Select/omit error on member COMMSWKBL1.
I don't know your files, but I am guessing that there is bad data in the file and that COMMSWKBL1 is a select logical that is accessed to process the selection when you add the DETLIST like '%4179792C%' to your query. Your task is to find out which file is associated with member COMMSWKBL1, and see if you can determine why a Select or omit error would be occurring on that file.
It is fine. I have modified the view to select both the header and detail, and search on both entity entries (linked on the key MSGOCCID) and do a DISTINCT on the key MSGOCCID. It seems a little bit slower than the view the question is about but is working fine. Thank you for you time.

Surrounding Areas SQL

I'm trying to get a query to work which returns a list of locId's from the database when fed a long and a lat.
Here's the sql:
eg: lat = "-37.8333300" : lon = "145.000000" : radius = (5 * 0.621371192) ^ 2
SELECT locId,longitude,latitude FROM tbliplocations WHERE (69.1*([longitude]- "&lon&") * cos("&lat&"/57.3))^2 + (69.1*([latitude]- "&lat&"))^2 < "&radius
Here's the error I receive:
The data types float and int are incompatible in the '^' operator.
I'm unsure of a workaround, can anyone point me in the right direction?
Answer:
Using SQL Server 2008 R2
SELECT city FROM tbliplocationsnew WHERE POWER((69.1*([longitude]- "&lon&") * cos("&lat&"/57.3)),2) + POWER((69.1*([latitude]- "&lat&")),2) < "&radius
Not sure what database you use, but I think that "^2" in SQL does not mean "squared" like in maths. You should use a math "power" function, like POWER(number,2) in SQL Server (since you use VB maybe you use SQL Server ?)
You need to have two of the same data type it's saying. SQL thinks "5" is an int. So, you should be able to trick it into treating it as a float, by putting "5.0" instead.