Function to evaluate valid and invalid dates - sql

I am using SQL Server 2008
I receive data in a couple of formats. Dates are varchar(10)
For example:
1> 951116 = YYMMDD
2> 122487 = MMDDYY
There exists rows with blank values or some rows have some text in it.
I want to check if it is a date and convert it, else it can be null.
I have been using the following code but it only enables me to convert the date as in the 1st example and throws an error in the second one.
create view v2 as
SELECT name
,CASE WHEN LEN(DOB) < 6 THEN '' ELSE CONVERT(VARCHAR,
CAST(CAST('19' + SUBSTRING(DOB, 5, 2) + SUBSTRING(DOB, 1, 2) + SUBSTRING(DOB, 3, 2) AS VARCHAR(10)) AS DATE), 110) END AS OWNR_DOB
FROM customer
I want to write a function and get it to YYYYMMDD format.
A simple function just to convert the YYMMDD format to YYYYMMDD and treat the other values as invalid or null can be a start too.
Any help is appreciated.

Related

Error Handling when converting text to dates

Lets says I have a column which has text and most of them are in a particular format which allows me to scrape out the date part. However, some of the texts does not have dates and I want it to return NULL rather than scraping it. How would I go about doing this in Redshift (SQL)?
Current Output:
CAST(REPLACE(SUBSTRING(c."name",3,9),'.','-') AS DATE)
Some fields are like the below and I need the code to return a null when it cannot convert the substring to a date datetype
Example:
Error:
You can use pattern matching and case + when for such cases
select case
when c."name" similar to '%\\d{2}.\\d{2}.\\d{2}%'
then CAST(REPLACE(SUBSTRING(c."name", 3, 9), '.', '-') AS DATE)
else null
end as cast_date;

Converting strings to date in SQL

I am using SQL Server 2017. I am trying to handle strings from a free format field and either convert them to a date in the format of "dd/mm/yyyy" or if they are not in this format then simply display the text verbatim.
I need this in a VIEW so can not use SET LANGUAGE. Sounds simple using Convert and IsDate but does not seem to work.
So for the snippet of code below (remember this will be in a view), I want to read the text and if the string converts to a date (ie. is in the format dd/mm/yyyy then run the convert to a date as I need it in date format for Excel to pick up (via Connect SQL Server database)), and if it does not convert to a date then display the text as it is.
create table dateTest1
(
idx int,
dateStringTest varchar(15)
);
insert into dateTest1 (idx, dateStringTest)
values (1, '13/01/2021'), (2, 'no');
select
case
when isdate(convert(datetime, dateStringTest, 103)) = 1
then convert(datetime, dateStringTest, 103)
else dateStringTest
end as dtres
from
dateTest1
--where idx = 1
Error:
Msg 241, Level 16, State 1, Line 15
Conversion failed when converting date and/or time from character string.
This error happens for idx = 2. Idx = 1 works ok.
Any assistance with this would be greatly appreciated. Thanks in advance.
You need to cast your resulting date to a varchar. A case expression can only return a single data type and the order-of-precedence means it is still trying to convert the varchar values to datetime
select
case when isdate( dateStringTest) = 1
then Cast(convert(datetime, dateStringTest, 103) as varchar(10))
else dateStringTest
end as dtres
from dateTest1
You can compact into a single statement (the same order of precedence applies)
select IsNull(Cast(Convert(datetime,Try_Cast(dateStringTest as date),103) as varchar(10)),dateStringTest)
from datetest1
Based on the syntax you are using, I assume you are using SQL Server. You should add the appropriate tag to your question.
The only way to do that in one column is to leave it as text.
Your use of ISDATE() is incorrect. If dateStringTest is not a valid date, CONVERT() on SQL Server will throw an error like:
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
That's why you get an error for idx=2
You say you "need it in date format for Excel to pick up". How Excel interprets it will probably depend on your locale settings. Assuming mm/dd/yyyy is valid in your locale, Excel probably already sees it as a date. But I assume that's not happening, so mm/dd/yyyy is not valid for your locale. For me, if I have this table in SQL...
create table dateTest1 (
id int identity(1,1) not null,
dateStringTest varchar(20)
)
insert dateTest1
values
('2020-01-01')
, ('2020-21-01')
, ('2020-01-21')
, ('1/1/2020')
, ('1/21/2020')
, ('21/1/2020')
, ('21/01/2020')
, ('other stuff')
...and query it from Excel, adding columns with the functions DATEVALUE, DAY, WEEKDAY, and YEAR, I get...
id
dateStringTest
DATEVALUE
DAY
WEEKDAY
YEAR
1
2020-01-01
43831
1
4
2020
2
2020-21-01
#VALUE!
#VALUE!
#VALUE!
#VALUE!
3
2020-01-21
43851
21
3
2020
4
1/1/2020
43831
1
4
2020
5
1/21/2020
43851
21
3
2020
6
21/1/2020
#VALUE!
#VALUE!
#VALUE!
#VALUE!
7
21/01/2020
#VALUE!
#VALUE!
#VALUE!
#VALUE!
8
other stuff
#VALUE!
#VALUE!
#VALUE!
#VALUE!
That seems to indicate that Excel is recognizing some of the values as dates.
What you need to do is try to convert the value to datetime, then if it fails report the original value. In both cases, output a string.
You should review the documentation for CONVERT().
Try this:
select dateStringTest
, coalesce(cast(try_convert(datetime, dateStringTest, 103) as varchar(20)), dateStringTest) as dtres1
, coalesce(convert(varchar(20), try_convert(datetime, dateStringTest, 103), 103), dateStringTest) as dtres2
, coalesce(convert(varchar(20), try_convert(datetime, dateStringTest, 103), 101), dateStringTest) as dtres3
from dateTest1
UPDATE
If you are considering only values matching the pattern dd/mm/yyyy as dates (so 17/01/2021 is a date and 17/1/2021 is not), this brute force method will work with SQL Server 2008 (compatibility level 100):
(Notice I updated my input, also, above.)
;
with a as (
select id
, dateStringTest
, SUBSTRING(dateStringTest, 7, 4) + '-' + SUBSTRING(dateStringTest, 4, 2) + '-' + SUBSTRING(dateStringTest, 1, 2) as converteDate
from dateTest1
)
select id
, cast(convert(datetime, dateStringTest, 103) as varchar(20)) as dtres
from a
where isdate(converteDate) = 1
union
select id
, dateStringTest as dtres
from a
where isdate(converteDate) = 0

Convert nvarchar date (DD/MM/YYYY) to Date Period (YYYY_MM)

I am trying to convert this into a period format, so e.g. 2018_05 (YYYY_MM). currently the data is in DD/MM/YYYY format.
I tried a cast code but it returns me YYYY_DD.
SELECT
CASE WHEN RESERVED_FIELD_4 IS NULL THEN NULL
ELSE cast(year(RESERVED_FIELD_4) as Nvarchar (4))
+'_'+right('00'+cast(month(RESERVED_FIELD_4) as Nvarchar (2)),2)
END AS [DATAFEED_PERIOD]
I expect/want to see YYYY_MM.
Assuming RESERVED_FIELD_4 is a string type (char/nchar/varchar/nvarchar) the simplest solution would be to use substring:
CASE
WHEN RESERVED_FIELD_4 IS NULL THEN NULL
ELSE SUBSTRING(RESERVED_FIELD_4, 7, 4) + '_'+ SUBSTRING(RESERVED_FIELD_4, 4, 2)
END AS [DATAFEED_PERIOD]
If it's a date/datetime/datetime2 data type, the simplest solution would be to use format:
FORMAT(RESERVED_FIELD_4, 'yyyy_MM')
But for better performance you can use convert and stuff:
SELECT STUFF(CONVERT(char(6), RESERVED_FIELD_4, 112), 5, 0, '_')
In case your format is actually d/m/y the simplest option is to convert to date and than back to string:
SELECT STUFF(CONVERT(char(6), CONVERT(Date, RESERVED_FIELD_4, 103), 112), 5, 0, '_')
This is the common problem of storing a date with a VARCHAR column. You are guessing that the stored pattern is DD/MM/YYYY but the SQL engine doesn't know that and is currently assuming the MM/DD/YYYY pattern.
Please check these results:
-- MM/DD/YYYY
SELECT
DAY ('05/01/2019'), -- 1
MONTH('05/01/2019') -- 5
-- DD/MM/YYYY
SELECT
DAY ('25/05/2019'), -- Conversion failed when converting date and/or time from character string
MONTH('25/05/2019') -- Conversion failed when converting date and/or time from character string.
To display what you want correctly use string functions:
SELECT
RIGHT(RESERVED_FIELD_4, 4) + '_' + SUBSTRING(RESERVED_FIELD_4, 4, 2)
But you should actually fix the values on your VARCHAR column, cast them to DATE and store the values as DATE.
ALTER TABLE YourTable ADD ReservedField4Date DATE
UPDATE YourTable SET
ReservedField4Date = CONVERT(DATE,
RIGHT(RESERVED_FIELD_4, 4) -- Year
+ '-' + SUBSTRING(RESERVED_FIELD_4, 4, 2) -- Month
+ '-' + LEFT(RESERVED_FIELD_4, 2)) -- Day
ALTER TABLE YourTable DROP COLUMN RESERVED_FIELD_4
EXEC sp_rename 'SchemaName.YourTable.ReservedField4Date', 'RESERVED_FIELD_4', 'COLUMN'
Beware that changing the column type might affect other queries that assume this is a VARCHAR column.
If your data is in DD/MM/YYYY format, then it is being stored as a string. Hence, string functions come to mind:
select right(RESERVED_FIELD_4) + '_' + substrint(RESERVED_FIELD_4, 4, 2)
In SQL-SERVER you can use 'format'
format(dy,#your_date) as day_of_year
month(#your_date) as month
Try this:
Select concat(month(#your_date),'_'year(#your_date)) as your_period
this is a reference
Why not just do conversations ? :
SELECT REPLACE(CONVERT(VARCHAR(7), CONVERT(date, RESERVED_FIELD_4, 101), 102), '.', '_')
This assumes RESERVED_FIELD_4 is date type.

query to search dates which are stored as string in the database

I have a table where I store an activity completion date as varchar. The format of the date stored is MM/DD/YYYY HH:MM:SS.
I have search window where I have two fields Completion date from and completion date to.The date format selected here is MM/DD/YYYY.
How do I write a query such that I am able to fetch the activity completion between two given dates from the table which has the dates stores as varchar.This table was created a long time back and no thought was given to saving dates as datetime.
You can use SQL CONVERT to change your columns to DATE format but that will cause performance issues.
SELECT *
FROM MyTable
WHERE CONVERT(DATETIME, MyDate) >= CONVERT(DATE, '01/01/2014')
AND CONVERT(DATETIME, MyDate) <= CONVERT(DATE, '01/31/2014')
CONVERT documentation - http://msdn.microsoft.com/en-us/library/ms187928.aspx
if you are unable to change how data is stored, than for better performance , you can create view with calculated column that converts VARCHAR to DATETIME. After that can create index on calculated column. Index on Computed Column documentation
Use the SUBSTRING function to get the date parts in a comparable order (i.e. yyyymmdd):
select *
from mytable
where
CONCAT( SUBSTRING(thedate, 7, 4) , SUBSTRING(thedate, 4, 2) , SUBSTRING(thedate, 1, 2) )
between
CONCAT( SUBSTRING(#FROMDATE, 7, 4) , SUBSTRING(#FROMDATE, 4, 2) , SUBSTRING(#FROMDATE, 1, 2) )
and
CONCAT( SUBSTRING(#TODATE, 7, 4) , SUBSTRING(#TODATE, 4, 2) , SUBSTRING(#TODATE, 1, 2) )
;
You could use this code :
select * from table_name
where CAST(col1 as date )
between CAST(Completion date from as date )
and CAST(Completion date to as date);
Function syntax CAST:
CAST ( expression AS data_type )
You can use below if the date format is {yyyy-MM-dd}, or you can adjust the charindex's index value depending on format
SELECT *
FROM table
WHERE
CHARINDEX('-', col_value, 0) = 5
AND CHARINDEX('-', col_value, 6) = 8
AND LEN(col_value) = 10
The above piece will look for first occurrence of char '-' at position 5 and the second char '-' at position 8 while the entire date value's length is equal to 10 chars
This is not full proof, but will narrow down the search. If you want to add time then just expand the criteria in the where to accommodate the format i.e. {yyyy-MM-dd 00:00:00.000}
This is a safe way to query the data, without any unexpected 'invalid cast / convert' errors.

Computed column

In current table I have a column that holds the date field in ddmmyyyy format and it is of type varchar(8). The column has some string value also. I want to create a computed column that will hold the value in DateTime format if the value in source column is valid date time.
Assuming your varchar(8) column is dateString :
Cast([dateString] as datetime)
SQL Server prefers dates in the format of yyyymmdd so there will be some string manipulation involved to format your data like this. We should also use the IsDate function to make sure we have a valid date.
So:
Cast(Case When IsDate(Right(#Data, 4)
+ SubString(#Data, 3, 2)
+ Left(#Data, 2)) = 1
Then Right(#Data, 4)
+ SubString(#Data, 3, 2)
+ Left(#Data, 2) End As DateTime)
Notice that this code should correctly handle invalid dates contained within your varchar column. If a date is invalid, this code will return NULL.
try parsing your varchar into dd/mm/yyyy before attempting the cast:
cast(substring([datestring],1,2) + '/' +
substring([datestring],3,2) + '/' +
substring([datestring],5,4) as datetime)