Hi,
I have a log table where a date time is stored in an ntext field.
I need to take this date time and use it to search for other records logged at that date time.
I've tried using a subquery and comparing the datetime to the ntext but this obvioiusly wouldn't work. Converting the ntext to a datetime, nvarchar or varchar isn't working for me either. I get over flow and syntax errors.
example code:
SELECT * FROM tbl_log
WHERE date_time IN
(SELECT data FROM tbl_log)
This is probably a very simple and naive way of attempting it but my SQL knowledge is limited.
date_time (datetime) has the format 2008-04-01 08:13:49.000
and data (ntext) has the format 3/31/2008 2:34:44 PM
if I explicitly use the date as a string e.g. date_time IN ('3/31/2008 2:34:44 PM') the format is still comparable and it works but I need it as a query.
Thanks.
6 7305
past this in your query analyzer - declare @date_time smalldatetime
-
declare @tbldata table (rownum int, data ntext)
-
-
select @date_time = '2008-04-01 08:13:49.000'
-
-
insert into @tbldata (rownum,data) values (1,'3/31/2008 2:34:44')
-
insert into @tbldata (rownum,data) values (2,'4/01/2008 2:34:44')
-
-
select * from @tbldata
-
-
select * from @tbldata where datediff(dd,cast(cast(data as varchar(30)) as smalldatetime),@date_time) = 0
-- CK
Thanks CK.
I tried this and it worked. I also added
AND datediff(ss,cast(cast(data as varchar(30)) as datetime),@date_time)=0 to compare the times too and it is working.
My next problem is how I apply this to my table and compare a list of dates. Each datetime we cast from data needs to be compared against each datetime in the table to see if there is a matching record. This is a log table and has columns:
DATE_TIME TYPE NAME DATA
2008-04-01 11:29:10.000 Print TIME_DATE 4/1/2008 9:33:35 AM
2008-04-01 14:09:13.000 Print TIME_DATE 4/1/2008 2:08:35 PM
.
.
.
2008-04-01 09:33:35.000 PrintInfo Description print sheet 1
2008-04-01 09:33:35.000 PrintInfo Quantity 4
2008-04-01 14:08:35.000 PrintInfo Description print letter
2008-04-01 14:08:35.000 PrintInfo Printer printerA
Some transactions record dates in data and these dates will have corresponding log entries and this is what I need to obtain. I hope this example shows what I'm trying to do and you might be able to help or steer me in the right direction.
Sorry I'm a little slow today :)
Are you saying you have two tables? Could you post some sample data of your source table and the output you're trying to get?
-- CK
I do have two tables but the first is easy so I haven't included it. I just get a date from it then use that date to search the second table, tbl_log, see my last post for the example. So say table1 gives me the date 2008-04-01 11:29:10.000
then I would use this in the second table tbl_log which would give me the data (ntext field) 4/1/2008 9:33:35 AM which I would use to query this same table again but against datetime i.e. data = datetime and (see further down the table) this should return the two records:
2008-04-01 09:33:35.000 PrintInfo Description print sheet 1
2008-04-01 09:33:35.000 PrintInfo Quantity 4
which is the information I need. I hope this is clearer. Thanks again.
I do have two tables but the first is easy so I haven't included it. I just get a date from it then use that date to search the second table, tbl_log, see my last post for the example. So say table1 gives me the date 2008-04-01 11:29:10.000
then I would use this in the second table tbl_log which would give me the data (ntext field) 4/1/2008 9:33:35 AM which I would use to query this same table again but against datetime i.e. data = datetime and (see further down the table) this should return the two records:
2008-04-01 09:33:35.000 PrintInfo Description print sheet 1
2008-04-01 09:33:35.000 PrintInfo Quantity 4
which is the information I need. I hope this is clearer. Thanks again.
OK. Am still slow :)
Here goes... -
select table1.field1, table1.field2, logtable.field1, logtable.field2
-
from table1
-
left join logtable on datediff(dd,table1.yourdatefield,cast(cast(dateonlogtable as varchar(30)) as datetime)) = 0
-- CK
Hi,
That wouldn't work for me since I need to query the log table twice. First to get the date from the data field using the table1 datetime and then again with the date from the data field. So I've decided on a much a simpler approach that provides me with the output I need.
1. get the data from logtable using table1 and cast this as a date and save it in a temporary table.
2. Use the temporary table to query the logtable. -
-
SELECT cast(cast(data as varchar) as datetime) as date
-
into #tempTable FROM logtable where date_time IN (
-
select date_time from table1)
-
-
select logtable.field1, logtable.field2 from logtable, #tempTable
-
where #tempTable.date = logtable.date_time
-
-
Thanks for your help.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Cally |
last post by:
Hello,
I would like to convert a field from ntext field found in one database
table to float field found in another database table. The reason why I
want to do this is a long one.
I have...
|
by: Tom |
last post by:
It appears that you can't compare two dates in DotNet. You must use
ToString and compare the strings. Is that the only reliable way?
Try this:
Dim dteOne As Date =...
|
by: conckrish |
last post by:
Hi all..
Can anyone tell me how to compare datetime objects?I ve three
objects namely Current date,start date and end date.. I need to check
the current date with Start date and end date....Plz...
|
by: Mark |
last post by:
I'd like to compare two datetime values in milliseconds. The
datetime.compare method appears to show only seconds. Milliseconds of a
datetime are available as a property of each datetime, but I...
|
by: =?Utf-8?B?ZGF2aWQ=?= |
last post by:
I try to follow Steve's paper to build a database, and store a small text
file into SQL Server database and retrieve it later. Only difference between
my table and Steve's table is that I use NTEXT...
|
by: xkenneth |
last post by:
Looking to do something similair. I'm working with alot of timestamps
and if they're within a couple seconds I need them to be indexed and
removed from a list.
Is there any possible way to index...
|
by: ChrisB |
last post by:
Hello,
I was wondering what the easiest way is to compare two DateTime objects and
not have the time components be included in the comparision.
For, example, if time1 = 10/01/07 9:00 am, and...
|
by: =?Utf-8?B?U2FuZHBvaW50R3V5?= |
last post by:
string dateTime = "5/28/2008";
DateTime.Compare(DateTime.Now,DateTime.Now) // returns 0
DateTime.Compare(DateTime.Now,DateTime.Parse(dateTime)) // returns 1
I need to compare 2 dates, using...
|
by: Brian |
last post by:
I have a datetimepicker formated for just time, the user selects the time.
I want to compare if that time is between midnight and 8 am
dtmTime #11:59:59 PM# and dtmTime < #08:00:00 AM#
this...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |