473,555 Members | 2,333 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Select Between 17:00 Yesterday and 17:00 Today

code green
1,726 Recognized Expert Top Contributor
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 7377
ck9663
2,878 Recognized Expert Specialist
Here, read some of this...

Happy Coding!!!

~ CK
Dec 31 '09 #2
code green
1,726 Recognized Expert Top Contributor
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
reachenbee
1 New Member
Please try this.

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

-bala
Jan 4 '10 #4
code green
1,726 Recognized Expert Top Contributor
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 Recognized Expert Top Contributor
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
2524
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 Declare @CounterMin08_01 float Declare @CounterMin09_01 float
3
9368
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 today. Is this possible as a query in sql2000?
2
1265
by: PYCTAM | last post by:
Hi, I have data stored as in below sample : -------------------------------+---------------------------------+-------------- DateBegin | DateEnd | Rate -------------------------------+---------------------------------+-------------- 2005-11-13 00:00:00 2005-11-14 00:00:00 63.0000 2005-11-14...
3
1449
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 2005-01-17 00:00:00 2 2005-02-02 00:00:00 1 2005-02-03 00:00:00 1
9
2847
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. But, recently, I wanted to list all "15" letters, a few of the above mentioned fields and one additional field: of nother letter "00" in the...
6
1530
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: Item Category Price -------- ----------- ---------- 1 2 ...
5
22745
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. how do I get a specific subset of rows from the result set if I give a start and end value. For example, retreiving rows 50 to 100 from the...
1
1244
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 15:58:17.000 34 2007-04-17 00:00:00.000 68 2007-04-17 15:58:17.000 2007-04-17 17:06:27.000 35 2007-04-17 00:00:00.000 1 2007-04-17...
3
1759
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 email of information to me fine. everything comes through apart from the option they've chosen from the drop down box. the code i have is the...
0
7547
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7825
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8060
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7588
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7904
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
3596
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2037
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1159
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
865
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.