Convert string to date in table sql query
Asked by Anders Kj?llerstr?m:

Hi all!

I'm using a Table Transformer Macro to create a table where one column is the the first ten characters of the title string, which represent a date.

Example:

2021-12-12 Super Important Meeting returns 2021-12-12

This part works fine. My remaining issue is that the result is not defined as date format..

I tried using both CONVERT and PARSE with a fixed date rather than the expression above, but this does not work out.

Any help is much appreciated!

Merry Christmas!

anders

```
CONVERT(DATETIME,'2021-12-12', 23) AS 'test',
```

returns:
> Error: The CONVERT style 23 is not realized yet.

```
Parse('2021-12-12' AS DATE USING 'sv-SE') AS 'test',
```

returns:
> SyntaxError: Parse error on line 1: ... Parse(\`2021-12-21\` AS DATE USING \`sv-SE -----------------------\^ Expecting 'EOF', 'COMMA', 'RPAR', 'IN', 'LIKE', 'ARROW', 'DOT', 'CARET', 'EQ', 'SLASH', 'EXCLAMATION', 'MODULO', 'GT', 'LT', 'GTGT', 'LTLT', 'NOT', 'AND', 'OR', 'PLUS', 'STAR', 'RBRA', 'END', 'ELSE', 'REGEXP', 'TILDA', 'GLOB', 'NOT_LIKE', 'BARBAR', 'MINUS', 'AMPERSAND', 'BAR', 'GE', 'LE', 'EQEQ', 'EQEQEQ', 'NE', 'NEEQEQ', 'NEEQEQEQ', 'BETWEEN', 'NOT_BETWEEN', 'IS', 'DOUBLECOLON', 'GO', 'SEMICOLON', got 'AS'


Answers:
author: Katerina Kovriga {Stiltsoft}
body: Hi [@Anders Kj?llerstr?m](/t5/user/viewprofilepage/user-id/4515898) ,

I assume you have the following case:

![Wed 7-1.png](https://community.atlassian.com/t5/image/serverpage/image-id/175015i8755AE1A2190360B/image-size/large?v=v2&px=999 "Wed 7-1.png")

![Wed 7-2.png](https://community.atlassian.com/t5/image/serverpage/image-id/175016iA6518A32C7B8A579/image-size/large?v=v2&px=999 "Wed 7-2.png")

SELECT SUBSTRING(T1.'Page Title', 1, 10) AS 'Date', \* FROM T1

![Wed 7-3.png](https://community.atlassian.com/t5/image/serverpage/image-id/175017i4460863A51C77B3B/image-size/large?v=v2&px=999 "Wed 7-3.png")

Now your date is a string and the simplest way to perform any manipulations with it as a real date is to use another Table Transformer macro with a correctly specified date format:

![Wed 7-4.png](https://community.atlassian.com/t5/image/serverpage/image-id/175018iDAF7BBAF19606CE2/image-size/large?v=v2&px=999 "Wed 7-4.png")

SELECT DATEDIFF(day, 'Date', "today") as 'Datediff',  
\* FROM T\*

![Wed 7-5.png](https://community.atlassian.com/t5/image/serverpage/image-id/175019iAFEB994A8B413D8D/image-size/large?v=v2&px=999 "Wed 7-5.png")

![Wed 7-6.png](https://community.atlassian.com/t5/image/serverpage/image-id/175020i97D3C8C118A22EB7/image-size/large?v=v2&px=999 "Wed 7-6.png")

And if you want to use other macros on top of the first Table Transformer macro, then you also don't need to convert the dates, just choose an appropriate date format in the macro settings.

Here is an example with the Pivot macro:

![Wed 7-7.png](https://community.atlassian.com/t5/image/serverpage/image-id/175021i0ACA57F1EABFC069/image-size/large?v=v2&px=999 "Wed 7-7.png")

![Wed 7-8.png](https://community.atlassian.com/t5/image/serverpage/image-id/175022i43080C1088EA4F68/image-size/large?v=v2&px=999 "Wed 7-8.png")

Hope this helps your case.

comments: author: Anders Kj?llerstr?m
body: Hi Katerina!

Thanks for helping out (again)!

Yes, your assumption of the start of my case is correct, but my issue is slightly different.

My problem was that when applying a filter to the column, the date column did not respond to filtering, I assumed wrongly that this was because it was not defined as date format.![Filtercase.png](https://community.atlassian.com/t5/image/serverpage/image-id/175029i093783928F8CB0E4/image-size/large?v=v2&px=999 "Filtercase.png")

While playing around with it a bit further, I've found the solution. Putting it here for fellow rookies :)

In the picture you can see that the date format is different in the date column and the date filter. I just had to change the date filter format to correspond to the column format. It can be done temporarily directly in the user view of the table or (better) permanently here:

![Filtercase2.png](https://community.atlassian.com/t5/image/serverpage/image-id/175031i3D8BE9C09663A3D6/image-size/large?v=v2&px=999 "Filtercase2.png")

I'm still in the dark as to why the filter differentiate the different types of date format. To me they all represent the same date value in the end. This seems to be more of a fundamental difference from my previous experience (i'm old), maybe it is just an effect of trying to make the administration more user friendly and not relying on administrators having coding experience? Could you [@Katerina Kovriga {Stiltsoft}](/t5/user/viewprofilepage/user-id/1464746) or anyone else point to some reading explaining this on a dummy level?

Anyway, main problem solved, thanks!

anders


author: Katerina Kovriga {Stiltsoft}
body: Here is [our documentation](https://docs.stiltsoft.com/display/public/TFAC/How+to+set+the+date+format+and+worklog+settings) about the dates' formats and their correct settings inside our macros.

You need to always check the specified date format - our macros "remember" the last used format and when you insert another macro on the page, the date format copies the previous use case.

Why do you need to specify the date format: for our macros to work correctly using the calendar. For example, the same date as 2021-12-01 could be 01 Dec 2021 or 12 Jan 2021. And it is important while filtering, aggregating by periods, counting datediffs, etc.


author: Katerina Kovriga {Stiltsoft}
body: Hi there,

As this Community question mentions our [Table Filter and Charts for Confluence](https://marketplace.atlassian.com/apps/27447/table-filter-and-charts-for-confluence?tab=overview&hosting=cloud&utm_source=atl_community) app, we are happy to introduce its new macro -- [Table Spreadsheet](https://docs.stiltsoft.com/display/public/TFAC/How+to+use+Table+Spreadsheet+macro?utm_source=atl_community).

The macro allows you to work with fully functional Excel spreadsheets right in Confluence.

You'll be able to use cells' formulas, filters, conditional formatting, etc., create pivot tables and charts from the page view and edit modes.

The Table Spreadsheet macro is available for Cloud and Server/Data Center.

![](https://docs.stiltsoft.com/download/attachments/83100938/Conditional%20formatting.png?version=1&modificationDate=1641987630000&api=v2&effects=border-simple,shadow-kn)


Tags: cloud

confluence-cloud