Solved

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

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
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.

831 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