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
 strange date in DB that I need to filter with

Author  Topic 

jdgower
Starting Member

4 Posts

Posted - 2011-06-08 : 12:00:54
I am attempting to filter records by date ('EXP' below)...

I am to the point where it is filtering them exactly how I want with 2 exceptions:

a. it's looking at an arbitrary date (10/09/99), I want it to look at today's date.

b. to make matters worse the database I have to use has a very strange format for dates where it's in the format of MM/DD/YY except that anything that is in the 2000's uses letters so that:

2005-03-24 would be 03/24/A5
2011-01-15 would be 01/15/B1

and i'm guessing:
2020-06-28 would be 06/28/C0

Any help at all would be greatly appreciated as I'm about to turn a gun on myself :) j/k.

Thanks in advance,
Jeremy

    <?php
$conn=odbc_connect('tam','','');
if (!$conn)
{exit("Connection Failed: " . $conn);}
$sql_first="SELECT DISTINCT(REC) FROM POLICY p INNER JOIN INS i ON i.REC=LEFT(p.POL_IDX, 7) AND i.KEY=p.PC WHERE p.TYPE IN ('PROF', 'PROO') AND p.EXP>'10/09/99' GROUP BY i.REC";
$rs_first=odbc_exec($conn,$sql_first);
while (odbc_fetch_row($rs_first)) { ?>
<tr>
<?php
$rec=odbc_result($rs_first,"REC");
echo "<td><a href=\"update.php?code={$rec}\">{$rec}</a></td>";
?>
</tr>
<?php }
odbc_close($conn); ?>

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2011-06-08 : 12:39:30
Something like this...
DECLARE	@dt char(8)
SET @dt = '03/24/A5'

select Convert(smalldatetime,
LEFT(@dt, 6) +
Convert(Char(4),
CASE SUBSTRING(@dt, 7, 1)
WHEN 'A' THEN 2000
WHEN 'B' THEN 2010
WHEN 'C' THEN 2020
WHEN 'D' THEN 2030
WHEN 'E' THEN 2040
WHEN 'F' THEN 2050
WHEN 'G' THEN 2060
WHEN 'H' THEN 2070
WHEN 'I' THEN 2080
END + Convert(smallint, RIGHT(@dt, 1))
)
)
Go to Top of Page

jdgower
Starting Member

4 Posts

Posted - 2011-06-08 : 13:52:54
First of all, thanks very much for responding!!! Secondly, I'm really a newbie to a lot of these things (sql, php, etc.)...up until now it's really been 'tinkering'. The below is the statement I have laid out which throws an error, I'm sure it's horribly bad, but I've done my best...


$sql_first="SELECT DISTINCT(REC) FROM POLICY p INNER JOIN INS i ON i.REC=LEFT(p.POL_IDX, 7) AND i.KEY=p.PC WHERE p.TYPE IN ('PROF', 'PROO') AND Convert(smalldatetime,
LEFT(p.EXP, 6) +
Convert(Char(4),
CASE SUBSTRING(p.EXP, 7, 1)
WHEN 'A' THEN 2000
WHEN 'B' THEN 2010
WHEN 'C' THEN 2020
WHEN 'D' THEN 2030
WHEN 'E' THEN 2040
WHEN 'F' THEN 2050
WHEN 'G' THEN 2060
WHEN 'H' THEN 2070
WHEN 'I' THEN 2080
END + Convert(smallint, RIGHT(p.EXP, 1))
)
)
>=GETDATE() GROUP BY i.REC";


throws:
Warning: odbc_exec() [function.odbc-exec]: SQL error: [Microsoft][ODBC dBase Driver] Syntax error (missing operator) in query expression 'p.TYPE IN ('PROF', 'PROO') AND Convert(smalldatetime, LEFT(p.EXP, 6) + Convert(Char(4), CASE SUBSTRING(p.EXP, 7, 1) WHEN 'A' THEN 2000 WHEN 'B' THEN 2010 WHEN 'C' THEN 2020 WHEN 'D' THEN 2030 WHEN 'E' THEN 2040 WHEN ''., SQL state 37000 in SQLExecDirect in C:\wamp\www\prefill\index.php on line 61

Warning: odbc_fetch_row() expects parameter 1 to be resource, boolean given in C:\wamp\www\prefill\index.php on line 62

----

If you could find it in your heart to help me further, it would be greatly appreciated. Either way, thank you very much for your time! :)
Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2011-06-08 : 14:25:17
change i.KEY to i.[KEY]
Go to Top of Page

jdgower
Starting Member

4 Posts

Posted - 2011-06-08 : 15:08:00
hmmm, didnt do anything. I also tried all variations of the brackets around all the columns to no avail. Holding my breath and praying you dont give up on me. :)
Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2011-06-08 : 22:17:21
before executing it, can you echo $sql_first; and post the output please?

Also paste it into your SSMS and try executing it.
Go to Top of Page

jdgower
Starting Member

4 Posts

Posted - 2011-06-08 : 23:47:35
echo sql_first results in:

SELECT DISTINCT(REC) FROM POLICY p INNER JOIN INS i ON i.REC=LEFT(p.POL_IDX, 7) AND i.KEY=p.PC WHERE p.TYPE IN ('PROF', 'PROO') AND Convert(smalldatetime, LEFT(p.EXP, 6) + Convert(Char(4), CASE SUBSTRING(p.EXP, 7, 1) WHEN 'A' THEN 2000 WHEN 'B' THEN 2010 WHEN 'C' THEN 2020 WHEN 'D' THEN 2030 WHEN 'E' THEN 2040 WHEN 'F' THEN 2050 WHEN 'G' THEN 2060 WHEN 'H' THEN 2070 WHEN 'I' THEN 2080 END + Convert(smallint, RIGHT(p.EXP, 1)) ) ) >=GETDATE() GROUP BY i.REC

Dont have SSMS but I will try it on my work's server tomorrow if I can and will post that. Thanks again.
Go to Top of Page
   

- Advertisement -