Solved

Date Conversion in T-SQL -Immediate help needed!!!

Posted on 2001-08-29
4
1,146 Views
Last Modified: 2010-05-18
This should be an easy question - but I need immediate help...

Here is the logic behind piece of SQL I am trying to run in SQL7

SELECT *
FROM   ExportLog
WHERE  TRUNC(ExportTimeStamp) = '01/01/2001'

Well TRUNC is a command in ORACLE that strips the time part of the argument.  Of course there is no TRUNC command in SQL7.  How do people do this in T-SQL??

is there some kind of TO_DATE or FORMAT function I can use on the date?  i.e:

SELECT *
FROM ExportLog
WHERE Format(ExportTimeStamp, 'yyyymmdd') = '20010101'

Thanks for your help
0
Comment
Question by:jeffstatt
  • 2
4 Comments
 
LVL 18

Expert Comment

by:nigelrivett
ID: 6436155
where convert(datetime,convert(varchar(13),ExportTimeStamp,113)) = '1 jan 2001'

0
 
LVL 18

Expert Comment

by:nigelrivett
ID: 6436170
The above does the comparison in a date datatype.
If you wanted to use '20010101' as character then you could

replace(convert(varchar(10),ExportTimeStamp,121),'-','') = '20010101'
0
 
LVL 3

Accepted Solution

by:
ibro earned 300 total points
ID: 6436174
SELECT *
FROM ExportLog
WHERE convert(varchar,ExportTimeStamp,101) = '01/01/2001'

101 means date is in format 'mm/dd/yyyy'

see convert help in BOL for more date formats
0
 

Author Comment

by:jeffstatt
ID: 6436195
That's the one I needed! Thanks
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

828 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