Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Select Starttime Expand / Collapse
Author
Message
Posted Thursday, April 15, 2010 7:22 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, December 15, 2010 12:39 PM
Points: 9, Visits: 305
Hi All,
Am trying to retrieve Start time From a table but the criteria is
i need to retrieve data between certain timing,The StartTime datatype is nvarchar and its in the format like '12:30 AM'


The query is :

SELECT
StartTime
FROM
TicketTransaction
WHERE
StartTime >= '12:30 PM'
AND StartTime <= '05:30 PM'
Post #904006
Posted Thursday, April 15, 2010 7:33 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Friday, December 14, 2012 2:23 AM
Points: 244, Visits: 409
should not query be like

SELECT
StartTime
FROM
TicketTransaction
WHERE
StartTime <= '12:30 PM'
AND StartTime >= '05:30 PM'


Regards,
MShenel
Post #904014
Posted Thursday, April 15, 2010 11:15 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, December 15, 2010 12:39 PM
Points: 9, Visits: 305
Thanks Shenel its executing the output but not exactly the record i need
for eg:


SELECT
StartTime
FROM
TicketTransaction
WHERE
StartTime <= '12:30 PM'
AND StartTime >= '05:30 PM'

The Output like :
05:45 PM
06:16 PM
06:16 PM
06:23 PM
06:23 PM
12:23 PM
12:23 PM
10:21 AM
06:15 PM
06:24 PM
06:36 PM
06:49 PM

But i need a out put between 12:30 pm and 05:30 pm

Regards
kjkeyan
Post #904557
Posted Saturday, April 17, 2010 7:54 AM


SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Monday, May 20, 2013 5:31 AM
Points: 275, Visits: 751
Try:

SELECT StartTime
FROM TicketTransaction
WHERE
convert(datetime,StartTime) <= '12:30 PM'
AND
convert(datetime,StartTime) >= '05:30 PM'

You should also consider permanently changing the datatype of this column to datetime, or perhaps time.

/SG
Post #905405
Posted Sunday, April 18, 2010 7:06 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 4:22 PM
Points: 10,990, Visits: 10,542
Stefan_G (4/17/2010)
You should also consider permanently changing the datatype of this column to datetime, or perhaps time.

Absolutely, yes. Problems related to storing dates and times as something other than a true date/time type come up again and again on these forums. I have yet to encounter a satisfactory reason to store date/time data like that - it just makes life harder, to no advantage.




Paul White
SQL Server MVP
SQLblog.com
@SQL_Kiwi
Post #905639
Posted Sunday, April 18, 2010 11:16 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, December 15, 2010 12:39 PM
Points: 9, Visits: 305
HI,
Thanks for your suggestion ,but i have tried this query

SELECT StartTime
FROM TicketTransaction
WHERE
convert(datetime,StartTime) <= '12:30 PM'
AND
convert(datetime,StartTime) >= '05:30 PM'

Its not working and i am getting an error
'Conversion failed when converting datetime from character string'

And i have tried this query


SELECT convert( datetime,StartTime,109)
FROM TicketTransaction
WHERE
StartTime <= '12:30 PM'
AND
StartTime >= '05:30 PM'

Its giving the output but not exact record .

Thanks and regards

kjkeyan


Post #905693
Posted Monday, April 19, 2010 12:25 AM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Sunday, March 03, 2013 10:42 PM
Points: 179, Visits: 561
Hi,

Try the below script. But changing datatype is the best option what Stefan and Paul White’s suggested. Otherwise you need to do lot of date conversions to get the result.

SELECT StartTime
FROM(SELECT StartTime, CAST(CONVERT(NVARCHAR(20), GETDATE(), 23) + ' ' + StartTime AS DATETIME) ConvertedStartTime
FROM TicketTransaction) AS T1
WHERE
CONVERT(NVARCHAR(20), ConvertedStartTime, 8) <= CONVERT(NVARCHAR(20), '12:30 PM', 8)
AND
CONVERT(NVARCHAR(20), ConvertedStartTime, 8) >= CONVERT(NVARCHAR(20), '05:30 PM', 8)
Post #905714
Posted Monday, April 19, 2010 2:33 AM


SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Monday, May 20, 2013 5:31 AM
Points: 275, Visits: 751
kjkeyan (4/18/2010)
HI,
Thanks for your suggestion ,but i have tried this query

SELECT StartTime
FROM TicketTransaction
WHERE
convert(datetime,StartTime) <= '12:30 PM'
AND
convert(datetime,StartTime) >= '05:30 PM'

Its not working and i am getting an error
'Conversion failed when converting datetime from character string'

And i have tried this query


SELECT convert(datetime, StartTime, 109)
FROM TicketTransaction
WHERE
StartTime <= '12:30 PM'
AND
StartTime >= '05:30 PM'

Its giving the output but not exact record .


It sounds like you have data in the table that is not in a valid format.

Try using

SELECT MAX(convert(datetime, StartTime, 109))
FROM TicketTransaction

and see if you get any error.

If you get an error, you definitely have bad data in the table, and you should be able to find the offending row(s) using this query:

SELECT *
FROM TicketTransaction
WHERE ISDATE(StartTime) = 0

After fixing the bad data in the table, my original query should work.

/SG
Post #905768
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse