How to correct an error in SELECT statement - sql-server

I've coded on SQL Server 2014:
create table CriticalStop
(
SAP_ID int not null,
Affected_ID char(15) not null,
FnLoc_ID char(20) not null,
MalFn_date date,
MalFn_time time,
MalFnHour_dur float,
MalFnMajor char(15),
primary key(SAP_ID)
);
go
insert into CriticalStop(SAP_ID, Affected_ID, FnLoc_ID, MalFn_date, MalFn_time, MalFnHour_dur, MalFnMajor)
values (10045929, 'TN.211-HC1', 'TN.211-HC1', convert(date,'20190101'), convert(time,'18:29:00.290'), 3.52, 'Process')
They was okay. but when I used statement below, SQL Server 2014 raised an error
select
SAP_ID,
Affected_ID,
FnLoc_ID,
MalFn_date+3 as MalFn_delay,
MalFn_time,
MalFnHour_dur,
MalFnMajor
from
CriticalStop;
Error:
Msg 206, Level 16, State 2, Line 2314
Operand type clash: date is incompatible with int.
Line 2314 was pointing to SELECT statement above. I don't understand why this error happens on line MalFn_date+3 as MalFn_delay. I remove +3, the result is okay, but I need to change its date. How can I get it working with +3.

It says what the problem is right in the error message, date is incompatible with int. You're trying to add an integer to a date field.
MalFn_date+3 as MalFn_delay,
You need to convert the integer to a time range so that it can be added to the date. SQL has no idea what 3 represents; seconds, minutes, days?
You can use the DATEADD() function to do this in SQL Server. For example, if you wanted to add 3 days,
DATEADD(day, 3, MalFn_date)

Related

error converting datatype conversion on single vendoritemnumber in duplicate rows

Sql server query shows error :
Msg 8114, Level 16, State 5, Line 1
Error converting data type varchar to numeric.
Here is the query:
WITH employee AS (
SELECT distinct vendoritemnumber,VendorItemId,
VendorItemDescription,
VendorItemDescriptionAlias,VendorId,BrgItemId,itemconversionFactor,
orderbyuomid,pricebyuomid,vendorcasedescription,manufacturernumber,skunumber,[weight],
averageweight,currentprice,taxable,[status],createddate,inactivedate,lastpurchaseddate,
lastupdatedby,lastupdateddate,vendorpercent,vendorfreight,brandid,pack,size,inventorycategoryid,
binlocation,inventorylocation,inventorystatus,physicalinventoryconversionfactor,stateswhereused,
conceptwhereused,priceupdatedate,size_uom,pack_uom,isdeleted,'' as id,'' itemid,'' as oldprice,
'' as newprice,'' as dateupdated,'' as weekenddate,'' as lastpurdate
FROM VendorItems WHERE BrgItemId=6056)
SELECT * FROM employee
UNION ALL
SELECT '' as ven,v.VendorItemId,
v.VendorItemDescription,v.VendorItemDescriptionAlias,v.VendorId,v.BrgItemId,v.itemconversionFactor,
v.orderbyuomid,v.pricebyuomid,v.vendorcasedescription,v.manufacturernumber,v.skunumber,v.[weight],
v.averageweight,v.currentprice,v.taxable,v.[status],v.createddate,v.inactivedate,v.lastpurchaseddate,
v.lastupdatedby,v.lastupdateddate,v.vendorpercent,v.vendorfreight,v.brandid,v.pack,v.size,v.inventorycategoryid,
v.binlocation,v.inventorylocation,v.inventorystatus,v.physicalinventoryconversionfactor,v.stateswhereused,
v.conceptwhereused,v.priceupdatedate,v.size_uom,v.pack_uom,v.isdeleted,ph.id,ph.itemid,ph.oldprice,
ph.newprice,ph.dateupdated,ph.weekenddate,brg.lastpurchasedate
from [dbo].[VendorItems] v join PriceHistory ph
on ph.ItemId=v.VendorItemId join brgitems brg on
brg.brgitemid=v.BrgItemId
WHERE v.BrgItemId=6056
group by vendoritemnumber,v.VendorItemId,v.VendorItemDescription,v.VendorItemDescriptionAlias,v.VendorId,v.BrgItemId,v.itemconversionFactor,
v.orderbyuomid,v.pricebyuomid,v.vendorcasedescription,v.manufacturernumber,v.skunumber,v.[weight],
v.averageweight,v.currentprice,v.taxable,v.[status],v.createddate,v.inactivedate,v.lastpurchaseddate,
v.lastupdatedby,v.lastupdateddate,v.vendorpercent,v.vendorfreight,v.brandid,v.pack,v.size,v.inventorycategoryid,
v.binlocation,v.inventorylocation,v.inventorystatus,v.physicalinventoryconversionfactor,v.stateswhereused,
v.conceptwhereused,v.priceupdatedate,v.size_uom,v.pack_uom,v.isdeleted,ph.id,ph.itemid,ph.oldprice,
ph.newprice,ph.dateupdated,ph.weekenddate,brg.lastpurchasedate
I want to show single vendoritemnumber in duplicate rows..Any suggestion??
Where is the error??
SQL Server cannot convert an empty string to NUMERIC data type. For example, the following will yield the same error:
SELECT CONVERT(NUMERIC, '')
Replace empty strings ('') with 0 or NULL.
#manoj .Just FYI to know exact error point , double click on the error in message window .It shows the exact line of code on error, Also try to check if any empty values are there for that column and as mentioned by #Serge SQL throws error while any data type is converted to numeric and if its value is empty. Best solution is to add WHERE clause and filter those cases.

