extracting only date from datetime
Asked by Rachi Manwal:

Hi, I was trying to extract only date from date time, but not sure which function to use. It would be very helpful if someone can guide.

SELECT DATEADD(DAY,1-WEEKDAY(NOW()),NOW())

output: **Mon Sep 26 2022 21:09:46 GMT+0530 (India Standard Time)**

expected output: I only want date "**Sep 26 2022**"


Answers:
author: Katerina Kovriga {Stiltsoft}
body: Hi [@Rachi Manwal](/t5/user/viewprofilepage/user-id/5036129) ,

By the provided query I assume you are using the [Table Transformer](https://docs.stiltsoft.com/display/public/TFAC/How+to+use+Table+Transformer+macro?utm_source=atl_community&utm_medium=answer&utm_campaign=table-filter-traffic-sources&utm_vendorID=1216111) macro provided by our app that is [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&utm_medium=answer&utm_campaign=table-filter-traffic-sources&utm_vendorID=1216111).

If this is so, go to the macro settings -\> Settings tab -\> Date format. Specify the desired output format. For example, "Sep 26 2022" corresponds the "M d yy" string (type it manually).

Then return to your query and add the FORMATDATE function before the manipulations with dates, for example, for the provided piece:

SELECT **FORMATDATE** (DATEADD(DAY,1-WEEKDAY(NOW()),NOW()))  
FROM T1

This should help your case.

comments: author: Rachi Manwal
body: Hi [@Katerina Kovriga {Stiltsoft}](/t5/user/viewprofilepage/user-id/1464746) Thank you, I was able to extract date part from the complete date. However, greater than and smaller than are not working fine after I formatted the date.

++This is the query, I have used:-++

SELECT \[Assigned on Date\], \[Supported/Approved Date\]  
from T2 where FORMATDATE(\[Assigned on Date\]) \>= FORMATDATE(DATEADD(DAY,1-WEEKDAY(NOW()),NOW()))  
or FORMATDATE(\[Supported/Approved Date\]) \>= FORMATDATE(DATEADD(DAY,1-WEEKDAY(NOW()),NOW()))![Capture.PNG](https://community.atlassian.com/t5/image/serverpage/image-id/218266i3ECC10034253ACC2/image-size/large?v=v2&px=999 "Capture.PNG")


author: Katerina Kovriga {Stiltsoft}
body: Hi [@Rachi Manwal](/t5/user/viewprofilepage/user-id/5036129) ,

As I understand now, you are trying to compare your dates with the today's date plus/minus several days.

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

For example, for this table I need only dates with a red background: today is 28 Sep 2022, so the start date will be 26 Sep 2022. It means that it will be "today - 3 days". Then every day the "today" date moves and the start date will be 27 Sep 2022, 28 Sep 2022, etc.

Here is my SQL query:

SELECT T2.'Assigned on Date', T2.'Supported/Approved Date'  
FROM T2 WHERE DATEDIFF(day, 'Assigned on Date', CURRENT_TIMESTAMP) \<= 3 OR  
DATEDIFF(day, 'Supported/Approved Date', CURRENT_TIMESTAMP) \<= 3

And here is the result:

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

As you can see, this query works without additional FORMATDATE functions and looks rather simple - hope it helps.


author: Rachi Manwal
body: Actually I am comparing the dates with the current week start date, as I have to filter records which are created in the current week, and for the same I am using below query. But through this time is getting included in the comparison which is giving me incorrect results

SELECT \* FROM T1  
WHERE (DATEADD(DAY,0,\[Assigned on Date\]) \>= DATEADD(DAY,1-WEEKDAY(NOW()),NOW()) AND DATEADD(DAY,0,\[Assigned on Date\]) \<= DATEADD(DAY,7-WEEKDAY(NOW()),NOW()))


author: Katerina Kovriga {Stiltsoft}
body: Please use the two Table Transformers for this table:

1) The internal TT will add a new column with a date that corresponds to the start of the week

SELECT \*,  
FORMATDATE(DATEADD(DAY,1-WEEKDAY(NOW()),NOW())) as 'Start of the week'  
FROM T1

2) The external TT will compare the dates:

SELECT T1.'Assigned on Date', T1.'Supported/Approved Date'  
FROM T1  
WHERE T1.'Assigned on Date' \>= T1.'Start of the week'  
OR T1.'Supported/Approved Date' \>= T1.'Start of the week'

So, it is your original query, but we simplify it (split it into two parts). In this case it works correctly for me.


author: Rachi Manwal
body: Thank you so much, it worked but I have one query why it didn't worked with single TT


author: Katerina Kovriga {Stiltsoft}
body: As it's not a fully functional SQL database, some complex combined queries may work unstable. Your idea was right, sorry for the inconvenience.


author: Rachi Manwal
body: It's fine, thank you for the quick help


Tags: filter

table

table-filter

table-filter-and-charts

table-transformer