Date conversion failing on selected date - sql-server

Hi, I am wondering why this date: 11/4/2011 fails when I do a conversion.
E.g. convert(date,date_field,103) returns as expected but when the convert function is trying to convert this date 11/17/2011 it returns conversion failed.
I tried ltrim to see if there is white space but the same error is returning,
I also tried isdate to make sure it is a date and isdate is returning greater than 0.
I know this is basic but could someone give an insight as to why it is rejecting the date?
Thank you.

MSDN says 103 = dd/mm/yyyy. Your date is "11/17/2011".
Is there a 17th month?

use varchar instead of date. like below
Select Convert(Varchar,'11/17/2011',103)

Related

Snowflake date_trunc to remove time from date

I have snowflake column which contains date in yyyy-mm-dd hh:MM:ss format.
I use the below function
date_trunc('DAY', '2019-09-23 12:33:25')
Output : 2019-09-23 00:00:00
Expected as per documentation : 2019-09-23
Is it a bug or is there any other way to remove the time component altogether ?
Depending on what you're wanting to do with the date, having a midnight time is fine.
If you really must get rid of it, this will work:
cast(date_trunc('DAY', '2019-09-23 12:33:25') as date)
date_trunc as the documentation say, truncates a timestamp to values on different grain. But the result is still a timestamp, thus the output format.
if you want just the truncated date, casting to date as cmcau mentions is a simple way to go. But if you are casting to date there is no need to truncate as they are the same value, thus '2019-09-23 12:33:25'::date should be all you need.
In certain environments like Mode Analytics, casting to date like some of the other answers mention still displays a 00:00:00 on the end. If this is the case and you are only using the date for display purposes, you can take your truncated date and cast it to varchar instead like this: '2019-09-23 12:33:25'::date::varchar
Note, I would only recommend this if the other answers are not working for you.

How to get rid of the following error? Converting date from string is failed

In SQL Server, I am getting the error:
Error -2147217913: Conversion failed when converting date an/or time from character string.
Error in converting
I am sure that my fields are in date field, but why this error keeps on showing?
First I validated first if the data type is really a date column:
CASE
WHEN ISDATE(dbo.AdditionalDetailInfo.UserDefined2) = 1
THEN dbo.AdditionalDetailInfo.UserDefined2
ELSE NULL
END AS ReceivedDate
I found out that yes, it's correct. so i proceed with converting this field where UserDefined2 value should always be plus 1 day.
CONVERT(VARCHAR(12), DATEADD(DAY, 1, CONVERT(DATETIME, dbo.AdditionalDetailInfo.UserDefined2)), 103) AS ReceivedDate
Please let me know if there's something wrong with my query as I really can't diagnose what went wrong.
See screenshot:
See this link, the actual data and in SQL server + 1 day
Maybe the field is indeed a date field, so it could be that no conversion is required. Try DATEADD(DAY, 1, dbo.AdditionalDetailInfo.UserDefined2).
If the column is indeed a string column, the you forgot to specify the style parameter when reading the value, you only used it when converting back. Try this instead:
CONVERT(VARCHAR(12), DATEADD(DAY, 1, CONVERT(DATETIME, dbo.AdditionalDetailInfo.UserDefined2, 103)), 103) AS ReceivedDate
MSDN says that date, time and datetime types are not allowed as arguments to ISDATE but in practical terms, passing a date to it:
SELECT ISDATE(getutcdate())
Causes the datetime to be converted to varchar implicitly, passed to ISDATE as varchar, which promptly then returns a 1
As such we've no guarantee that your column really is a date type like you say, so I'm guessing it's probably a string.
Really, what you should do is change your column to be a date type. Don't store dates as strings; if Microsoft had intended you to do this and thought for a second it was a good idea, they wouldn't have implemented other column types than char based ones
If you cannot convert your column to date then you'll need to find the bad value:
select * from table where isdate(column) =0
Note that your attempt to convert style 103 (dd/mm/yyyy) may be failing because you have data that isn't in this style eg Christmas as 12/25/2000
Isdate might not pick this up because your database date format is set to mm/dd/yyyy and isdate thinks it's ok. Change your database date format so that isdate can properly tell what is a good date and what is a bad date
https://learn.microsoft.com/en-us/sql/t-sql/functions/isdate-transact-sql?view=sql-server-2017
And then, please, for the love of doing this properly, use a date typed column, not a string typed one
We need more information. In particular, we need to know what data type the UserDefined2 is. If you tell us that, then we can give better replies.
Just a note that ISDATE is a pretty worthless function since it returns 1 if the string is convertible to datetime. But what if what you really have os datetime2? Or smalldatetime? Because of that, use TRY_CAST instead.
Also, you just gave us pieces of queries, expressions. We don't see the context. There is something called predicate pushing in SQL Server that can mess things up for you (SQL Server pushes a predicate deeper into the query resulting that an expression work over non-datetime values even though you think that you have excluded them in a WHERE clause.
Also, note that how a datetime value is interpreted when you have separators and use the old types (datetime or datetime2) depends on the login's language setting.
More info in http://karaszi.com/the-ultimate-guide-to-the-datetime-datatypes

GQL error when formatting datetime query

I'm attempting to format a GQL query that pulls data between two dates. I've referred to several existing StackOverflow threads (GQL SELECT by date for example), and have tried following the formatting shown there, but for some reason when I test my query out it gives me an error.
Here is the query I'm attempting to use:
SELECT * FROM Packets WHERE timestamp > DATETIME(2017,12,23) AND timestamp < DATETIME(2017,12,29) LIMIT 10
It gives this error:
"GQL query error: Encountered "2017" at line 1, column 50. Was expecting one of: <SINGLE_QUOTE_STRING>, <DOUBLE_QUOTE_STRING>"
I've tried enclosing the dates in strings, I've tried using the DATE object, every format I can think of gives me some sort of error. What am I doing wrong?
The error is right, the DATETIME method needs a single string parameter.
According to the GQL reference, to instanciate a DATETIME in a query the format must be 'YYYY-MM-DDThh:mm:ss.SSSSSS+zz:ZZ':
DATETIME DATETIME() represents a timestamp. must be
in the time format specified in RFC 3339 section 5.6. (However, the
second precision is limited to microseconds and leap seconds are
omitted.) This standard format is: YYYY-MM-DDThh:mm:ss.SSSSSS+zz:ZZ
...
Your example working:
SELECT * FROM Packets WHERE timestamp > DATETIME('2013-09-20T09:30:20.00002-08:00') AND timestamp < DATETIME('2013-09-29T09:30:20.00002-08:00') LIMIT 10
You can check the complete article here :
https://cloud.google.com/datastore/docs/reference/gql_reference
Thanks for comment this problem.
With the answers of each one I can do a very easy (but almost impossible in GQL) Query.
Check this out, I hope it will help to someone:
SELECT * FROM Task WHERE recordDate >= DATETIME('2018-09-09T00:00:00.00000-03:00')
AND recordDate <= DATETIME('2018-09-20T23:59:59.99999-03:00')
Where "2018-09-09T00:00:00.00000-03:00" is the full datetime value and
it means:
2018-09-09 -> Date Indicator (YYYY-MM-DD in my case)
T -> Indicator that the next values are Time values
00:00:00.00000 -> Time Indicator (HH:mm:ss:[miliseconds])
-03:00 -> Time Zone indicator (Chile in my case)
I really hope this post will be useful to anyone that have the same trouble with dates using GQL

SQL Server query (view) Convert String to DateTime

I'm a bit rusty and seem to have completely forgot how to do this. I am receiving data and the datatime column is in string format char(12).
Example : 201411061900
How would I write the query (view) to convert that to DateTime?
I've used CONVERT (datetime, dbo.KWH.mr_dtm,120), but I get the error Conversion failed when converting date and/or time from character string.
I haven't had much luck. Thank you very much in advance.
If you want to suggest -5 time zone also, I wouldn't mind. :)
Magnus answered my question. I wanted to post it here incase it helps anyone in the future.
Here is the link to the answer.
There is no output format identifier (like for example 120) that lets you convert a char(12) into a datetime in SQL Server. But you could use the Substring function:
Select Cast(Substring(dbo.KWH.mr_dtm,1,8) + ' ' + Substring(dbo.KWH.mr_dtm,9,2)+':'+ Substring(dbo.KWH.mr_dtm,11,2) as DateTime)
Thank you to everyone! It is very much appreciated.
Oh, and this post help me solve my timezone setup.
Convert Datetime column from UTC to local time in select statement
Many thanks to Michael Goldshteyn for the timezone convert.

SFDC, difference between two dates

I'm trying to get the number of days between a start date and end date.
When I do "eDate - sDate" I receeive "Incorrect parameter type for operator '-'. Expected Number, Date, DateTime, received Text."
When I do "DATEVALUE(eDate )-DATEVALUE(sDate)" I receive "Formula result is data type (Date), incompatible with expected data type (Number)."
The date format is "mm/dd/yyyy"
Can someone suggest a fix
Try and use DATETIMEVALUE instead . If its a field change the type accordingly if its a string just add 00:00:00 to end of it.
Field need to be the same Datatype, either Date, or Date/Time, otherwise you'll have to convert them into the same datatype before you evaluate them.

Resources