Entity Framework stored procedure error with DateTime - sql-server

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)

Related

How to correct an error in SELECT statement

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)

Convert String column to DataTime

In my .net winform application i m importing excel data to SQL server. in my excel sheet i have 'STRT_TM' column containing dates in 03/01/2017 21:33:22 format. i want to do explicit conversion because in implicit conversion convert function is unable to determine whether the date is dd/mm/yyyy or mm/dd/yyyy.
In Sql Server i tried to convert a string to date. here is the code
select convert(smalldateTime,SUBSTRING('03/01/2017 14:05:34',7,4)+
SUBSTRING('03/01/2017 14:05:34',1,2) + SUBSTRING('03/01/2017 14:05:34',4,2))
the result is 2017-03-01 00:00:00
but when i try to include time part it gives error.
here is the code.
select convert(smalldateTime,SUBSTRING('03/01/2017 14:05:34',7,4)+
SUBSTRING('03/01/2017 14:05:34',1,2) + SUBSTRING('03/01/2017 14:05:34',4,2) +
Substring('03/01/2017 14:05:34',12,8))
and error is
Msg 295, Level 16, State 3, Line 3
Conversion failed when converting character string to smalldatetime data type.
any help will be appreciated.
You missed a space between date and time:
select convert(smalldateTime,
Substring('03/01/2017 14:05:34',7,4) +
Substring('03/01/2017 14:05:34',1,2) +
Substring('03/01/2017 14:05:34',4,2) + ' ' +
Substring('03/01/2017 14:05:34',12,8))

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,"/",""))

Resources