Link to home
Start Free TrialLog in
Avatar of Destiny Amana
Destiny AmanaFlag for Nigeria

asked on

Upsizing from Access to MSSQL, Problem with SQL code and Functions not found in SQL

this statement works well in ACCESS


  TbillSQL = "SELECT tbl_research_tbills.autoid, CDate(Format(tbl_research_tbills.tbilldate,'dd/mm/yy')) AS NEWDATE, tbl_research_tbills.mrr, tbl_research_tbills.subscription, "&_
                                                      "tbl_research_tbills.amountonoffer, tbl_research_tbills.allotment, tbl_research_tbills.stoprate, tbl_research_tbills.trueyield "&_
                                                      "FROM tbl_research_tbills "&_
                                                      "WHERE CDate(Format(tbl_research_tbills.tbilldate,'dd/mm/yy')) Between #"& startdate &"# And #"& enddate  &"# "&_
                                                      "ORDER BY CDate(Format(tbl_research_tbills.tbilldate,'dd/mm/yy'))"


But once I change the databse to MSSQL, I get this error message

[Microsoft][ODBC SQL Server Driver][SQL Server]'Format' is not a recognized function name.


HELP!!!
Avatar of Arthur_Wood
Arthur_Wood
Flag of United States of America image

why are you going through these wild formatting and then conversion to a Data value, when it appears the the field in

tbl_research_tbills  

is already a DateTime field to begin with.  YOu can simply compare one datetime field to another, or to a range of date value, using Between.  There is no need to Format the DateTime fioeld, and then use the cDate function to convert it back into a DateTime value.  The does absolutely nothing.

AW
Avatar of Destiny Amana

ASKER

The Date is in the format 04-Jan-96 will this be recognised by MSSQL as a Date?
yes, and why is the date in a TEXT field?  If that is the format of the DateTime field, then no big deal.  The Date format as set in the table is PURELY for display purposes only.  DateTime values are actually held in the tables without ANY formatting whatsoever.  In fact, Access TDateTime values are stored as Numbers with a decimal point.

The value of the number to the LEFT of the decimal point is the count of the number of days since the base date of 30 DEC 1899.  The value of the number to the RIGHT of the decimal point is the time, measured in seconds since Midnight, as a fraction of 1 day (1 day = 86400 seconds) so that Noon would have the fractional value of 0.5.  The Format property of a DateTime field simply controls how the Date and Time are displyed, and has nothing at all to do with out the value is actually held in the table.  As I said, all of your cDate and Format gyrations were a pure waste of time and totally un-necessary.. SQL Server will be able to handle your DateTime fields just fine.

AW
yes,  ! GREAT!

and why is the date in a TEXT field

-- data is uploaded via a text file which is read into the database from excel.

Thanks.

i will take out the extra superfluous code and test then return to give you full credit!
well, the pages are now displaying but the query is not doing a date filter that works.

The dates are not being recognised correctly.

What next?
Avatar of Aneesh
there is no format function in SQL server,
you can use CONVERT for this, please refer BOL for convert
I do not understand.

How do I apply the convert function to work the same way the original SQL statement worked with Access whenthe date format is 01-apr-05

Thanks
declare @TbillSQL varchar(1000),@startdate varchar(10), @enddate varchar(10)

SET @startdate = '2/12/06'
set @enddate   = '10/12/06'


SET @TbillSQL = 'SELECT tbl_research_tbills.autoid,
             CONVERT(varchar(8),tbl_research_tbills.tbilldate,3) AS NEWDATE,
             tbl_research_tbills.mrr, tbl_research_tbills.subscription, '+
             'tbl_research_tbills.amountonoffer, tbl_research_tbills.allotment, tbl_research_tbills.stoprate, tbl_research_tbills.trueyield '+
             'FROM tbl_research_tbills '+
             'WHERE CONVERT(varchar(8),tbl_research_tbills.tbilldate,3) Between '''+@startdate  +
             ''' And '''+@enddate  +''' ORDER BY tbl_research_tbills.tbilldate '
print @TbillSQL
in order to execute the above code
simply use

exec (@TbillSQL)
ASKER CERTIFIED SOLUTION
Avatar of junglerover77
junglerover77

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial