Score:0

SQL Converting strings to date

us flag

this has really baffled me for a few days and am seeking help. 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 of "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 Sever 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 as it's doing my head in.

Thanks in advance Paul

Score:1
cn flag

I'm not sure how well suited your database is to doing this. The data conversion functions in SQL Server are a bit "clumsy", compared to some others.

However, from your database's point of view:

If something is supposed to be a Date, then it should be a date.
It might be "missing" (i.e. nullable) but it can't be anything else.
If it were to be anything else, then it should not be treated as a Date.

Users cannot enter Dates - only the Character Representations of them.
Displaying [numbers and] dates from your application requires formatting on the "way out".
Reading [numbers and] dates into your application requires the reverse, "un-formatting" (and validation, of course) on the "way in".
Data should always be stored in the correct Data Type - "The Right Tool for the Right Job".

Free-text parsing is probably best done in your application, extracting the date value, validating and "un-formatting" it and then storing it.

Michael Hampton avatar
cz flag
Please try to avoid answering off-topic questions.
Phill  W. avatar
cn flag
Answering the question is one thing. Guiding the poster to question the question can often be better because they may be trying to solve the /wrong/ problem. I do not regard guiding people away from [frankly] Bad Practices that /will/ come back and bite them later as "off topic". Date handling is hard enough as it is when you know you're working with a date. Adding ifs and buts and maybes about whether what you're looking is a date or not will drive you and your database to madness.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.