Here is what you would use inline in your code.
SELECT [Date] = CAST (@DateValue AS datetime)
Main Topics
Browse All TopicsI have a csv file that is not formatted exactly the same every time. So... in order to get the csv file into sql, I made a table with all Text columns and then import the file. My issue is that I have several fields that are dates, and since they are in SQL as text and not as Dates I can not run a query that involves dates.
How can I convert these on the fly for comparison.
Here is some sample data and my sql query at the bottom...
Thanks
125635/ JL/JL 963499709701 0034.61 34.0 1 11/03/2009 11/02/2009 4576
125719 DA/DA 963499710267 0026.51 22.0 1 11/04/2009 11/03/2009 4632
125809 JL/JL 963499710820 0011.25 05.0 1 11/05/2009 11/04/2009 4688
125729 DA/DA 963499710278 0028.52 22.0 1 11/04/2009 11/03/2009 4633
125775/ JL/JL 963499710289 0018.72 10.0 1 11/04/2009 11/03/2009 4634
125742/ DA/DA 963499710290 0027.38 22.0 1 11/04/2009 11/03/2009 4635
124328/C/DA 963499710304 0010.69 14.0 1 11/06/2009 11/03/2009 4636
select
ref as 'Reference',
case when patindex('%[0-9][0-9][0-9]
then cast(substring(ref, patindex('%[0-9][0-9][0-9]
else 0 end as 'SalesOrder #',
case when patindex('%[0-9][0-9][0-9]
then cast(substring(tracking, patindex('%[0-9][0-9][0-9]
else cast(substring(totalcustom
shipmentdate
from fedex
where shipmentdate = '11/02/2009'
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Here is a link to a page of useful conversions as well.
http://www.sqlusa.com/best
Are you running this against just the data above or a larger set of data? If it's just the data above it should work. If you have other rows and some of the data in some of the "date" rows are not formatted the same then you will get the error "Explicit conversion from data type text to datetime is not allowed." In other words if one row had $1/2/2008 and you told it to convert to a date it wouldn't know what to do and error out.
Well I changed my columns to varchar(Max) and now my query works even if the data is not formatted properly.
select
ref as 'Reference',
case when patindex('%[0-9][0-9][0-9]
then cast(substring(ref, patindex('%[0-9][0-9][0-9]
else 0 end as 'SalesOrder #',
case when patindex('%[0-9][0-9][0-9]
then cast(substring(tracking, patindex('%[0-9][0-9][0-9]
else cast(substring(totalcustom
shipmentdate
from fedex
where shipmentdate = '11/02/2009'
Thank you for your assistance.
Jeff
Business Accounts
Answer for Membership
by: aneeshattingalPosted on 2009-11-05 at 10:47:20ID: 25752543
where CONVERT(datetime, shipmentdate,101) = '11/02/2009'