Solved

Sql 2008 server query select all recorders created today varchar date field.

Posted on 2011-03-24
5
295 Views
Last Modified: 2012-05-11
I am trying to retreive all the records from a table comparing against todays date and a varchar that holds the date. The problem is my query never returns any rows. Below a test I ran to determine what is being returned

My query:
SELECT CONVERT(char(8), j.StartTimeStamp, 112) as dt FROM  Jobs AS j INNER JOIN  JobDetails AS jd ON j.OrderId = jd.OrderID where CONVERT(char(8), j.StartTimeStamp, 112) = CONVERT(char(8), '20112403', 112) ;

This:
SELECT CONVERT(char(8), j.StartTimeStamp, 112) as dt
FROM  Jobs AS j
Returns:
3/24/2011

THIS:
SELECT CONVERT(char(8), CURRENT_TIMESTAMP, 112) as dt
Returns:
20110324

Which of course will return false on a compare
Whats the best way to handle this.



0
Comment
Question by:JonMny
  • 3
  • 2
5 Comments
 
LVL 4

Expert Comment

by:jimtpowers
ID: 35209427
Try:

SELECT CONVERT(date, CURRENT_TIMESTAMP, 112) as dt 

Open in new window

0
 
LVL 4

Expert Comment

by:jimtpowers
ID: 35209448
Your query can be:

SELECT CONVERT(DATE, j.StartTimeStamp, 112) as dt
FROM   Jobs AS j
       INNER JOIN  JobDetails AS jd ON j.OrderId = jd.OrderID
WHERE CONVERT(DATE, j.StartTimeStamp, 112) = '2011-03-24';

Open in new window

0
 
LVL 9

Author Comment

by:JonMny
ID: 35209554
This fails with an error: Conversion failed when converting datetime from character string.

SELECT CONVERT(DATE, j.StartTimeStamp, 112) as dt
FROM   Jobs AS j
       INNER JOIN  JobDetails AS jd ON j.OrderId = jd.OrderID
WHERE CONVERT(DATE, j.StartTimeStamp, 112) = '2011-03-24';

0
 
LVL 9

Author Comment

by:JonMny
ID: 35209629

If I try this
SELECT CONVERT(char(8), j.StartTimeStamp, 112) as dt ,CONVERT(char(8), current_timestamp, 112) as dtt FROM  Jobs as j

I get this
3/8/2011      20110324
3/8/2011      20110324
3/8/2011      20110324
3/8/2011      20110324
3/8/2011      20110324
3/8/2011      20110324
3/23/201      20110324
3/24/201      20110324

I'm requesting the same format what am I missing?
0
 
LVL 4

Accepted Solution

by:
jimtpowers earned 500 total points
ID: 35210148
Try this:

SELECT CONVERT(DATE, j.StartTimeStamp, 112) as dt  
FROM   Jobs AS j  
       INNER JOIN  JobDetails AS jd ON j.OrderId = jd.OrderID  
WHERE CONVERT(DATE, j.StartTimeStamp, 112) = CONVERT(DATE, CURRENT_TIMESTAMP);

Open in new window


I'm not sure why you are getting different results asking for the same format. I have had troubles with the formatting as well.
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to search for strings inside db views 4 35
Complex Query - help please 5 52
Re-appearing SQL Server Agent jobs 7 28
RAISERROR WITH NOWAIT 2 14
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

792 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