473,387 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Select Between 17:00 Yesterday and 17:00 Today

code green
1,726 Expert 1GB
I can get the whole of yesterday and today using this query
Expand|Select|Wrap|Line Numbers
  1. SELECT site, ord_date,
  2. LEFT(RTRIM(LTRIM(descr)),24) account
  3. FROM orders
  4. WHERE ord_date BETWEEN 
  5. CAST(CAST(DATEADD(DAY,-1,GETDATE()) AS INT) AS DATETIME)
  6.  AND 
  7. CAST(GETDATE() AS DATETIME)
Casting GETDATE to INT truncates the time element and casting back to DATETIME resets the time element to midnight.
I figured if I did something like this it would give me 17:00 yesterday
Expand|Select|Wrap|Line Numbers
  1. CAST(CAST(DATEADD(DAY,-1,GETDATE()) AS INT)+' 17:00:00.000' AS DATETIME)
But error:
Syntax error converting the varchar value ' 17:00:00.000' to a column of data type int.
I must be close.
Any help appreciated
Dec 31 '09 #1
5 7362
ck9663
2,878 Expert 2GB
Here, read some of this...

Happy Coding!!!

~ CK
Dec 31 '09 #2
code green
1,726 Expert 1GB
Here, read some of this
That doesn't teach me anything.
String literals are used for the dates throughout.
Or did I miss something?
Jan 1 '10 #3
Please try this.

DATEADD( HH, 17, CAST(CAST(DATEADD(DAY,-1,GETDATE()) AS INT) AS DATETIME))

-bala
Jan 4 '10 #4
code green
1,726 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. DATEADD( HH, 17, CAST(CAST(DATEADD(DAY,-1,GETDATE()) AS INT) AS DATETIME))
  2.  
Good Thinking! Add 17 hours to midnight. Wow. I'll try that. Thanks.
Jan 4 '10 #5
code green
1,726 Expert 1GB
Just an update for anybody interested.
CAST ...TO INT is unreliable.
It can flip a day either way, probably something to do with rounding.
CONVERT to a date works better. then CAST back to DATETIME to get midnight.
And simply DATEADD any number of hours you like
Expand|Select|Wrap|Line Numbers
  1. DATEADD(HH,17,CAST(CONVERT(VARCHAR,DATEADD(
  2.      DAY,-1,GETDATE()) ,23) AS DATETIME)) yesterday1700 
Jan 8 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: dregier | last post by:
attached is my full query that I am currently working on: Declare @counterday1 int Declare @counterday2 int set @counterday1 = '20' set @counterday2 = '629' Declare @CounterMin07_01 float...
3
by: Fred | last post by:
I am trying to get al the rows from table1 where datetime is between 9:00AM yesterday and 9:00AM today if the time now is less than 9:00AM. Otherwise it should return all where datetime>9:00 AM...
2
by: PYCTAM | last post by:
Hi, I have data stored as in below sample : -------------------------------+---------------------------------+-------------- DateBegin | DateEnd | Rate...
3
by: elubin_nospam | last post by:
Given these two tables in DB/2: Date Activity ------------------- -------- 2005-01-02 00:00:00 1 2005-01-05 00:00:00 2 2005-01-05 00:00:01 1 2005-01-07 00:00:00 2...
9
by: MLH | last post by:
I have a table (tblCorrespondence) holding records with fields like , , , , , , , etc... About a dozen 's are defined and I often use queries to extract records of a given . That's pretty easy....
6
by: Maya | last post by:
Hello guys, I have table "Shopping" that has 3 columns (Item, Category, Price) Is there a straight forward way in T-SQL to select the Item that has the highest price for each category as below:...
5
by: Mahesh S | last post by:
Hi I would like to write a SQL select statement that would fetch rows numbered 50 to 100. Let me clarify, if i say "fetch first 10 rows only", it returns the first 10 rows from the resultset....
1
by: KingKong07 | last post by:
Hi I have a table with the columns Id, Date, Downtime, FromDate, ToDate How the data should look like in the table 33 2007-04-17 00:00:00.000 2 2007-04-17 15:55:52.000 2007-04-17...
3
by: jerry101 | last post by:
hi, i've got a form in which the user can fill out consisting of 2 text boxes to enter information in, a drop down box of choices and of course a submit button. when they fill it out, it sends the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.