Convert varchar to date with malformed values in SQL Server 2008 - sql-server

This whole date code is beyond my understanding but non the less I need to convert a column in a query result from varchar to date. I have some malformed values and I'm trying to filter them out.
SET DATEFORMAT dmy
SET LANGUAGE us_english
select convert(datetime, some_date, 103)
from some_table
where isdate(some_date) = 1
I keep getting
Conversion failed when converting date and/or time from character string
When querying for the malformed values I get these two records
some_date
---------
621
232
Is there any way to validate the format using isdate ?
Thanks, Frustrated Oracle DBA.
EDIT
Tried #Sean Lange suggestions and indeed it looked promising
select convert(datetime, some_date, 103)
from ( select * from some_table where isdate(some_date) = 1 )
But then I added a where clause
select convert(datetime, some_date, 103)
from ( select * from some_table where isdate(some_date) = 1 )
where convert(datetime, some_date ,103) between
convert(datetime, '01/07/2014')
and convert(datetime,'31/07/2014')
How does a where clause being executed before the from?

Since we know that a valid date looks like dd/mm/yyyy how about a simple structure test?
select convert(datetime, some_date, 103)
from some_table
where len(some_date) = 10 --must have exactly the expected number of characters
This could be expanded upon (depending on requirements)
and some_date like '__/__/____'
Or maybe
and some_date like '[0-9][0-9]/[0-9][0-9]/[0-9][0-9][0-9][0-9]'
Anything better would probably need you to put together a custom function that does the CONVERT in a try block, returning NULL in the catch block if its an invalid record. Depends on your requirements...

The problem is happening and will continue to happen because you can't be certain which order the sql engine will take here. One time it might filter the rows first and another time it does the conversion. So what you need to do is force the order the engine works. We can do this by using a CTE to get only the rows we want to convert.
with MyCTE as
(
select some_date
from some_table
where ISDATE(some_date) = 1
)
select CONVERT(datetime, some_date, 103)
from MyCTE

If you're positive that these are the only malformed values and all malformed values will look like this, you could just go ahead and use a hackish CASE statement
SELECT (CASE WHEN LEN(datetime) > 3 THEN convert(datetime, some_date, 103) ELSE NULL),
...
It's really not elegant, but datetimes are notoriously difficult to make elegant because there are so many fragmented formats. If possible, it saves more headaches just to make sure that your data is correctly formatted in the first place through form validation and regex stuff, but since that's not always possible, we have crummy hacks like this to patch ourselves through. Best of luck!

Related

Char conversion to date fails depending on where clause

I am trying to set up a simple query on my data converting char YYYYMMDD to date type. I am using simple convert(date, MyDateColumn).
Select isdate(MyDateColumn),
convert(date,MyDateColumn)
FROM MyTable
WHERE MyTimeColumn = '000000'
Result: Conversion failed when converting date and/or time from character string.
Select isdate(MyDateColumn),
MyDateColumn
FROM MyTable
WHERE MyTimeColumn = '000000'
Result: 1 20190821
Select isdate(MyDateColumn),
convert(date,MyDateColumn)
FROM MyTable
WHERE MyDateColumn = '20190821' AND MyTimeColumn = '000000'
Result: 1 2019-08-21
I have observed that this query has failed for me for specific row above depending whether I added where clause to MyDateColumn. I have added where clause MyDateColumn=MyDateColumn as a workaround. Any idea for a better solution?
I am using SQL Server 2016 (13.0.5201.2). MyDateColumn is char(10) type
The best solution here is to stop storing your dates as text, in text columns, and to start using actual date columns instead. The conversion failure in your very first query is alarming, and would seem to indicate that either you have non dates stored, or maybe there is some trailing/leading whitespace. Since you are using SQL Server 2016, you may try the following query to flush out any non conforming date strings:
SELECT MyDateColumn
FROM MyTable
WHERE TRY_CONVERT(datetime, MyDateColumn) IS NULL;
Once you have located the problematical records, you may fix them, and then give your queries another try.

convert datetime is not running to view,

