Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-03-24
5
Medium Priority
?
321 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 2000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Recursive SQL is one of the most fascinating and powerful and yet dangerous feature offered in many modern databases today using a Common Table Expression (CTE) first introduced in the ANSI SQL 99 standard. The first implementations of CTE began ap…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

971 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