Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Parsing datetime out of varchar field for paramete

Author  Topic 

RalphWiggum
Starting Member

13 Posts

Posted - 2012-03-29 : 08:34:45
Hi all. I have a strange one.
First of all, I have a stored procedure that works fine with the exception of needing to allow datetime parameters to look at a varchar field. I feel like this should be possible but I can't get it to work.

1st. The field is VARCHAR(40), with the last 26 characters containing a datetime format of 2012-01-01-18.49.15.032000. The stupid thing about this field is that it also contains a username, so the entire field will actually contain data like USERNAME 2012-01-01-18.49.15.032000

I need the stored procedure's datetime parameters to look at the datetime portion of this varchar field. I have concluded that SQL Server hates the - between the date and time, or between the 01 and 18 in this example. It also seems to hate the .xxxxxx milisecond format, and prefers the .xxx

So, in order to attempt to parse it out, I have the following where clause in my SP to attempt to look at the last 26 characters of this goofy field, and I've also attempted to parse the datetime portion of this field into something SQL Server likes. Here is what I have:

WHERE DATEADD(dd, 0, DATEDIFF(dd, 0, CAST(LEFT(RIGHT(RTRIM(W.USER3), 26),10) + ' ' + replace(LEFT(RIGHT(RTRIM(W.USER3), 15),9), '.', ':') + LEFT(RIGHT(RTRIM(W.USER3), 6),3)AS DATETIME))) BETWEEN @StartDate AND @EndDate

This looks and parses okay in SSMS, however, when I exeute the SP, I get "Conversion failed when converting datetime from character string".

Any ideas would be greatly and awesomely appreciated.

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2012-03-29 : 09:29:35
This only works if the date format is consist and the last 26 characters are always the date

declare @str varchar(40) ='USERNAME 2012-01-01-18.49.15.032000'
select CONVERT(Datetime,SUBSTRING(@str,LEN(@str)-26,11) +' '+ REPLACE(SUBSTRING(@str,LEN(@str)-14,8) ,'.',':'))

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-03-29 : 09:45:27
DECLARE @str varchar(40) ='USERNAME 2012-01-01-18.49.15.032000'

SELECT RIGHT(@str,26)

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-03-29 : 12:17:04
quote:
Originally posted by RalphWiggum

Hi all. I have a strange one.
First of all, I have a stored procedure that works fine with the exception of needing to allow datetime parameters to look at a varchar field. I feel like this should be possible but I can't get it to work.

1st. The field is VARCHAR(40), with the last 26 characters containing a datetime format of 2012-01-01-18.49.15.032000. The stupid thing about this field is that it also contains a username, so the entire field will actually contain data like USERNAME 2012-01-01-18.49.15.032000

I need the stored procedure's datetime parameters to look at the datetime portion of this varchar field. I have concluded that SQL Server hates the - between the date and time, or between the 01 and 18 in this example. It also seems to hate the .xxxxxx milisecond format, and prefers the .xxx

So, in order to attempt to parse it out, I have the following where clause in my SP to attempt to look at the last 26 characters of this goofy field, and I've also attempted to parse the datetime portion of this field into something SQL Server likes. Here is what I have:

WHERE DATEADD(dd, 0, DATEDIFF(dd, 0, CAST(LEFT(RIGHT(RTRIM(W.USER3), 26),10) + ' ' + replace(LEFT(RIGHT(RTRIM(W.USER3), 15),9), '.', ':') + LEFT(RIGHT(RTRIM(W.USER3), 6),3)AS DATETIME))) BETWEEN @StartDate AND @EndDate

This looks and parses okay in SSMS, however, when I exeute the SP, I get "Conversion failed when converting datetime from character string".

Any ideas would be greatly and awesomely appreciated.


thats the problem of mixing datatypes. why not maintain date values independently in a datetime field?
Otherwise you need to put some parsing logic like suggestions given and also make sure date values are in consistent unambiguos format
make sure you read this

http://visakhm.blogspot.com/2011/12/why-iso-format-is-recommended-while.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-03-29 : 12:28:32
ppffftttt...why not state the obvious visakh ;-)

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

RalphWiggum
Starting Member

13 Posts

Posted - 2012-03-29 : 13:55:51
quote:
Originally posted by visakh16

quote:
Originally posted by RalphWiggum

Hi all. I have a strange one.
First of all, I have a stored procedure that works fine with the exception of needing to allow datetime parameters to look at a varchar field. I feel like this should be possible but I can't get it to work.

1st. The field is VARCHAR(40), with the last 26 characters containing a datetime format of 2012-01-01-18.49.15.032000. The stupid thing about this field is that it also contains a username, so the entire field will actually contain data like USERNAME 2012-01-01-18.49.15.032000

I need the stored procedure's datetime parameters to look at the datetime portion of this varchar field. I have concluded that SQL Server hates the - between the date and time, or between the 01 and 18 in this example. It also seems to hate the .xxxxxx milisecond format, and prefers the .xxx

So, in order to attempt to parse it out, I have the following where clause in my SP to attempt to look at the last 26 characters of this goofy field, and I've also attempted to parse the datetime portion of this field into something SQL Server likes. Here is what I have:

WHERE DATEADD(dd, 0, DATEDIFF(dd, 0, CAST(LEFT(RIGHT(RTRIM(W.USER3), 26),10) + ' ' + replace(LEFT(RIGHT(RTRIM(W.USER3), 15),9), '.', ':') + LEFT(RIGHT(RTRIM(W.USER3), 6),3)AS DATETIME))) BETWEEN @StartDate AND @EndDate

This looks and parses okay in SSMS, however, when I exeute the SP, I get "Conversion failed when converting datetime from character string".

Any ideas would be greatly and awesomely appreciated.


thats the problem of mixing datatypes. why not maintain date values independently in a datetime field?
Otherwise you need to put some parsing logic like suggestions given and also make sure date values are in consistent unambiguos format
make sure you read this

http://visakhm.blogspot.com/2011/12/why-iso-format-is-recommended-while.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/




visakhm: Yeah, had I designed the database backend to the application we purchased, I wouldn't have done it this way...obviously. You don't know how many times I've asked myself this question when I come across datatypes that don't fit the situation when we purchase software from vendors.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-03-29 : 14:09:38
That's because I believe ALL 3rd party vendor apps are CRAP

They hire cheap overseas labor and don't care how the backend is built..as long as it does (and as far as it's limited to do) what its touts

DON'T Look for ANY Customization

I once had to tell a 3rd party Vendor Why their code was taking an hour...put a trace on and found that they had several nested cursors...I re-wrote it and it flew...my boss never gave them my solution...he just made THEM fix it



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page
   

- Advertisement -