Converting date and/or time from character string - sql

Can someone please tell me what 's wrong with this code that I get error:
Conversion failed when converting date and/or time from character string.
, iif(t1.medbuildcmpltdt IS NOT NULL,cast(datediff(day, t1.medbuildcmpltdt,t1.orderingtoolbuildcmpltdt)AS nvarchar(255)), cast(t1.orderingtoolbuildcmpltdt AS nvarchar(255)) AS 'Main_Days Build'

Conversion failed when converting date and/or time from character string.
The problem is where SQL Server wants a date or datetime and is receiving a string. The only thing in the code you provided that requires a date or datetime is
datediff(
day,
t1.medbuildcmpltdt,
t1.orderingtoolbuildcmpltdt
)
So, t1.medbuildcmpltdt or t1.orderingtoolbuildcmpltdt is a string ([n][var]char) and contains values that cannot be interpreted as valid date or datetime values.

Related

How to convert YYMMDDhhmm formatted NVARCHAR into YYYY-MM-DD hh:mm:ss formatted DATETIME?

For example:
The value 2208111603(nvarchar) needs to be converted to 2022-08-11 00:00:00(datetime).
The things I have tried and the corresponding errors I've got so far as follow:
CAST(DATE_TIME AS numeric) works but CAST(CAST(DATE_TIME AS numeric) AS datetime) returns Arithmetic Overflow Error.
convert(datetime2, CAST(DATE_TIME AS numeric), 105) returns
Explicit conversion from data type numeric to datetime2 is not allowed
CONVERT(datetime2, DATE_TIME , 105) returns
Conversion failed when converting date and/or time from character
string.
CONVERT(datetime2, CONVERT(CHAR(10), DATE_TIME), 105) returns
Conversion failed when converting date and/or time from character
string.
Finally, I tried PARSE(DATE_TIME AS date USING 'en-US') which resulted in
Error converting string value '2208111603' into data type date using
culture 'en-US'.
The SQL Server's CONVERT function also has some built-in formats as a third parameter for converting varchar to datetime. So, I tried SELECT convert(datetime,'2208111603',112) which resulted in the same error (Msg 241):
Conversion failed when converting date and/or time from character
string.
Any help would be much appreciated!
With just a bit of string manipulation...
Note: this will assume century of 20
Example
Declare #S varchar(50) ='2208111603'
Select try_convert(datetime,stuff(stuff(#S,7,0,' '),10,0,':'))
Results
2022-08-11 16:03:00.000
A little bit ugly, but to overcome the century indicator you an nest a little logic to conditionally add 19.
Declare #S varchar(50) ='6408111603'
Select try_convert(datetime,case when left(#S,2)>'50' then '19' else '' end + stuff(stuff(#S,7,0,' '),10,0,':'))

Data type conversion failed when converting date and/or time from string in SQL

select distinct
format(dateadd(day, 8- datepart(weekday, dlv_hdr.actual_goods_movement_dt),
cast(dlv_hdr.actual_goods_movement_dt as date)), 'MM/dd/yyyy') as "Week End Date"
from
can_delivery.sap_delivery_header_data dlv_hdr
I'm getting the error shown below while running this SQL Server code in SSMS. The data type of column actual_goods_movement_dt is varchar(20) and data is in yyyymmdd format. I tried many solutions available but non of them worked for me. Your help is appreciated
Conversion failed when converting date and/or time from character string

Showing this error "Conversion failed when converting date and/or time from character string." in SQL

My sp is
EXEC _REPORTS #StartDate=N'17-Sep-2016',
#EndDate=N'17-Sep-2016',
#CustomerID=0,
#ProductID=0,
#BranchID=0,
#NumberOfRecords=100,
#PageNo=1,
#IsBuy=1
here the region of date used in where
AND FCSB.FCBuySellDate
BETWEEN CONVERT(datetime,#StartDate,105) AND CONVERT(datetime,#EndDate,105)
The reason is primarily you are getting some wrong formatted data to convert, For example
declare #StartDate nvarchar(15) = N'17-Septt-2016'
select CONVERT(datetime,#StartDate,105)
You will get this error
Conversion failed when converting date and/or time from character string.
If you get correct date you get it converted, one way to ignore this error is to do TRY_CONVERT which willl return NULL. Best way to solve this problem is to identify why it is getting wrong date as parameter and fix that.
select TRY_CONVERT(datetime,#StartDate,105)
check for null value in FCSB.FCBuySellDate. It might be cause of an error
try this
AND FCSB.FCBuySellDate BETWEEN CONVERT(datetime,#StartDate,105) AND CONVERT(datetime,#EndDate,105)
AND isnull(FCSB.FCBuySellDate,'') <> ''
Also try this, convert column in datetime
AND CONVERT(datetime,FCSB.FCBuySellDate) BETWEEN CONVERT(datetime,#StartDate,105) AND CONVERT(datetime,#EndDate,105)

Date Cast/Convert

I get errors like "Conversion failed when converting date and/or time from character string." All I have to do is add a WHERE clause where I compare values having (datetime, null) format (like 2014-07-29 00:00:00.000) and values having (decimal (8,0), null) format (like 20140723).
I tried to cast both these values as varchar, tried to cast one of them as date - nothing works.
At the same time, when I ran a previous query without the WHERE clause, the following join condition worked perfctly:
on cast(DATE_1 as date)= cast(cast(DATE_2 as varchar) as date)
Any ideas? Thanks a bunch!

How do I convert a custom time format to time type in sql server

In the db table the time is imported into a field with the following format h:mmt (12 hour:minutes AM/PM designator). The field is a varchar type field. I need to copy this into a field of type time.
I tried select CONVERT(time, '[Time]', 8) but I get the following error: Conversion failed when converting date and/or time from character string.
Any ideas on how I can convert and copy the varchar time to a type of time?
You just need to remove the quotes from around your column name, as you're trying to convert the word [Time] to a time, which won't work.
Try the below:
SELECT CONVERT(time, [Time], 8)