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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 PatIndex

Author  Topic 

thamatrix1436
Starting Member

3 Posts

Posted - 2011-03-29 : 13:52:15
Problem: Trying to pick out two digit day from a string in a Desc column and concatenating with a month column and a year column from another table.

Here is the scenario...

Table 1 (No day column in this table)
--------
Year(Decimal) | Month(Decimal)
2011 | 2

Table 2
--------
Desc(Varchar)
020511 Covers

So, I am trying to take the '05' from the Desc column in Table 2 and add with Month and Year column from Table 1.

Inital attempt (Didnt work...Got The conversion of a varchar data type to a datetime data type resulted in an out-of-range value)

Select case when (select PatIndex('%[0-9]%',left(Table2.DESC,6))) = 1 then cast(rtrim(Table1.Year*10000+Table1.Month*100+convert(nvarchar,right(left(Table2.DESC,4),2)))
as datetime) else 0 end

Thoughts?

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-03-29 : 14:46:35
When you want to pick data from two tables, you need something to relate the two tables to one another. If you have such a join column that you can use, then may be this is what you are looking for? I called the join colum as "JoinColumn" in my example below:
select 
cast (
cast(Table1.[Year] as varchar(4))
+ left('0'+cast(Table1.[Month] as varchar(2)),2)
+ case
when patindex('%[^0-9]%',left(ltrim(Table2.[Desc]),6)) = 0 and len(Table2.[Desc]) >= 6
then substring( ltrim(Table2.[Desc]), 3,2 )
end
as datetime)
from
Table1 inner join Table2
on Table1.JoinColumn = Table2.JoinColumn
Go to Top of Page

DonAtWork
Master Smack Fu Yak Hacker

2167 Posts

Posted - 2011-03-29 : 14:47:35
Does table 2 have the actual whole date you are looking for, but in varchar? Asking because that is what you example shows. If so, just convert the item in table 2 to a date (checking if it would be valid of course).

http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspx
How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page

thamatrix1436
Starting Member

3 Posts

Posted - 2011-03-29 : 15:08:46
quote:
Originally posted by sunitabeck

When you want to pick data from two tables, you need something to relate the two tables to one another. If you have such a join column that you can use, then may be this is what you are looking for? I called the join colum as "JoinColumn" in my example below:
select 
cast (
cast(Table1.[Year] as varchar(4))
+ left('0'+cast(Table1.[Month] as varchar(2)),2)
+ case
when patindex('%[^0-9]%',left(ltrim(Table2.[Desc]),6)) = 0 and len(Table2.[Desc]) >= 6
then substring( ltrim(Table2.[Desc]), 3,2 )
end
as datetime)
from
Table1 inner join Table2
on Table1.JoinColumn = Table2.JoinColumn




Have a join column. Just didnt include it in the scenario I listed. Applied your fix and got the following error this time...
Conversion failed when converting date and/or time from character string.
Go to Top of Page

thamatrix1436
Starting Member

3 Posts

Posted - 2011-03-29 : 15:13:15
quote:
Originally posted by DonAtWork

Does table 2 have the actual whole date you are looking for, but in varchar? Asking because that is what you example shows. If so, just convert the item in table 2 to a date (checking if it would be valid of course).

http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspx
How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp


The best answer is sometimes. Sometimes the actual whole date is contained in the Desc field sometimes its not. Here is an example...
Desc
------
020511 Covers
Covers to Covers
0111 Covers
Covers Time
Go to Top of Page

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2011-03-29 : 15:20:40
can you change your design that populates this table? what is the source of the data, a UI or are you getting this from third party? do you have control over the data?
why is the data as it is in the first place

If you don't have the passion to help people, you have no passion
Go to Top of Page
   

- Advertisement -