OK just joined stackoverflow today and I am very new to scripting, specially SQL
I am trying to run below to get a invoice num
SELECT
*
FROM
[Database].[dbo].[Table]
WHERE
[EPSINO] = 915561
AND [EPYEA4] = 2017
AND [EPSPYN] = EPSPYN
AND [EPCONO] = 100
AND [EPDIVI] = 400
GO
But I'm getting an error
Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the nvarchar value 'R8463 ' to data type int.
Understand I need a convert line in there somewhere but not sure where
PLEASE HELP
Thanks
Look at the datatypes defined on your columns. nvarchar columns need to be wrapped in single quotes. For example, EPSPYN is most likely an nvarchar column. I can't tell if the remaining columns are int or nvarchar. You need to look at your table definition and wrap in quotes appropriately.
select *
from[Database].[dbo].[Table]
where [EPSINO] = 915561
and [EPYEA4] = 2017
and [EPSPYN] = 'EPSPYN'
and [EPCONO] = 100
and [EPDIVI] = 400
GO
When you have a column of type char, you have to compare its value within single quotes. So if column EPSPYN was of type char, ensure that the value against it is written within single quote.
Select * From[Database].[dbo].[Table]
WHERE
[EPSINO] = 915561
and [EPYEA4] = 2017
and [EPSPYN] = 'EPSPYN' -- See this.
and [EPCONO] = 100
and [EPDIVI] = 400
GO
First find out the datatypes of that table, then build the WHERE clause. like VARCHAR, NVARCHAR and DATE Type enclose the Search text with Single Quotes.
Use Below command for the list of columns and its datatypes and build your Query.
SP_HELP [Database].[dbo].[Table]
Related
I'm developing a SQL Server 2016 SP2 stored procedure that generates a JSON string.
When I run it, this piece of code:
set #batch = (
select 1 as [BatchId], Value as [Name], (CAST(SYSDATETIMEOFFSET() as varchar(34))) as [Created]
from VariableData
where VariableData.ProductionOrderId = #productionOrderId and VariableData.VariableDataId = 10
for json path, WITHOUT_ARRAY_WRAPPER
);
With these data:
Generates the following error:
Error when converting the varchar value 'A001' to the data type int
But I don't understand why I get that message because I don't use VariableDataId column in the above sql code.
If there isn't any char in column VariableDataId (of type varchar(4)), I don't get any error.
Why do I get that error message?
But I don't understand why I get that message because I don't use VariableDataId column in the above sql code.
Actually you are, in the where condition VariableData.VariableDataId = 10. It is causing an implicit conversion to int for all values in that column.
Just modify the comparison to VariableData.VariableDataId = '10' (where '10' is a string/varchar), and the implicit conversion will not be performed.
You refer to VariableDataId column in line 4 of your query
where VariableData.ProductionOrderId = #productionOrderId and VariableData.VariableDataId = 10
You have two options. First, use the correct data type in the predicate.
where VariableData.ProductionOrderId = #productionOrderId and VariableData.VariableDataId = '10'
Second, convert column with TRY_CAST/TRY_CONVERT if you want to use INT value as the condition.
where VariableData.ProductionOrderId = #productionOrderId and TRY_CAST(VariableData.VariableDataId AS INT) = 10
I recommend the first option.
/****** Script for SelectTopNRows command from SSMS ******/
SELECT *
FROM Zuege as a
INNER JOIN [ResultateProzessModellZug_PF3_PF3_SimFV_2015_mNT_mSKT_L91_mZuschlaege_SKT] as b
ON a.ZuglaufID = b.Zuglaufid
LEFT JOIN [SysPue].[dbo].[VISUM_I_Knoten_Netz2015] as c
on [KnotenNummer] = Nr
LEFT JOIN [SysPue].[dbo].[LINIE_I_Richtung_Fahrplan2015] as d
on d.ZugNr = LineRouteName
WHERE Zugvariante = 216
and ModellzugID in (1,2,3,4,5,6,7,8,9,10)
and Qualitaetszug = 1
and Inland = 1
It's givin be back this ERROR:
Msg 245, Level 16, State 1, Line 2
Conversion failed when converting the nvarchar value '3255+' to data type int.
I am really new to SQL i don't have an idea what might be wrong. Thanks for your help.
Bob
Look at the datatypes of every column that is involved in a comparison. Comparisons are found in the ON clauses of your JOINS, and in the WHERE clause.
Any time you are comparing a string (varchar) to a number (integer or other numeric type), you need to CAST the number-type column as a string (varchar). Otherwise SQL Server will try to implicitly CAST the string as a number, and if it can't, it will raise the error you are seeing.
So, if for example you find that Zugvariante is a varchar, then you need to make 216 a string by putting it in single quotes like this:
WHERE Zugvariante = '216'
If d.ZugNr is a number and LineRouteName is a string, then you need to compare them both as strings:
CAST(d.ZugNr AS varchar(31)) = LineRouteName
Some of the columns contained in the WHERE clause are not an integer field.
To solve the problem, search the string as a string.
Example:
SELECT *
FROM Zuege as a
INNER JOIN [ResultateProzessModellZug_PF3_PF3_SimFV_2015_mNT_mSKT_L91_mZuschlaege_SKT] AS b ON a.ZuglaufID = b.Zuglaufid
LEFT JOIN [SysPue].[dbo].[VISUM_I_Knoten_Netz2015] AS c ON [KnotenNummer] = Nr
LEFT JOIN [SysPue].[dbo].[LINIE_I_Richtung_Fahrplan2015] AS d ON d.ZugNr = LineRouteName
WHERE Zugvariante = 216 -- (IF [Zugvariante] IS VARCHAR)
AND ModellzugID IN ('1','2','3','4','5','6','7','8','9','10') -- (IF [ModellzugID] IS VARCHAR)
AND Qualitaetszug = '1' -- (IF [Qualitaetszug] IS VARCHAR)
AND Inland = '1' -- (IF [Inland] IS VARCHAR)
I suspect the problem is with one of the JOINs being between an INT column and a NVARCHAR column, and the latter contains '3255+' that cannot be converter to an integer.
I suggest that you check the column types of:
a.ZuglaufID and b.Zuglaufid
[KnotenNummer] and Nr
d.ZugNr and LineRouteName
and make sure the types match.
The data contained in that column contains values that cannot be converted to an integer.
The Plus + value is causing the conversion to fail. Remove the + using some code to clean up the string. (Hint: Replace comes to mind.)
I am having an issue with my T-SQL query here:
UPDATE a
SET a.application =
SUBSTRING(b.postdata,
NULLIF(
PATINDEX('%[A-Za-z0-9][A-Za-z0-9][A-Za-z0-9][A-Za-z0-9][A-Za-z0-9][A-Za-z0-9][A-Za-z0-9][A-Za-z0-9]-[A-Za-z0-9][A-Za-z0-9][A-Za-z0-9][A-Za-z0-9]-%',b.postdata),0),36)
FROM [Lionel1].[dbo].[CSL_Logging_STAGE] a
INNER JOIN [Lionel1].[dbo].[CSL_Logging_STAGE] b ON b.Id = a.Id
WHERE
a.application LIKE '%000%'
AND a.postdata != ''
AND a.postdata IS NOT NULL
AND a.http_method = 'post';
I have tried a handful of different castings for the GUID that is stored in the application column and I have also restructured the query to have a subquery but that makes no difference.
The error is
Msg 8169, Level 16, State 2, Line 3
Conversion failed when converting from a character string to uniqueidentifier.
Anyone point me in the right direction?
Some of the VALUES I am trying to set to application are
NULL
0dab3646-a4ed-4b63-9441-11e27fa36df8
f6a5c360-635a-48e0-bf71-76ef0b865441
It looks like I needed to do a better job matching the appkey. It was grabbing extra data on a few of the compares adding quotes and extra characters which are not valid in a GUID.
I've 2 columns which I want to use a condition on. But I get this error message and my query is correct I'll come to that soon.
Msg 8114, Level 16, State 5, Line 1
Error converting data type varchar to float.
So this is the problem, I have a temp-table in which ID-number looks like this 9001011234 we can call it A, in the other one that I want to check with it looks like this 900101-1234 and this one for B this is the swedish format for Id-numbers.
So in my condition I want to check this to get the right amount and the correct result.
where A = B
The rest of the query is fine, when I remove this condition it gives me a result. It's just this one bit that is incorrect.
You have a VARCHAR format that can't be trivially transformed to a number. I'd use REPLACE(b,'-','') = a to fix the format, and let SQL Server take care of the rest.
Say:
where A = CAST(REPLACE(B, '-', '') AS float)
You are trying to compare values that are not the same datatype. eg Where 'one' = 1
You will need to convert one of the values to the same datatype as the other.
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