• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 392
  • Last Modified:

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!!!
0
souldj
Asked:
souldj
  • 4
  • 3
  • 2
  • +1
1 Solution
 
Arthur_WoodCommented:
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
0
 
souldjAuthor Commented:
The Date is in the format 04-Jan-96 will this be recognised by MSSQL as a Date?
0
 
Arthur_WoodCommented:
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
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
souldjAuthor Commented:
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!
0
 
souldjAuthor Commented:
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?
0
 
Aneesh RetnakaranDatabase AdministratorCommented:
there is no format function in SQL server,
you can use CONVERT for this, please refer BOL for convert
0
 
souldjAuthor Commented:
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
0
 
Aneesh RetnakaranDatabase AdministratorCommented:
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
0
 
Aneesh RetnakaranDatabase AdministratorCommented:
in order to execute the above code
simply use

exec (@TbillSQL)
0
 
junglerover77Commented:
According to your syntax, I think you are using VB or VB.NET. So just try the following statement:

  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(tbl_research_tbills.tbilldate) Between CDate('" & startdate & "') And CDate('" & enddate & "') " & _
            "ORDER BY CDate(tbl_research_tbills.tbilldate)"

This statement is based on:
"tbl_research_tbills.tbilldate" column is char or varchar;
startedate, enddate are string.

If all of them are in "yyyy-MM-dd" format, such as "2006-04-26", that statement would work perfectly. If not, please show me your sample values of the column and variables, I will give you further instruction.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now