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.
Related
I am creating a table
create or replace table "test" (
type1 DATE,
type2 DATE,
type3 DATE
);
insert into "test" (type1, type2, type3) values
('20170415', '1996','15042017');
select * from "test";
I dont see the results as intended, we can see the below image
How to get the results as i want.
Dates are held in the underlying database as decimals, how they are displayed in the UI is determined by the account or session settings. If you want a date to be shown in a different format then you would need to convert it to a char in your SQL and supply the required format string.
If you insert a string into a date field then Snowflake will do its best to understand what date that string represents and convert it to that date - however you should always provide these strings in a date format that Snowflake is guaranteed to convert to the correct date or, preferably, explicitly cast the strings as dates. Don’t rely on Snowflake converting the dates in the way you require.
1996 is not a valid date so you can’t hold this value in a date column
I am trying to convert the date time column of this format 1978-01-29 00:00:00.000 to date 1978-01-29
I tried convert , cast, case statement ,nothing works. I am getting the following error
The conversion of a date data type to a datetime data type resulted in
an out-of-range value.
When I run:
SELECT CAST('1978-01-29 00:00:00.000' AS date)
I get:
1978-01-29
If you get an error running that exact code, then probably your locale settings are treating "29" as the month instead of the day, though I'm not aware of any locale setting that would treat that string that way.
I also just noticed that your error message says the error occurred while converting a date to a datetime. Yet in your question you claim you are attempting to convert a "date time column" to a "date". Maybe the error isn't in the part of the code you think it is.
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
In which format I should pass date field to parameter to be able to choose date picker insted of list?
My query returns date (date format) and I cast it in a different ways (yyyy-dd-MM, yyyy-MM-dd, dd-MM-yyyy, ...) in SSRS:
=Format(Fields!StartDate2.Value,"yyyy-dd-MM")
I use this field in parameter, but I always get error:
An error has occurred during report processing. (rsProcessingAborted)
The property ‘ValidValues’ of report parameter ‘STARTDATE’ doesn't
have the expected type. (rsParameterPropertyTypeMismatch)
When I just passing result of query (date format), I have list:
even If chose Date/Time:
Answer
The reason you are getting this problem is because the Language/Culture/Date Format on your environments are not similar.
SQL by DEFAULT uses en-US and your local pc uses your local Language/Culture/Date Format.
there is your problem, instead of converting the value of your dates to your local
Language/Culture/Date Format, convert it to en-US
"MM-dd-yyyy"
Answer explained
to put this in perspective you are sending a SQL server with the date format
"MM/dd/yyyy" the value "2017/16/03".
so the server thinks "this guy is telling me to search for the 2017th month, 16th day of the year 03"
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)