Author |
Topic |
fmardani
Constraint Violating Yak Guru
433 Posts |
Posted - 2007-06-29 : 05:31:01
|
There is a field called field1 in a table called table1Field1 is varchar(50)Data inside field1 is such as:3Tapr 2007feb 2001jan 2001dec 1999...I am writing a sql query to return the data so that the field1 is sorted:ie:3Tdec 1999jan 2001feb 2001apr 2007...This is what I have in the order by clause:cast('01-' + replace(field1, ' ', '-') as datetime)The problem is if there is something else such as '3T' or another text inside this field1.Should there be a case statement inside the oprder by clause?Please note that if there is any text which does not seem to be a date then it should appear first in the list as shown in the example above.Thanks |
|
Koji Matsumura
Posting Yak Master
141 Posts |
Posted - 2007-06-29 : 05:48:00
|
Yes, you need a case statement. The statement depends on what kind of string there can be. |
 |
|
fmardani
Constraint Violating Yak Guru
433 Posts |
Posted - 2007-06-29 : 05:50:27
|
I don't want to hard code the strings. |
 |
|
pbguy
Constraint Violating Yak Guru
319 Posts |
Posted - 2007-06-29 : 06:39:42
|
declare @t table(col1 varchar(50))insert @tselect 'aez' union allselect '3T' union allselect 'zzz' union allselect 'dec 1999' union allselect 'jan 2007' union allselect 'feb 2001' union allselect 'apr 2005'Select * from @t order by case when isdate('01-' + replace(col1, ' ', '-')) = 1 then cast('01-' + replace(col1, ' ', '-') as datetime) Else cast('01/01/1900' as datetime) End--------------------------------------------------S.Ahamed |
 |
|
Koji Matsumura
Posting Yak Master
141 Posts |
Posted - 2007-06-29 : 06:41:21
|
You don't have to.For example is they are all shorter than 8 charactersORDER BYCASE WHEN LEN(field1) < 8 THEN 1 ELSE 2 END,CASE WHEN LEN(field1) < 8 THEN field1 ELSE cast('01-' + replace(field1, ' ', '-') as datetime) END |
 |
|
Koji Matsumura
Posting Yak Master
141 Posts |
Posted - 2007-06-29 : 06:45:26
|
Ah, that is a nice and clear solution. |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-06-29 : 09:09:54
|
quote: Originally posted by pbguy declare @t table(col1 varchar(50))insert @tselect 'aez' union allselect '3T' union allselect 'zzz' union allselect 'dec 1999' union allselect 'jan 2007' union allselect 'feb 2001' union allselect 'apr 2005'Select * from @t order by case when isdate('01-' + replace(col1, ' ', '-')) = 1 then cast('01-' + replace(col1, ' ', '-') as datetime) Else cast('01/01/1900' as datetime) End--------------------------------------------------S.Ahamed
Note that like ISNUMERIC, ISDATE also not reliableSelect ISDATE('2006'),ISDATE(2006)MadhivananFailing to plan is Planning to fail |
 |
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2007-06-29 : 09:52:22
|
You have 429 posts here and you still have not learned to use correct datatypes!?? ALWAYS use proper datatypes for your data and this will NEVER be an issue. Why do people insist of making things harder on themselves ?????- Jeffhttp://weblogs.sqlteam.com/JeffS |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-06-29 : 23:30:08
|
quote: Originally posted by jsmith8858 You have 429 posts here and you still have not learned to use correct datatypes!?? ALWAYS use proper datatypes for your data and this will NEVER be an issue. Why do people insist of making things harder on themselves ?????- Jeffhttp://weblogs.sqlteam.com/JeffS
Becuase they want to write VERY complicated queries MadhivananFailing to plan is Planning to fail |
 |
|
|