Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 NULL value on time/date, please help

Author  Topic 

chef423
Starting Member

15 Posts

Posted - 2015-03-11 : 13:11:12
This query is returning a NULL:
SELECT
'Banquets - All Day' as revName,
SUM(t.c_items_total) AS Banquet_Total,
SUM(t.cover_count) as Total_Covers,
SUM(t.c_items_total) / SUM(t.cover_count) as AvgPer_Cover

FROM
dbo.Ticket AS t JOIN
dbo.PeriodDefinition AS pd ON CAST(t.dt_close_time AS time) BETWEEN CAST(pd.dt_start AS time) AND CAST(pd.dt_finish AS time) AND
pd.i_period_definition_id = 4 and t.i_revcenter_id = 7 and t.i_void_ticket_id IS NULL and t.b_closed = 1

There are 4 dayParts in the software:
Lunch, i_period_definition_id = 1 '1899-12-30 10:00:00.000' to
'1899-12-30 15:00:00.000'
Dinner, i_period_definition_id = 2 '1899-12-30 15:00:00.000' to
'1899-12-30 22:00:00.000'
Breakfast, i_period_definition_id = 3 '1899-12-30 04:00:00.000' to
'1899-12-30 10:00:00.000'
LateNight, i_period_definition_id = 4 '1899-12-30 22:00:00.000' to
'1899-12-30 04:00:00.000'


The reason is (the why) the i_period_definition_id = 4 is from '1899-12-30 22:00:00.000' to '1899-12-30 04:00:00.000' - this returns Ticket totals for the time range, this is Point of Sale software. So from 10pm to the following day at 4am. I just lack the syntax to make this why, I know why it doesn't work...but lack the language to fix.

A 24 day in this software is from 4am to 4am..but as we know our clock is from 12am to 12am...so query retuns a NULL due to the fact the period_definition is from 10pm to 4am. If I change '1899-12-30 04:00:00.000' TO '1899-12-30 23:59:59.000', the query returns the totals, but they close Tickets after Midnight.

Can someone help?

Thanks.

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2015-03-11 : 13:35:49
You didn't mention if your time fields are just times or datetimes. I assumed the latter.


SELECT
'Banquets - All Day' as revName,
SUM(t.c_items_total) AS Banquet_Total,
SUM(t.cover_count) as Total_Covers,
SUM(t.c_items_total) / SUM(t.cover_count) as AvgPer_Cover

FROM dbo.Ticket AS t
CROSS JOIN dbo.PeriodDefinition AS pd
WHERE t.dt_close_time >= pd.dt_start and t.dt_close_time <= pd.dt_finish
AND pd.i_period_definition_id = 4
AND t.i_revcenter_id = 7
AND t.i_void_ticket_id IS NULL
AND t.b_closed = 1
Go to Top of Page

chef423
Starting Member

15 Posts

Posted - 2015-03-11 : 13:44:19
quote:
Originally posted by gbritton

You didn't mention if your time fields are just times or datetimes. I assumed the latter.


SELECT
'Banquets - All Day' as revName,
SUM(t.c_items_total) AS Banquet_Total,
SUM(t.cover_count) as Total_Covers,
SUM(t.c_items_total) / SUM(t.cover_count) as AvgPer_Cover

FROM dbo.Ticket AS t
CROSS JOIN dbo.PeriodDefinition AS pd
WHERE t.dt_close_time >= pd.dt_start and t.dt_close_time <= pd.dt_finish
AND pd.i_period_definition_id = 4
AND t.i_revcenter_id = 7
AND t.i_void_ticket_id IS NULL
AND t.b_closed = 1




Thank you for the response...Yes, DATETIME, my apology.

The WHERE clause wont work due to WHERE %PARAMS% - where Params is the time and date range selected by the User in the UI of the software.

So only SELECT and FROM clauses can be used.

Thank you.

EDIT: I am still getting a NULL with the above Query, as-is.
Go to Top of Page

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2015-03-11 : 13:48:57
Hmmm... not good practice. So keep the last bit as


FROM dbo.Ticket AS t
JOIN dbo.PeriodDefinition AS pd
ON t.dt_close_time >= pd.dt_start and t.dt_close_time <= pd.dt_finish
AND pd.i_period_definition_id = 4
AND t.i_revcenter_id = 7
AND t.i_void_ticket_id IS NULL
AND t.b_closed = 1
Go to Top of Page

chef423
Starting Member

15 Posts

Posted - 2015-03-11 : 14:16:04
quote:
Originally posted by gbritton

Hmmm... not good practice. So keep the last bit as


FROM dbo.Ticket AS t
JOIN dbo.PeriodDefinition AS pd
ON t.dt_close_time >= pd.dt_start and t.dt_close_time <= pd.dt_finish
AND pd.i_period_definition_id = 4
AND t.i_revcenter_id = 7
AND t.i_void_ticket_id IS NULL
AND t.b_closed = 1




Yea, still getting a NULL.

The fact that the DATE changes at midnight is the issue....so there needs to be a DATEADD + 1 somehow/where?
Go to Top of Page

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2015-03-11 : 14:24:12
Tell me. are pd.dt_start an pd.dt_finish datetime types or just time values? If they have the dates, you should be fine. Post a few samples if you can.
Go to Top of Page

chef423
Starting Member

15 Posts

Posted - 2015-03-11 : 14:34:09
quote:
Originally posted by gbritton

Tell me. are pd.dt_start an pd.dt_finish datetime types or just time values? If they have the dates, you should be fine. Post a few samples if you can.



DateTime Types, if I am correct in saying that...

i_period_definition_id, s_name, dt_start, dt_finish
1, Lunch, 1899-12-30 10:00:00.000, 1899-12-30 15:00:00.000
Go to Top of Page

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2015-03-11 : 14:38:56
so, a ticket with a given close time must fall between those times,right?

If a 24-hour period begins a 4 am, a typical one might be

2015-03-11 04:00:00.000 2015-03-12 03:59:59:999

correct?

so, a ticket closed at, say 12:30 on the 12th would fall into this interval, right?
Go to Top of Page

chef423
Starting Member

15 Posts

Posted - 2015-03-11 : 14:52:42
quote:
Originally posted by gbritton

so, a ticket with a given close time must fall between those times,right?

If a 24-hour period begins a 4 am, a typical one might be

2015-03-11 04:00:00.000 2015-03-12 03:59:59:999

correct?

so, a ticket closed at, say 12:30 on the 12th would fall into this interval, right?



No, the time is from 04:00:00.000 to 04:00:00.000
AS you see below, there are no gaps...where one ends, the other begins. So a ticket close time can be, for example: 2014-05-01 21:59:01.097

There are 4 dayParts in the software:
Lunch, i_period_definition_id = 1 '1899-12-30 10:00:00.000' to
'1899-12-30 15:00:00.000'
Dinner, i_period_definition_id = 2 '1899-12-30 15:00:00.000' to
'1899-12-30 22:00:00.000'
Breakfast, i_period_definition_id = 3 '1899-12-30 04:00:00.000' to
'1899-12-30 10:00:00.000'
LateNight, i_period_definition_id = 4 '1899-12-30 22:00:00.000' to
'1899-12-30 04:00:00.000'
Go to Top of Page

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2015-03-11 : 15:42:28
Why does period 4 start at 10 p.m. and end at 4 a.m. on the same day? shouldn't the end date be the next day?

Also, Change the join predicate to:

ON t.dt_close_time >= pd.dt_start and t.dt_close_time < pd.dt_finish -- strictly less than
AND ...
Go to Top of Page

chef423
Starting Member

15 Posts

Posted - 2015-03-11 : 17:01:41
quote:
Originally posted by gbritton

Why does period 4 start at 10 p.m. and end at 4 a.m. on the same day? shouldn't the end date be the next day?

Also, Change the join predicate to:

ON t.dt_close_time >= pd.dt_start and t.dt_close_time < pd.dt_finish -- strictly less than
AND ...



"A 24 day in the Point of Sale software is from 4am to 4am [why? because bars and restaurants are open past midnight and the 'business day' is from 4am to 4am]..but as we know our clock is from 12am to 12am...so query retuns a NULL due to the fact the period_definition is from 10pm to 4am. If I change '1899-12-30 04:00:00.000' TO '1899-12-30 23:59:59.000', the query returns the totals, but they close Tickets after Midnight."
Go to Top of Page

ScottPletcher
Aged Yak Warrior

550 Posts

Posted - 2015-03-11 : 17:46:20
[code]

SELECT
'Banquets - All Day' as revName,
SUM(t.c_items_total) AS Banquet_Total,
SUM(t.cover_count) as Total_Covers,
SUM(t.c_items_total) / SUM(t.cover_count) as AvgPer_Cover

FROM
dbo.Ticket AS t JOIN
dbo.PeriodDefinition AS pd ON
CAST(t.dt_close_time AS time) BETWEEN CAST(pd.dt_start AS time) AND
CASE WHEN CAST(pd.dt_start AS time) > CAST(pd.dt_finish AS time)
THEN '23:59:59.997'
ELSE CAST(pd.dt_finish AS time) END AND
(CAST(pd.dt_start AS time) < CAST(pd.dt_finish AS time) OR
CAST(t.dt_close_time AS time) <= CAST(pd.dt_finish AS time)) AND
pd.i_period_definition_id = 4 and
t.i_revcenter_id = 7 and
t.i_void_ticket_id IS NULL and
t.b_closed = 1

[/code]
Go to Top of Page

chef423
Starting Member

15 Posts

Posted - 2015-03-11 : 18:06:02
Still NULL.

Thanks for the response Scott.
Go to Top of Page

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2015-03-11 : 19:09:37
Your period table is messed up. I asked:

"Why does period 4 start at 10 p.m. and end at 4 a.m. on the same day? shouldn't the end date be the next day?"

You reiterated that the workday is 4am to 4am but didn't answer my question.

Earlier you posted this example:

LateNight, i_period_definition_id = 4 '1899-12-30 22:00:00.000' to
'1899-12-30 04:00:00.000'

but the end datetime is before the start datetime, since it is the same day. That just can't be correct

but the dt_finish
Go to Top of Page

chef423
Starting Member

15 Posts

Posted - 2015-03-11 : 19:26:19
quote:
Originally posted by gbritton

Your period table is messed up. I asked:

"Why does period 4 start at 10 p.m. and end at 4 a.m. on the same day? shouldn't the end date be the next day?"

You reiterated that the workday is 4am to 4am but didn't answer my question.

Earlier you posted this example:

LateNight, i_period_definition_id = 4 '1899-12-30 22:00:00.000' to
'1899-12-30 04:00:00.000'

but the end datetime is before the start datetime, since it is the same day. That just can't be correct

but the dt_finish



Can I dropbox share the DB? Its all test data...

Just a .bak file, SQL 2008 R2
Go to Top of Page

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2015-03-12 : 08:35:30
Just post a few rows of your input tables and the results you want to see -- using those rows as input.
Go to Top of Page

ScottPletcher
Aged Yak Warrior

550 Posts

Posted - 2015-03-12 : 15:31:40
With no test data, I can't test it myself, so good luck from here.
Go to Top of Page
   

- Advertisement -