Solved

Query Date and Time in Different Columns

Posted on 2008-06-16
4
631 Views
Last Modified: 2008-06-16
I have a table that has a modified Julean date stored as a varchar in one column and a time value stored as a varchar in another column.  I would like to be able to query for all values that are between a start date/time and an end date/time.  For example if my start date is 108152 and start time is 175945 and my end date is 108155 and end time is 085500 then I would like all of the values inbetween.  Please provide me with an example where statement.
0
Comment
Question by:schlepuetz
  • 2
4 Comments
 
LVL 68

Accepted Solution

by:
Qlemo earned 125 total points
ID: 21795385
1. if your time and date values are right aligned, you can simply concatenate both to compare:

where juldate + ' ' + jultime between '108152 175945' and '108155 085500'

Otherwise, you will have to convert time and date to number, and compare those:

where convert(float, juldate) + convert(float, jultime)/240000 between ... and ...

I left some stuff to your imagination ;-)

BTW: Bad idea, storing date/time values in varchar ....
0
 
LVL 3

Author Comment

by:schlepuetz
ID: 21795460
I agree with the bad idea thing but that is the nature of the beast when dealing with other people's stuff.  Let me give your suggestions a whirl.
0
 
LVL 51

Expert Comment

by:Mark Wills
ID: 21795802
Time seems to be straight time - 24 hour clock...  So, try these... and convert either Julian to date or date to julian and compare time seperately....

-- JULIAN TO SQL SERVER
declare @date char(10)
set @date=108152
select dateadd(dd,@date-(@date/1000)*1000-1, convert(datetime,'01/01/'+ right(convert(varchar,@date/1000),2))) as Date_From_Julian

-- SQL SERVER DATE TO JULIAN
declare @date datetime
set @date=getdate()
select (100+year(@date)-2000)*1000+ datediff(d, convert(datetime,('01/01/'+convert(char(4),year(@date)))),@date)+1 as Date_To_Julian
 
-- TIME
declare @date datetime
set @date=getdate()
select replace(convert(varchar,@date,108),':','')   --or 114
0
 
LVL 3

Author Comment

by:schlepuetz
ID: 21796702
Here is final solution that I used.
cast([Date]as varchar(6)) + right('000000' + cast([Time]as varchar(6)),6)

I used the right to make sure that I had the same number for the times as some of ny times where not the full 6 characters.

Sorry mark wills I had tried before converting the date and time to normal date time values but the query was too slow.  The difference was 15 seconds versus 2 seconds.

Thanks Qlemo for the push in the right direction!
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now