Error of conversion Datetime-varchar in MSSQL - sql

This is the query I am getting error with:
Select Time from Flights where Time <= DATEADD(day,3,'14/05/2018 00:00:00')
AND Time >= getdate()
The error says:
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
This error though does not happen if I run the query on a clone-database in SQLExpress (management Studio). Any Idea? Thanks.

SQL Server likes this format (assuming standard latin collation. I can't speak to other lesser used collations):
YYYY-MM-DD
Also, Is your Time column a datetime or is it just time? That could cause another issue.
Try this query and see if it works.
Select Time from Flights where Time <= DATEADD(day,3,'2018-05-14 00:00:00')
AND Time >= getdate()

Related

Convert('yymmdd' as datetime) is not working in sql server

I have this function in one of the SP it's working in one server but same conversion thouing error in other server .
Example:
Convert(datetime,'210319')
Error:
The conversion of a varchar data type to a datetime data type resulted
in an out-of-range value.
You need a four-digit year. How about adding one in?
Convert(datetime, '20' + '210319')

SQL Server date format to Datetimeoffset with specifc format

I have database table that has datetime field in SQL Server.
I have to send as below sample.
Can you please help how to get this dateformat?
Example: 2020-06-10T13:11:00-05:00
Thanks
If you were on a supported version of SQL Server, this is actually quite trivial. If we assume the value in the table is 2020-06-10T13:11:00 it would just need a CONVERT and SWITCHOFFSET:
SELECT V.Dt,
SWITCHOFFSET(CONVERT(datetimeoffset(0),V.dt),'-05:00')
FROM (VALUES(CONVERT(datetime2(0),'2020-06-10T13:11:00')))V(Dt);
If it's actually a UTC time, and you need to change it to (I assume) Central, then it would be:
SELECT V.Dt,
CONVERT(datetimeoffset(0),V.dt) AT TIME ZONE 'Central Standard Time'
FROM (VALUES(CONVERT(datetime2(0),'2020-06-10T18:11:00')))V(Dt);
Instead, you're going to have to do some varchar manipulation, as you're using a version of SQL Server that is completely unsupported:
--If switching the offset
SELECT V.Dt,
CONVERT(datetimeoffset(0),CONVERT(varchar(20),V.Dt,126) + '-05:00')
FROM (VALUES(CONVERT(datetime2(0),'2020-06-10T13:11:00')))V(Dt);
--If changing the offset
SELECT V.Dt,
CONVERT(datetimeoffset(0),CONVERT(varchar(20),DATEADD(HOUR, -5,V.Dt),126) + '-05:00')
FROM (VALUES(CONVERT(datetime2(0),'2020-06-10T18:11:00')))V(Dt);
Note that both of these are DST agnositic, as it SWITCHOFFSET. Only AT TIME ZONE will consider the DST.

Setting sql date format

I can't work out what has changed to only just start getting this error
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
I have checked that the language of the SQL Server instance is "British English"
I have checked that the language of the user is "British English"
What have I missed?
select * from table where updated <= '15/09/2012'
If I run set dateformat dmy, it works, but obviously only at a session level. I need to fix it for the server
A better approach which eliminates all ambiguity is to use ISO 8601 formatting for all your dates - 2012-09-15. And that will work regardless of your regional settings.
select * from table where convert(date,updated,105) <= '15/09/2012'
can you check above query?. I hope it will work.
declare #updated date = GetDate()
select
case when (convert(date,'15/09/2012', 105) <= #updated) then 1
else 2 end
this query is giving 1 as output, so i guess it would work for your case also.

sql datetime queries

I have tables with this type of datetime: 2010-09-16 00:32:41.960' CreatedDate column.
I need to perform a query ... where [CreatedDate] >= '2010-09-16 00:32:41.960'
but that just gives me
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
What is the correct syntax, it's been a while since I've done SQL the last time.
Thanks in advance.
Use an unambiguous datetime format so SQL Server doesn't have to guess how to convert it:
where [CreatedDate] >= '2010-09-16T00:32:41.960'
It's a shame that, depending on your regional settings, it may interpret the form with a space separator (rather than T) as YYYY-DD-MM hh:mm:ss. Which gives an out of range month with your example (and wrong results for dates early in the month, except when day=month)
I'd normally list the safe formats as:
YYYYMMDD
YYYY-MM-DD'T'hh:mm:ss
YYYY-MM-DD'T'hh:mm:ss.mil
There are some other formats that are now safe if converting to datetime2 or date, but I can't remember them, and the above usually suffice.
Another alternative would be to run a set dateformat statement before using these date literals:
set dateformat mdy
select MONTH('2010-09-16 00:32:41.960')
Gives 9 as the result, whereas what you're experiencing can be reproduced with:
set dateformat dmy
select MONTH('2010-09-16 00:32:41.960')
Try:
where [CreatedDate] >= CONVERT(datetime,'2010-09-16 00:32:41.960');

Converting a BMC Remedy timestamp to mm/dd/yyyy format

According to the documentation I've found from AR Systems on BMC Remedy, timestamps in an MSSQL Server environment (we're using SQL Server 2005 here) are stored as an int datatype in UNIX timestamp format.
I cannot for the life of me get custom SQL in Business Objects Web Intelligence to let me convert this timestamp into mm/dd/yyyy format, which I need to be able to group records by a date (without the timestamp, obviously). Anything I try to do involving math or datatype conversion throws an "invalid type" error. I can't convert the field to an int, varchar, datetime, or anything else. The only function that works is DATEADD, which still returns the full timestamp.
How can I get this converted? I'm going nuts here!
to convert GMT/UTC/Zulu to Local time Zone(EDT/New York):
DATEADD(hour,-5,DATEADD(s,Last_Modified_Date,'1/1/1970 00:00:00'))
Example of use to display Remedy work info entries (Transact-SQL):
SELECT Work_Log_ID, DATEADD(hour, +12, DATEADD(s, Last_Modified_Date, '1/1/1970 00:00:00')) as Last_Modified_Date , Description, Detailed_Description, Infrastructure_change_ID, Number_of_Attachments
FROM dbo.CHG_WorkLog
WHERE Infrastructure_Change_ID = 'CRQ001261'
ORDER BY Work_Log_ID desc
Why doesn't this work?
DECLARE #timestamp INT
SELECT #timestamp = DATEDIFF(s,'1/1/1970 00:00:00',GETDATE())
SELECT DATEADD(s,#timestamp,'1/1/1970 00:00:00')
Substitute the #Timestamp with the value from your table.
You may need to multiply the int timestamp by 1000. The AR System stores the date as the number of 'seconds' where as most other languages store the unix timestamp as milliseconds (and as a long data type)
Hope that helps!
Go to
user tool -> Tools -> Options -> Locale tab -> Date/Time Style -> Custom Format -> mm/dd/yyyy