July 12, 2009 at 5:02 am
Hi all,
I'm having a problem to convert a varchar to timestamp. Someone in our business wants to use an export and when all data is changed in the export the business wants to import it back into the database, but only the records that aren't changed yet.
So I'm having the issue that I get an string back from the import and placed into a stored procedure, but I can't convert a varchar(255) '
Anyone have an idea how to solve this issue?
July 12, 2009 at 5:04 am
can you pls post an example of the format of source data that u want to be converted to datetime?
July 12, 2009 at 5:16 am
It's not a datetime format, but a timestamp format:
this is an example how it works when I get the data for my sp.
exec sp_example '9929129', '0x0000000001E5CFE7'
where the hex is my timestamp, it's also known as rowversion.
example data in stored procedure
ALTER PROCEDURE [dbo].[sp_example]
(
@Schip_Id varchar(10),
@TimeStamp varchar(20)
)
AS
BEGIN
-- the declare statement is used for unusing of quotes from the @TimeStamp...
DECLARE @convertedTimeStamp varchar(20)
SET @convertedTimeStamp = @timestamp
print @convertedTimeStamp
select * from
WHERE schip_id = Schip_Id
and ([timestamp] = Convert(varbinary(8), @convertedTimeStamp))
END
July 12, 2009 at 5:40 am
I changed the datatype of @TimeStamp parameter of the sp to TimeStamp and created some test data and i could select exact values
-- create a test table
create table #t
(
col1 int,
timestamp timestamp
)
insert into #t(col1) values(1)
go 5
select * from #t
OUTPUT
----------
col1 timestamp
----------- ------------------
1 0x00000000000007D8
1 0x00000000000007D9
1 0x00000000000007DA
1 0x00000000000007DB
1 0x00000000000007DC
1 0x00000000000007DD
1 0x00000000000007DE
1 0x00000000000007DF
1 0x00000000000007E0
1 0x00000000000007E1
-- create the proc
alter PROCEDURE [dbo].[sp_example]
(
@Schip_Id varchar(10),
@TimeStamp timestamp
)
AS
BEGIN
-- the declare statement is used for unusing of quotes from the @TimeStamp...
DECLARE @convertedTimeStamp varchar(20)
SET @convertedTimeStamp = @timestamp
print @convertedTimeStamp
select * from #t
WHERE col1 = @Schip_Id
and [timestamp] = @TimeStamp
--Convert(varbinary(8), @convertedTimeStamp))
END
-- run the sp
exec sp_example 1,0x00000000000007DC
OUTPUT-
---------
col1 timestamp
----------- ------------------
1 0x00000000000007DC
(1 row(s) affected)
Is this what you need?
July 12, 2009 at 8:09 am
The issue here is that I get an parameter filled with quotes. This isn't accepted by SQL Server when executing the stored procedure. So I can't validate on the timestamp in the where clause.
July 13, 2009 at 1:28 am
Just to verify, do you have quotes in your timestamp? .. what data type in timestamp
July 13, 2009 at 3:28 am
mysticslayer (7/12/2009)
The issue here is that I get an parameter filled with quotes. This isn't accepted by SQL Server when executing the stored procedure. So I can't validate on the timestamp in the where clause.
I dont think it's possible to convert a varchar to timestamp..
may be you can modify the application that exports data to exclude the quotes in this field.
July 13, 2009 at 3:55 am
Try making the parameter a varbinary instead of a varchar.
ALTER PROCEDURE [dbo].[sp_example]
(
@Schip_Id varchar(10),
@TimeStamp varbinary(8)
)
AS
BEGIN
-- the declare statement is used for unusing of quotes from the @TimeStamp...
print @convertedTimeStamp
select * from
WHERE schip_id = Schip_Id
and ([timestamp] = @convertedTimeStamp)
END
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
July 13, 2009 at 3:58 am
mysticslayer (7/12/2009)
The issue here is that I get an parameter filled with quotes.
Not quite sure what you mean here. Could you give more detail?
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
July 13, 2009 at 4:08 am
GilaMonster (7/13/2009)
mysticslayer (7/12/2009)
The issue here is that I get an parameter filled with quotes.Not quite sure what you mean here. Could you give more detail?
Perhaps he's getting data as '0x00000000000007D6' as a string instead of plain 0x00000000000007D6.
July 13, 2009 at 4:12 am
ps (7/13/2009)
GilaMonster (7/13/2009)
mysticslayer (7/12/2009)
The issue here is that I get an parameter filled with quotes.Not quite sure what you mean here. Could you give more detail?
Perhaps he's getting data as '0x00000000000007D6' as a string instead of plain 0x00000000000007D6.
That's something that has to be fixed in whatever is calling the proc.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
July 13, 2009 at 8:44 am
Well, because SSIS sees it as a varchar.
But I found a solution for me as well for SQL2k0, SQL2k5 and SQL2k8.
So this topic can be closed 🙂
July 13, 2009 at 9:02 am
It'd be great for everyone if you share the solution here 🙂
July 19, 2009 at 9:33 pm
mysticslayer (7/13/2009)
Well, because SSIS sees it as a varchar.But I found a solution for me as well for SQL2k0, SQL2k5 and SQL2k8.
So this topic can be closed 🙂
Two way street here... 😉 What IS the solution? Don't get labeled as a "checkvalve" on only your 4th visit. :hehe:
--Jeff Moden
Change is inevitable... Change for the better is not.
July 19, 2009 at 9:36 pm
Heh... seriously...
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 15 posts - 1 through 15 (of 15 total)
You must be logged in to reply to this topic. Login to reply