Skip to main content
Question

MS SQL time functions are not working on Vertica

  • September 7, 2021
  • 5 replies
  • 17 views

bhasreddy1

Hi Team,

I am Bhaskar from OBM Background.

I have developed MS SQL query to get last 7 days events (with Servicenow Integration) . The query is working as expected on MSSQL Database but when I tried it on Vertica it is not working.

Could you please help me in fixing the time functions.

MS SQL Query:

SELECT right(cast([TIME_CREATED] as date),10) as 'Day', count(substring([TEXT],21,10)) as Tickets
FROM [event_schema].[dbo].[EVENT_ANNOTATIONS]
where TIME_CREATED > = convert(datetime,convert(varchar(8),getdate()-7,112))
--and TIME_CREATED between convert(datetime,convert(varchar(8),getdate(),112)) and convert(datetime,convert(varchar(8),getutcdate(),112))
and [text] not like '%changed%'
group by cast(TIME_CREATED as date)

5 replies

bhasreddy1
  • Author
  • Participating Frequently
  • September 7, 2021

Hi Hibiki,

Please find the query and error screenshot

SELECT right(cast(time_created as date),10) as 'Day',
event_id as Events
FROM opr_event
where time_created >= convert(datetime,convert(varchar(8),getdate()-7,112))
group by cast(time_created as date);


bhasreddy1
  • Author
  • Participating Frequently
  • September 7, 2021

Hi Hibiki,

Please check the below error screenshot.


bhasreddy1
  • Author
  • Participating Frequently
  • September 7, 2021

Hi Hibiki,

Please find the below error error.

SELECT to_char(time_created,'YYYY-MM-DD') as 'Day', event_id as Events
FROM opr_event
WHERE time_created >= trunc(getdate()-7,'DD')
group by to_char(time_created,'YYYY-MM-DD');

Then, I modified the query as below. But GROUP BY is not working as expected.

SELECT to_char(time_created,'YYYY-MM-DD') as 'Day', count(event_id) as Event_Count
FROM opr_event
WHERE time_created >= trunc(getdate()-7,'DD')
group by to_char(time_created,'YYYY-MM-DD'), opr_event.event_id;

After adding the ORDER BY then the issue re-occurred.

SELECT to_char(time_created,'YYYY-MM-DD') as 'Day', count(event_id) as Event_Count
FROM opr_event
WHERE time_created >= trunc(getdate()-7,'DD')
group by to_char(opr_event.time_created,'YYYY-MM-DD'), opr_event.event_id
order by opr_event.time_created desc;


bhasreddy1
  • Author
  • Participating Frequently
  • September 7, 2021

Hi Hibiki,

Yes, now its working fine. Thanks a lot and appreciating your effort and quick help.

May I know what time functions should I use if I want to query for weeks, months and years?

We are looking for historical event and metric data for POC Project.


bhasreddy1
  • Author
  • Participating Frequently
  • September 7, 2021

Hi Hibiki,

Below is the exact query which I was looking for.

SELECT to_char(time_created,'YYYY-MM-DD') as 'Day', count(distinct event_id) as Events
FROM opr_event
WHERE time_created >= trunc(getdate()-7,'DD')
group by to_char(time_created,'YYYY-MM-DD')
order by to_char(time_created,'YYYY-MM-DD') desc;

Meanwhile, could you please clarify the need of usage of below syntax.

to_char(time_created,'YYYY-MM-DD')