Stored Procedure Date time error

I wrote an sp basically to check the diff between dates If the diff is either 4 or 2 then the sp runs and raises an error it not.
CREATE PROCEDURE AML_DATECHECK
#ALERTDAY VARCHAR
AS
IF((SELECT "DAY_DESC" =
CASE WHEN DATEDIFF(DAY,CONVERT(VARCHAR,"BUSINESS_DAY",120),
CONVERT(VARCHAR,CONVERT(VARCHAR,'#ALERTDAY',121),120))= 4
THEN 'BUSINESS DAY IS FRIDAY'
WHEN DATEDIFF(DAY,CONVERT(VARCHAR,"BUSINESS_DAY",120),
CONVERT(VARCHAR,CONVERT(VARCHAR(25),'#ALERTDAY',121),120)) = 2
THEN 'BUSINESS DAY IS' + datename(dw,"BUSINESS_DAY")
END
FROM [US_AML_APP].[dbo].[SAM_APP_USER_PROCESS] WHERE
[PROCESS_NAME]='AML_SAM_dailyProcess') IS NULL )
RAISERROR('DATE CHECK FAILED',11,1)
ELSE
RAISERROR('DATE CHECK PASSED',12,2)
RETURN
When I try to pass a parameter like here
exec AML_DATECHECK #ALERTDAY='2015-11-23'
it fails.
I have also tried all the date formats such as 2015/23/11 and tried giving all the datatypes but the execution fails with:
Conversion failed when converting date and/or time from character
string.
Could anyone help me?

SQL Server Cast varchar column value '1218300.00' as int

A large value in a column has caused my SQL to throw error msg:
Error converting data type varchar to numeric
I have isolated this to a particular row. Here is a simplified script that "works":
SELECT
MtgeLoanAmount
, CAST(convert(numeric(15,2),'1218300.00') as int) as TrialValue
FROM dbo.Processed_VA_From_Excel
where FipsStateCode='06'
and FipsCountyCode='013'
and GuarantyAmount = '304575'
which returns results as pasted here:
So when I try to "generalize" my test by adding a 3rd column as follows it fails to convert:
SELECT
MtgeLoanAmount
, CAST(convert(numeric(15,2),'1218300.00') as int) as TrialValue
, CAST(convert(numeric(15,2),MtgeLoanAmount) as int)
FROM dbo.Processed_VA_From_Excel
where
FipsStateCode='06'
and FipsCountyCode='013'
and GuarantyAmount = '304575'
returns this:
Msg 8114, Level 16, State 5, Line 1
Error converting data type varchar to numeric.
This may work for you:
SELECT
MtgeLoanAmount,
CONVERT(INT, ROUND(MtgeLoanAmount, 0)) AS MtgeLoanAmountNoCents
FROM
dbo.Processed_VA_From_Excel
WHERE
FipsStateCode = '06' AND
FipsCountyCode = '013' AND
GuarantyAmount = '304575'

convert string date to date type in sql server 2012

