The CONVERT function operates the same whether there is 120 there or not. One post suggested that 120 made the SQL output into a ‘proper’ date format, which is why I tried it. But the output is exactly the same as the CAST function.
I am unsure whether the issue is with SQL or with KNIME’s interpretation of the data. Which is why I asked.
I could not explain why your MSSQL was behaving the way it is. I found an existing MSSQL database that I could connect to and run a sample query similar to yours. The results are that it returns these fields as DATE and not Varchar/String columns
As I would expect, both cast(xxxxxx as date)
and convert(date, xxxxxx)
return DATE columns from a DATETIME column xxxxxx
The suggestion of the 120 argument being used in this case to output a “proper” date format is, I believe, erroneous. a Date is a “date” and has no inherent “format”. The only time a date has a format is when it is rendered as a string. The format of a date we see in KNIME is KNIME’s representation of the date, and is not the date itself. I believe that the 120, in the context of cast to date is ignored, which is why I also threw in an example 99999 in its place in the above screenshot. As you can see, any number can be supplied as the third parameter in cast(date, xxxxxx , ? ) and does not affect the result.
So that left me wondering why you are not seeing dates.
The answer to that is, I believe, likely to be the jdbc driver that you are using. The above result was achieved using either of the official Microsoft SQL Server drivers shown below.
As you can see, for the jTDS driver, the dates are returned as String columns. So this is actually a jdbc driver issue rather than a KNIME issue, and it appears to be a known bug in jTDS.
Do you have the Microsoft jdbc driver available to try? If not, you may need to install the Microsoft SQL extension in KNIME
With this in mind, a google search for knime jtds database driver date reveals a similar post:
Of course, now that we have a reason, the alternative approach is as @rfeigel 's suggestion of converting the string after it is returned.
A second option, if you don’t want to add the additional node could be that you go to the Input Type Mapping tab on the DB Query reader, and specify the mapping for particular date columns. In this case I used regex for any column I had beginning “cdate”. You may be able to do something similar.
You can also do something similar on the actual Microsoft SQL Server Connector:
Here, I have said that any column containing the word “date” followed by optional digits should be returned as a date: