Solved

select date between startdate and enddate return no row :S

Posted on 2012-12-26
10
445 Views
Last Modified: 2013-01-09
i have two table ..
SELECT TOP 1000 [ProgramID]
      ,[ProgramName]
      ,[ProgramStart]
      ,[ProgramEnd]
      ,[ProgramWeekDay]
  FROM [radiomix].[dbo].[MixFmProgram]

Open in new window



ProgramID	ProgramName	ProgramStart	ProgramEnd	ProgramWeekDay
63	¿¿ ¿¿¿¿¿¿	12:00AM	1:00AM	WE
64	¿¿¿¿¿¿ ¿¿¿¿¿¿ General Music	1:00AM	4:00AM	WE
65	¿¿¿¿¿ ¿¿¿¿ Religious 	4:00AM	4:30AM	WE
66	¿¿¿¿¿¿ ¿¿¿¿¿¿ General Music	4:30AM	6:00AM	WE
67	¿¿¿¿¿¿	6:00AM	10:00AM	WE
68	¿¿¿¿¿ ¿¿¿¿	10:00AM	11:00AM	WE
69	¿¿¿¿ ¿¿¿¿	11:00AM	1:00PM	WE
70	¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿	1:00PM	2:00PM	WE
71	¿¿¿ ¿¿¿¿¿	2:00PM	4:00PM	WE

Open in new window



SELECT TOP 1000 [id]
      ,[datetimein]
      ,[msisdn]
      ,[reqport]
      ,[message]
      ,[DAY]
      ,[ProgramIDM]
  FROM [radiomix].[dbo].[chat]

Open in new window



id	datetimein	msisdn	reqport	message	DAY	ProgramIDM
381996	2012-12-26 16:25:43.043	966568946632	630590	¿¿¿¿ ¿¿¿¿ ¿¿ ¿¿¿¿¿¿ ¿¿¿¿¿ ¿¿¿¿¿ ¿¿¿¿¿¿ ¿¿¿ ¿¿¿¿¿  0568946632	WE	0
381995	2012-12-26 16:24:00.483	966568946632	630590	¿¿¿¿ ¿¿¿¿ ¿¿ ¿¿¿¿¿¿ ¿¿¿¿¿ ¿¿¿¿¿ ¿¿¿¿¿¿ ¿¿¿ ¿¿¿¿¿  0568946632	WE	0

Open in new window




my query to select the program name :

SELECT a.*
	  ,p.*
	  ,RIGHT(CONVERT(VARCHAR,[datetimein],100),7)
  FROM [radiomix].[dbo].[chat] a
 inner JOIN [radiomix].[dbo].[MixFmProgram] p ON (a.DAY = p.ProgramWeekDay)
   WHERE a.ID = '381963'
 AND RIGHT(CONVERT(VARCHAR,a.datetimein,100),7) > p.ProgramStart 
  and RIGHT(CONVERT(VARCHAR,a.datetimein,100),7) < p.ProgramEnd

Open in new window


result no  record :S


why ?? if i remove the restriction..
i get all possibility :


ProgramStart	ProgramEnd	ProgramWeekDay	msgdatetime
12:00AM	1:00AM	WE	 3:58PM
1:00AM	4:00AM	WE	 3:58PM
4:00AM	4:30AM	WE	 3:58PM
4:30AM	6:00AM	WE	 3:58PM
6:00AM	10:00AM	WE	 3:58PM
10:00AM	11:00AM	WE	 3:58PM
11:00AM	1:00PM	WE	 3:58PM
1:00PM	2:00PM	WE	 3:58PM
2:00PM	4:00PM	WE	 3:58PM
4:00PM	6:00PM	WE	 3:58PM
6:00PM	7:00PM	WE	 3:58PM
7:00PM	8:00PM	WE	 3:58PM
8:00PM	9:00PM	WE	 3:58PM
9:00PM	10:00PM	WE	 3:58PM
10:00PM	10:30PM	WE	 3:58PM
10:30PM	12:00AM	WE	 3:58PM

Open in new window

0
Comment
Question by:afifosh
  • 5
  • 4
10 Comments
 
LVL 8

Expert Comment

by:hpdvs2
ID: 38721149
Is ProgramStart and ProgramEnd just varchars?  Because your comparing Greater / Less than with strings, not dates.


>>>  RIGHT(CONVERT(VARCHAR,a.datetimein,100),7) < p.ProgramEnd  <<<


try this: CAST(...) AS TIME, so this:


SELECT a.*
        ,p.*
        ,RIGHT(CONVERT(VARCHAR,[datetimein],100),7)
  FROM [radiomix].[dbo].[chat] a
 inner JOIN [radiomix].[dbo].[MixFmProgram] p ON (a.DAY = p.ProgramWeekDay)
   WHERE a.ID = '381963'
 AND CAST(RIGHT(CONVERT(VARCHAR,a.datetimein,100),7)) AS TIME > CAST(p.ProgramStart) AS TIME
  and CAST(RIGHT(CONVERT(VARCHAR,a.datetimein,100),7)) AS TIME < CAST(p.ProgramEnd) AS TIME
0
 
LVL 1

Author Comment

by:afifosh
ID: 38721222
Msg 1035, Level 15, State 10, Line 7
Incorrect syntax near 'CAST', expected 'AS'.
0
 
LVL 1

Author Comment

by:afifosh
ID: 38721223
the format of programstart and programend is like 1:00Am
and datetimein is a datetime i should get the time only and compare

and i have already have two field in each tables to check weekday !
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 38721320
the format of programstart and programend is like 1:00Am
If you treat that as a string then 9:00AM > 11:00AM  or 9:00AM > 2:00PM Is that really what you want?
0
 
LVL 1

Author Comment

by:afifosh
ID: 38721327
yes i have write this query if we can simplify it .. on convert and like this
the data stored in programstart is varchar type with format 1:00am 2:00pm

and i have a datetimein in another table with type datetime i should take the part time and compare
SELECT top 2 a.id,p.ProgramID,a.ProgramIDM
  FROM [radiomix].[dbo].[chat] a
 inner JOIN [radiomix].[dbo].[MixFmProgram] p ON (a.DAY = p.ProgramWeekDay)
   WHERE 
  CONVERT(varchar,[datetimein], 114) >= convert(varchar, CONVERT(datetime,p.ProgramStart), 114)
and CONVERT(varchar,[datetimein], 114) < convert(varchar, CONVERT(datetime,p.ProgramEnd), 114 )
 order by a.id desc

Open in new window

0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 38721334
If you are just trying to compare times than using a style of 114 should be acceptable for comparisons.
0
 
LVL 1

Author Comment

by:afifosh
ID: 38721341
if you can tell me how i can convert a varchar 2:00pm to hh:mm:ss 24hr
and same the datetime to a var char hh:mm:ss 24hr
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 500 total points
ID: 38721375
if you can tell me how i can convert a varchar 2:00pm to hh:mm:ss 24hr
You can convert 2:00pm to either time or datetime as follows:
CAST('02:00pm' AS time)
CAST('02:00pm' AS datetime)
0
 
LVL 1

Author Comment

by:afifosh
ID: 38721382
select  CAST('02:00pm' AS time)
 CAST('02:00pm' AS datetime)

 Msg 102, Level 15, State 1, Line 2
Incorrect syntax near '02:00pm'.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 38722528
You do realize you are missing a comma, right?  As in:
select  CAST('02:00pm' AS time) ,
 CAST('02:00pm' AS datetime)
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
migration MS SQL database to Oracle 30 61
SQL Error - Query 6 26
SSAS Hierarchy with columns with folder names 10 14
2 IIF's in Access query 25 31
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question