i tried to this query. but its not running. how can i do?
SELECT dbo.isemri_data.sipnum,
dbo.kayitlar.id,
dbo.kayitlar.makina_id,
dbo.kayitlar.personel,
dbo.kayitlar.isemrino,
dbo.kayitlar.tarih,
dbo.kayitlar.gercek_hiz,
dbo.kayitlar.durus_kod,
dbo.kayitlar.miktar,
dbo.kayitlar.fire
FROM dbo.isemri_data
LEFT OUTER JOIN dbo.kayitlar
ON dbo.isemri_data.isemrino = dbo.kayitlar.isemrino
WHERE CONVERT(VARCHAR(10), dbo.kayitlar.tarih, 104) BETWEEN
CONVERT(VARCHAR(10), '12.08.2015', 104) AND
CONVERT(VARCHAR(10), '19.08.2015', 104)
AND CONVERT(VARCHAR(3), dbo.kayitlar.makina_id) = 'M1'
AND dbo.kayitlar.isemrino LIKE '%'
if delete
CONVERT(varchar(10),dbo.KAYITLAR.TARIH,104) BETWEEN convert(varchar(10),'12.08.2015',104) and convert(varchar(10),'19.08.2015',104)
query is running.
or if run to only main table allready running my query.
i cant find what is the problem.
Since you have mentioned the data type is Datetime , DO NOT convert it to anything else in there where clause, do it in select if you want to.
SELECT dbo.isemri_data.sipnum,
dbo.kayitlar.id,
dbo.kayitlar.makina_id,
dbo.kayitlar.personel,
dbo.kayitlar.isemrino,
dbo.kayitlar.tarih,
dbo.kayitlar.gercek_hiz,
dbo.kayitlar.durus_kod,
dbo.kayitlar.miktar,
dbo.kayitlar.fire
FROM dbo.isemri_data
LEFT OUTER JOIN dbo.kayitlar
ON dbo.isemri_data.isemrino = dbo.kayitlar.isemrino
AND dbo.kayitlar.tarih >= '20150812' -- use ANSI date YYYYMMDD
AND dbo.kayitlar.tarih <= '20150819' -- use ANSI date YYYYMMDD
AND CONVERT(VARCHAR(3), dbo.kayitlar.makina_id) = 'M1'
AND dbo.kayitlar.isemrino LIKE '%'
Converting your datetime to a string means sql server treats them values as strings and not a date/datetime values hence you will get unexpected results Also sql server will not be able to make use of any indexes defined on that column if there are any.
Keep date/datetime values as it is and change the format of date in presentation layer for more easy to eyes date/datetime format.
i found answer. if i use
dbo.kayitlar.tarih BETWEEN
CONVERT(DATETIME, '12.08.2015', 104) AND
CONVERT(DATETIME, '19.08.2015', 104)
running query.
thanx for your help

Converting Date Strings (in different formats) Into Data Type In SQL Server

I have a column of data in the below string formats
When I copy and paste the above into an Excel sheet and change the column to a date format, all of the above will be converted into the same format correctly. Is there a similar feature in SQL server? I've tried using CAST and CONVERT, but have ran into conversion failed errors.
Follow-up
I found the values causing standard CAST and CONVERT functions to fail. There's some rows with decimal and int values as follows:
I ended up writing a CASE statement to account for the different format variations:
CASE
WHEN [CommissionStart Date] LIKE '%NULL%'
THEN '1/1/1900'
WHEN ISDATE([CommissionStart Date]) = 1
THEN CAST([CommissionStart Date] AS DATE)
ELSE CONVERT(DATETIME, CAST([CommissionStart Date] AS DECIMAL(11, 5)))
END
Cast or Convert should work provided the dates are all in a valid format. If a value can't be parsed as a date, there's no magic fix. You have to find them and deal with them manually. The IsDate() function can help you find them.
select * from MyTable where IsDate(TextDateColumn) = 0
IsDate() will return 0 for numeric values like 4567 or 5456.6826 so if you have a lot of those then you can also check IsNumeric()
select * from MyTable where IsDate(TextDateColumn) = 0 and IsNumeric(TextDateColumn) = 0
That should allow you to identify the unparseable dates that are causing the conversion errors.

Remove time from DateTime sql server 2005

