Conversion of a varchar data type to a datetime data type resulted in an out-of-range value in TSQL - sql-server

In my T-SQL select there appears to be an error with my datetime
select
t.F47, t.F53, t.F40, t.F162, t.F163,
N'10' as kostenart, t.F39, t.F2, t.F5,
convert(nvarchar, cast(t.F9 as datetime), 112),
t.PARID, 20170928135800 as exportzeitstempel
from
T_TRANS6 as t
where
t.F20 = N'Erledigt'
and t.F9 < convert(datetime, '01.09.2017 00:00:00', 104)
The error message is German and it says:
Meldung 242, Ebene 16, Status 3, Zeile 1
Bei der Konvertierung eines nvarchar-Datentyps in einen datetime-Datentyp liegt der Wert außerhalb des gültigen Bereichs.
I tried to translate this to:
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value."
I really don't know how I did from so thanks for any help guys

Your problem is triggered by cast(t.F9 as datetime).
Please do : SELECT getdate(); to get the implicit "datetime to string" convertion format.
WARNING : Implicit convertion format are set at the instance level. It can differ from a server to another, even in the same compagny...
This will gives you someting like dd.MM.yyyy HH:mm:ss or yyyy-MM-dd HH:mm:ss or ...
The given format is the one expected and required for all F9 of TRANS6 table records!!
A single TRANS6.F9 with a wrong formating patern will raise this ERROR. So analyse your F9 data, find the concerned rows, clean them and retry...
NOTE : CONVERT(NVARCHAR(8), getdate(),112) get a string like YYYYMMDD (Ex : '20170928') witch is the only sortable string format of dates...
EDIT : F9 = '2016.10.30' and Implicit convertion expect '2016-10-30' !!
Try this :
select
t.F47, t.F53, t.F40, t.F162, t.F163,
N'10' as kostenart, t.F39, t.F2, t.F5,
convert(nvarchar, convert(datetime,t.F9,102), 112),
t.PARID, 20170928135800 as exportzeitstempel
from
T_TRANS6 as t
where
t.F20 = N'Erledigt'
and t.F9 < convert(datetime, '01.09.2017 00:00:00', 104)
Does it works?

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)

invalid input syntax for type timestamp

While running the below code i get an error saying invalid input syntax for type timestamp from admission_datetime.
UPDATE ccsm.stg_demographics_baseline
SET xx_los_days =
(CASE WHEN admission_datetime IS NULL OR
date_trunc('day',admission_datetime) = ''
THEN NULL
WHEN discharge_datetime IS NULL OR
date_trunc('day',discharge_datetime) = ''
THEN date_diff('day', admission_datetime, CURRENT_DATE)
ELSE
date_diff('day', admission_datetime, discharge_datetime)
END);
enter code here
See date_trunc documentation:
The return value is of type timestamp or interval with all fields that are less significant than the selected one set to zero (or one, for day and month).
So you can not compare it with an empty string:
date_trunc('day', admission_datetime) = ''
The invalid input syntax for type timestamp error message concerns the empty string (''), not the admission_datetime column.
Furthermore, there is no date_diff function in PostgreSQL. Just subtract one timestamp from another and you will get an interval result:
SELECT timestamp '2001-09-29 03:00' - timestamp '2001-09-27 12:00'
You'll get
interval '1 day 15:00:00'
If you need the difference in days, try this:
SELECT DATE_PART('day', timestamp '2001-09-29 03:00' - timestamp '2001-09-27 12:00')
The result is 1.
See here for examples of DATEDIFF-like expressions in PostgreSQL.

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))

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