In a school website, I want to enable the admin to filter students based on date range when they were born. Dates in my tblStudent are stored as strings, so I cannot use:
SELECT ts.Name from tblStudent ts WHERE ts.BirthDay>'1367/01/31' AND ts.BirthDay<'1377/01/31'
I have saved dates (Jalali Format) in database table tblStudent. I need to do comparison based on dates. So I need to convert date strings to date type in sql server. To this purpose I used:
SELECT convert(date,tblStudent.BirthDay) from tblStudent
However,It stops after 27 results with the following error
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
I have the following date strings in my tblStudent table.
1379/09/01
1375/04/20
1378/03/02
1378/03/21
1378/04/18
1378/04/18
1378/05/05
1375/04/20
1379/01/03
1378/03/01
1370/09/09
1378/03/22
1375/09/15
1379/09/01
1379/09/10
1375/04/08
1375/05/06
1370/09/09
1379/10/10
1375/04/10
1375/11/01
1375/04/04
1375/08/11
1375/05/05
1376/09/19
1375/12/12
1376/01/13
1375/15/10
1375/04/14
1375/04/04
1375/05/14
1374/11/11
1375/05/30
1375/05/14
1377/12/13
1377/02/31
1377/12/14
1377/01/13
1375/05/31
1377/11/05
1377/07/05
1375/05/31
1377/03/01
1377/04/01
1377/05/02
1377/05/04
1377/03/03
1377/01/14
1377/05/30
1377/04/31
1375/05/30
1376/06/12
1375/12/10
1377/08/14
1377/03/04
1375/04/08
1375/07/18
1375/08/09
1375/09/12
1375/11/12
1376/12/12
1375/01/02
1375/05/09
1375/04/09
1376/01/01
1375/01/30
1377/04/04
1375/05/23
1375/05/01
1377/02/01
1367/12/05
1375/05/31
1373/03/29
1373/03/03
1375/05/05
Is there a way to convert these string dates to date type and then compare them with some query? For example, such a query can be:
SELECT ts.Name from tblStudent ts where ts.BirthDay>'1375/05/31'
I think you can make them ints and compare them:
SELECT ts.Name
FROM tblStudent ts
WHERE CONVERT(INT,REPLACE(ts.BirthDay,'/','') > 13670131
AND CONVERT(INT,REPLACE(ts.BirthDay,'/','') < 13770131
Or for your second example:
SELECT ts.Name
FROM tblStudent ts
WHERE CONVERT(INT,REPLACE(ts.BirthDay,'/','') > 13750531
This would work because having the order Year-Month-Day will ensure that the int representation of a later time will be greater than the int representation of an earlier time.
I really do not know if this is the best idea, but it is an idea of how to do it. After all you would be using a conversion.
From C# you have a few options:
If your input is string:
var dateInt = Int32.Parse(dateString.Replace("/",""));
If your input is Date then:
var dateInt = Int32.Parse(dateValue.ToString("yyyyMMdd"));
You could also pass the string itself in the db and let the db do the work for you :
DECLARE #Date AS VARCHAR(10)
SET #Date = ...--This will be filled with the inputed string
DECLARE #DateINT AS INT
SET #DateINT = CONVERT(INT,REPLACE(#Date,"/",""))

Entity Framework stored procedure error with DateTime

Hi
I am getting an error
'Conversion failed when converting date and/or time from character string'
When calling a stored procedure using EF4.
I am passing from my c# 2 DateTimes like this
#FromDate='2010-11-10 12:30:14.2558729'
#ToDate= '2010-11-10 12:30:15.1169590'
How can I prevent this error?
if I do the following in my UI it works
FromDate = new DateTime (SelectedFromDate.Year,SelectedFromDate.Month,SelectedFromDate.Day),
ToDate = new DateTime(SelectedToDate.Year, SelectedToDate.Month, SelectedToDate.Day),
Thanks for any suggestions
Reduce the number of digits on the fractional seconds to three. Try it out...
What you are trying to do:
select convert(datetime,'2010-11-10 12:30:14.2558729')
-- ^^^^^^^
OUTPUT:
-----------------------
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting datetime from character string.
Using only 3 decimal digits:
select convert(datetime,'2010-11-10 12:30:14.255')
-- ^^^
OUTPUT:
-----------------------
2010-11-10 12:30:14.257
(1 row(s) affected)

Resources