I need date part from datetime. in format of "dd-mm-yyyy"
I have tried follwoing
Query:
select Convert(varchar(11), getdate(),101)
Output:
01/11/2011
Query
SELECT cast(floor(cast(GETDATE() as float)) as datetime)
Output
2011-01-11 00:00:00.000
Query:
SELECT
CONVERT(VARCHAR(MAX),DATENAME(DD,GETDATE())) + '-' +
CONVERT(VARCHAR(MAX),DATEPART(MONTH,GETDATE())) + '-' +
CONVERT(VARCHAR(MAX),DATENAME(YYYY,GETDATE())) `
Output:
11-1-2011 i.e. "d-m-yyyy"
I required output in "dd-mm-yyyy" format.
SELECT CONVERT(VARCHAR(10),GETDATE(),105)
Try:
SELECT convert(varchar, getdate(), 105)
More here.
Here you can find some examples how to do this: http://blog.pengoworks.com/index.cfm/2009/1/9/Useful-tips-and-tricks-for-dealing-with-datetime-in-SQL
Using the CONVERT function "works" but only if you're comparing strings with strings. To compare dates effectively, you really need to keep the SMALLDATETIME data type strongly typed on both side of the equation (ie "="). Therefore 'apros' comment above is really the best answer here because the blog mentioned has the right formulas to use to strip off the time component by "flattening" it to midnight (ie 12:00:00) via rounding and any date column in SQL Server 2005 will always default to 12:00:00 if the date is given without a time.
This worked for me ...
select dateadd(day, datediff(day, '20000101', #date), '20000101')

Sql Server string to date conversion

I want to convert a string like this:
'10/15/2008 10:06:32 PM'
into the equivalent DATETIME value in Sql Server.
In Oracle, I would say this:
TO_DATE('10/15/2008 10:06:32 PM','MM/DD/YYYY HH:MI:SS AM')
This question implies that I must parse the string into one of the standard formats, and then convert using one of those codes. That seems ludicrous for such a mundane operation. Is there an easier way?
Try this
Cast('7/7/2011' as datetime)
and
Convert(DATETIME, '7/7/2011', 101)
See CAST and CONVERT (Transact-SQL) for more details.
Run this through your query processor. It formats dates and/or times like so and one of these should give you what you're looking for. It wont be hard to adapt:
Declare #d datetime
select #d = getdate()
select #d as OriginalDate,
convert(varchar,#d,100) as ConvertedDate,
100 as FormatValue,
'mon dd yyyy hh:miAM (or PM)' as OutputFormat
union all
select #d,convert(varchar,#d,101),101,'mm/dd/yy'
union all
select #d,convert(varchar,#d,102),102,'yy.mm.dd'
union all
select #d,convert(varchar,#d,103),103,'dd/mm/yy'
union all
select #d,convert(varchar,#d,104),104,'dd.mm.yy'
union all
select #d,convert(varchar,#d,105),105,'dd-mm-yy'
union all
select #d,convert(varchar,#d,106),106,'dd mon yy'
union all
select #d,convert(varchar,#d,107),107,'Mon dd, yy'
union all
select #d,convert(varchar,#d,108),108,'hh:mm:ss'
union all
select #d,convert(varchar,#d,109),109,'mon dd yyyy hh:mi:ss:mmmAM (or PM)'
union all
select #d,convert(varchar,#d,110),110,'mm-dd-yy'
union all
select #d,convert(varchar,#d,111),111,'yy/mm/dd'
union all
select #d,convert(varchar,#d,12),12,'yymmdd'
union all
select #d,convert(varchar,#d,112),112,'yyyymmdd'
union all
select #d,convert(varchar,#d,113),113,'dd mon yyyy hh:mm:ss:mmm(24h)'
union all
select #d,convert(varchar,#d,114),114,'hh:mi:ss:mmm(24h)'
union all
select #d,convert(varchar,#d,120),120,'yyyy-mm-dd hh:mi:ss(24h)'
union all
select #d,convert(varchar,#d,121),121,'yyyy-mm-dd hh:mi:ss.mmm(24h)'
union all
select #d,convert(varchar,#d,126),126,'yyyy-mm-dd Thh:mm:ss:mmm(no spaces)'
In SQL Server Denali, you will be able to do something that approaches what you're looking for. But you still can't just pass any arbitrarily defined wacky date string and expect SQL Server to accommodate. Here is one example using something you posted in your own answer. The FORMAT() function and can also accept locales as an optional argument - it is based on .Net's format, so most if not all of the token formats you'd expect to see will be there.
DECLARE #d DATETIME = '2008-10-13 18:45:19';
-- returns Oct-13/2008 18:45:19:
SELECT FORMAT(#d, N'MMM-dd/yyyy HH:mm:ss');
-- returns NULL if the conversion fails:
SELECT TRY_PARSE(FORMAT(#d, N'MMM-dd/yyyy HH:mm:ss') AS DATETIME);
-- returns an error if the conversion fails:
SELECT PARSE(FORMAT(#d, N'MMM-dd/yyyy HH:mm:ss') AS DATETIME);
I strongly encourage you to take more control and sanitize your date inputs. The days of letting people type dates using whatever format they want into a freetext form field should be way behind us by now. If someone enters 8/9/2011 is that August 9th or September 8th? If you make them pick a date on a calendar control, then the app can control the format. No matter how much you try to predict your users' behavior, they'll always figure out a dumber way to enter a date that you didn't plan for.
Until Denali, though, I think that #Ovidiu has the best advice so far... this can be made fairly trivial by implementing your own CLR function. Then you can write a case/switch for as many wacky non-standard formats as you want.
UPDATE for #dhergert:
SELECT TRY_PARSE('10/15/2008 10:06:32 PM' AS DATETIME USING 'en-us');
SELECT TRY_PARSE('15/10/2008 10:06:32 PM' AS DATETIME USING 'en-gb');
Results:
2008-10-15 22:06:32.000
2008-10-15 22:06:32.000
You still need to have that other crucial piece of information first. You can't use native T-SQL to determine whether 6/9/2012 is June 9th or September 6th.
SQL Server (2005, 2000, 7.0) does not have any flexible, or even non-flexible, way of taking an arbitrarily structured datetime in string format and converting it to the datetime data type.
By "arbitrarily", I mean "a form that the person who wrote it, though perhaps not you or I or someone on the other side of the planet, would consider to be intuitive and completely obvious." Frankly, I'm not sure there is any such algorithm.
Use this:
SELECT convert(datetime, '2018-10-25 20:44:11.500', 121) -- yyyy-mm-dd hh:mm:ss.mmm
And refer to the table in the official documentation for the conversion codes.
For this problem the best solution I use is to have a CLR function in Sql Server 2005 that uses one of DateTime.Parse or ParseExact function to return the DateTime value with a specified format.
Short answer:
SELECT convert(date, '10/15/2011 00:00:00', 101) as [MM/dd/YYYY]
Other date formats can be found at SQL Server Helper > SQL Server Date Formats
Took me a minute to figure this out so here it is in case it might help someone:
In SQL Server 2012 and better you can use this function:
SELECT DATEFROMPARTS(2013, 8, 19);
Here's how I ended up extracting the parts of the date to put into this function:
select
DATEFROMPARTS(right(cms.projectedInstallDate,4),left(cms.ProjectedInstallDate,2),right( left(cms.ProjectedInstallDate,5),2)) as 'dateFromParts'
from MyTable
The most upvoted answer here are guravg's and Taptronic's. However, there's one contribution I'd like to make.
The specific format number they showed from 0 to 131 may vary depending on your use-case (see full number list here), the input number can be a nondeterministic one, which means that the expected result date isn't consistent from one SQL SERVER INSTANCE to another, avoid using the cast a string approach for the same reason.
Starting with SQL Server 2005 and its compatibility level of 90,
implicit date conversions became nondeterministic. Date conversions
became dependent on SET LANGUAGE and SET DATEFORMAT starting with
level 90.
Non deterministic values are 0-100, 106, 107, 109, 113, 130. which may result in errors.
The best option is to stick to a deterministic setting, my current preference are ISO formats (12, 112, 23, 126), as they seem to be the most standard for IT people use cases.
Convert(varchar(30), '210510', 12) -- yymmdd
Convert(varchar(30), '20210510', 112) -- yyyymmdd
Convert(varchar(30), '2021-05-10', 23) -- yyyy-mm-dd
Convert(varchar(30), '2021-05-10T17:01:33.777', 126) -- yyyy-mm-ddThh:mi:ss.mmm (no spaces)
This page has some references for all of the specified datetime conversions available to the CONVERT function. If your values don't fall into one of the acceptable patterns, then I think the best thing is to go the ParseExact route.
Personally if your dealing with arbitrary or totally off the wall formats, provided you know what they are ahead of time or are going to be then simply use regexp to pull the sections of the date you want and form a valid date/datetime component.
If you want SQL Server to try and figure it out, just use CAST
CAST('whatever' AS datetime)
However that is a bad idea in general. There are issues with international dates that would come up. So as you've found, to avoid those issues, you want to use the ODBC canonical format of the date. That is format number 120, 20 is the format for just two digit years.
I don't think SQL Server has a built-in function that allows you to provide a user given format. You can write your own and might even find one if you search online.
convert string to datetime in MSSQL implicitly
create table tmp
(
ENTRYDATETIME datetime
);
insert into tmp (ENTRYDATETIME) values (getdate());
insert into tmp (ENTRYDATETIME) values ('20190101'); --convert string 'yyyymmdd' to datetime
select * from tmp where ENTRYDATETIME > '20190925' --yyyymmdd
select * from tmp where ENTRYDATETIME > '20190925 12:11:09.555'--yyyymmdd HH:MIN:SS:MS
You can easily achieve this by using this code.
SELECT Convert(datetime, Convert(varchar(30),'10/15/2008 10:06:32 PM',102),102)
This code solve my problem :
convert(date,YOUR_DATE,104)
If you are using timestamp you can you the below code :
convert(datetime,YOUR_DATE,104)
dateadd(day,0,'10/15/2008 10:06:32 PM')

Resources