I found the following issue when I try to valid the following date
ISDATE('3/31/019') this return 1, but is not a valid date, when you try to convert(date, '3/31/019') you will get an error
I expect ISDATE('3/31/019') return distinct 1
The documentation states (my added emphasis):
Returns 1 if the expression is a valid datetime value; otherwise, 0.
So if you run:
SELECT convert(datetime, '3/31/019');
The result with session setting SET DATEFMT mdy succeedes with value '2019-03-31 00:00:00.000'.
Instead of ISDATE, use TRY_CONVERT or TRY_CAST to determine if the expression can be converted to a date. The result will be NULL if not convertable.
According to ISDATE documentation
Returns 1 if the expression is a valid datetime value; otherwise,
0.
While you are trying to convert to date format.
The below query runs without issue
SELECT CONVERT(datetime, '3/31/019')
Related
I am inputting 2 date values in order to filter out from SQL query.
EXEC [Report].[usp_EmployeeReport_Detail] '01-01-2017','31-08-2019'
I am inputting the date as MM/dd/yyyy and the
WHERE clause contains
(j.StartDate BETWEEN #BegDate AND #EndDate)
I m getting the below error when executing the query.
The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value.
I tried to convert the datetime result using the below way as well.
SELECT CONVERT(datetime,01-31-2017,101)
This returns 1894-05-25 00:00:00.000 as the response. How its possible?
As #Damien_The_Unbeliever has said,01-31-2017 using the Numerical expression, which evaluates to -2047.
-2047 means the day minus 2047 days from 1900-01-01, so result of date will be 1894-05-25 00:00:00.000.
So your query SELECT CONVERT(datetime,01-31-2017,101) same as SELECT CONVERT(datetime,-2047,101)
sqlfiddle
You can use the ANSI compliant format YYYYMMDD
SELECT CONVERT(datetime,'20170131',101);
instead of
DD-MM-YYYY 01-31-2017.
Or just add ' to contain the date '01-31-2017' like #fa06 answered.
Try this: u've missed the quote in date:
SELECT CONVERT(datetime,'01-31-2017',101)
try below way
SELECT CONVERT(CHAR,GETDATE(),1 )
it returns 08/13/18
in your case you missed the quote as a result output that type shown
I have the following line in a TVF:
CASE WHEN MIN(ISNULL(EersteStart,GETDATE())) = GETDATE()
then 'InOverleg'
else MIN(ISNULL(EersteStart,GETDATE())) end as EersteStartDatum
Except the fact that this seems kind of inefficiƫnt it still does not work, since
it generates the following error:
Conversion failed when converting date and/or time from character string.
I understand the error but how can I adjust the query to provide the functionality I want? (return string 'InOverleg' when the underlying query returns null for all EersteStartDatum fields of each row or show the MIN date when there are rows where EersteStartDatum is not NULL).
What is confusing? case is an expression and needs to return one type. SQL Server tends to convert to the more restrictive type. In this case, the more restrictive time is a datetime. 'InOverLeg' does not convert.
You can convert the date to a string:
(CASE WHEN MIN(COALESCE(EersteStart, GETDATE())) = GETDATE()
THEN 'InOverleg'
ELSE CONVERT(VARCHAR(10), MIN(COALESCE(EersteStart, GETDATE())), 121) -- or whatever format you like
END) as EersteStartDatum
Use CAST(data_to_return AS varchar(my_desired_string_length)) for all your return data (then result and else result). Now you will have string returned in any case.
everyone,
On a SQL Server instance running 2012 SP3, the following code returns "1":
SELECT ISDATE('january,25,1999')
However, the following fails the conversion:
SELECT CAST('january,25,1999' AS DATE)
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
Am I misunderstanding what ISDATE() ought to return? Why does ISDATE() return a value of "1" for the string? How ought we determine, from SQL Server, whether strings such as "january,25,1999" can be casted as dates?
Thanks!
Forget ISDATE()! You are using SQL Server 2012:
SELECT TRY_CONVERT(date, 'january,25,1999')
This will return NULL if the date cannot be converted -- which I don't think this can be. You can specify a third argument for the particular format you want to convert.
I will note something that in SQL Server 2014:
SELECT TRY_CONVERT(date, 'january,25,1999')
returns an error.
SELECT TRY_CONVERT(datetime, 'january,25,1999')
succeeds.
I have no idea why. But for what you want:
SELECT CONVERT(DATE, TRY_CONVERT(datetime, 'january,25,1999'))
You need to use CONVERT, not CAST, to parse dates from non-standard string formats.
SELECT CONVERT(datetime, 'january,25,1999')
will give you a SQL Server datetime containing 1999-01-25 00:00:00.000
As regards the 2008R2 part...
The issue I found with the ISDATE function is
-- my valid date dd/mm/yy stored as a char
Select ISDATE('dd/mm/yy') returns zero unless you convert to a date
Select ISDATE(convert(date,'dd/mm/yy',103).. say
,but, then as a consequence this returns an error when the convert evaluates a non-date field.
The way around this is to first run
SET DATEFORMAT dmy;
(assume this is scoped to the proc but may not be?)
then ISDATE('dd/mm/yy') should return 1
This looks easy solution but I can't seem to figure out as to why this is not working for me. I have a column that has data like this:
DateField
----------
12/16/2016
11/06/2016
All I want to do is to convert from varchar into a date column, but I am getting this error:
Conversion failed when converting date and/or time from character string.
Here is my simple query:
select convert (date, DateField) as convertedField
from myTable
Nothing wrong with the two examples you have given. There are some bad dates in your table which cannot be converted to date.
Use TRY_CONVERT function for bad dates it will return NULL
select TRY_Convert(date,DateField)
From myTable
You should always store dates in DATE/DATETIME datatype.
If you want to see the records which cannot be converted to date then
select DateField
From myTable
Where TRY_Convert(date,DateField) IS NULL
If working with a specific date format like mm/dd/yyyy You can specify it in Convert() function like the following
CONVERT(DATETIME,DATAFIELD,101)
If it still is not working, use TRY_CONVERT() to get which rows are throwing this exception:
SELECT *
FROM TBL
WHERE TRY_CONVERT(DATETIME, DATAFIELD, 101) IS NULL
This will return rows that cannot be converted
TRY_CONVERT() will return NULL if conversion failed
Read more about DateTime formats here:
SQL Server CONVERT() Function tutorial
Read TRY_CONVERT MSDN Article
You need to specify the format of date time while formatting. The date in your table is currently in U.S format so you should pass the third argument 101 in your convert function.
SELECT CONVERT(date,[DateField],101) FROM myTable;
Working Fiddle here http://rextester.com/NYKR49788
More info about date time style here: https://msdn.microsoft.com/en-us/library/ms187928.aspx
I currently have dates stored in a general attribute field in the database as a string.
They are all stored in the format DD/MM/YYYY for example 01/01/2000
I am able to convert them them to datetime successfully by using the following in my select statement. For example CONVERT(DATETIME, attribute.field_value, 103) where attribute.field_value contains a date.
The SELECT statement works fine and returns the whole table with them correctly.
I can also return a column with todays date in the same format as follows CAST(getdate() AS datetime)
The problem occurs when I try to compare, now I only want to return everything that is newer than today in pseudo code that would dateNewerThanToday > dateToday
Therefore I have tried
WHERE CONVERT(DATETIME, attribute.field_value, 103) > CAST(getdate() AS datetime)
this gives me the error
Conversion failed when converting datetime from character string.
I have tried a multitude of cast/converts to get it to work. I have also wrapped by select so I am only doing it on dataset with the correct data.
Any help would be super useful! Many thanks in advance!!
A couple of things ..
You do not need to convert to GETDATE() to DATETIME data type as it already returns datetime data type.
Instead of CONVERT(DATETIME, attribute.field_value, 103)
use
CONVERT(DATETIME, attribute.field_value) or CAST(attribute.field_value AS DATETIME)
Add a where clause in your select to get only valid DATETIME values. something like
WHERE ISDATE(attribute.field_value) = 1
This will filter out any values which appears to be a date value but sql server doesnt see them as valid date values.
Important Not
Use appropriate data types. If this column is storing date values why not use the DATE or DATETIME data types.
I ran into this exact problem.
Values from a VARCHAR(50) column returned in the SELECT could be cast as date without issue. However when cast in a comparison in the WHERE clause the error occurred.
Of note, the error only occurred when I had other restrictions in the WHERE clause.
When I added ISDATE() to the WHERE clause, the error no longer occurred.
e.g. Shortened example of what worked:
SELECT CONVERT(DATE, mat.myAttributeColumn), mdct.myDateComparisonColumn
FROM myAttributeTable mat
JOIN myDateComparisonTable mdct ON mdct.key = mat.key
WHERE ISDATE(mat.myAttributeColumn) = 1
and mdct.myDateComparisonColumn < convert(DATE, mat.myAttributeColumn)