Solved

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

Posted on 2011-03-24
5
268 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 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

12 Experts available now in Live!

Get 1:1 Help Now