Where are the CONVERT & CAST functions in MDB?

G

Guest

I am trying to compare 2 fields with different data types (text and single),
but I cannot the SQL type-conversion functions that works in SQL Server
aren't recognised. Is this a profound shortcoming of Access 2003, or is the
information I want just carefully hidden or omitted from Help?
 
6

'69 Camaro

Hi, Allen.
I am trying to compare 2 fields with different data types (text and
single),
but I cannot the SQL type-conversion functions that works in SQL Server
aren't recognised.

T-SQL syntax doesn't work the same (or often not at all) in Jet SQL or in
Access VBA, just like it won't work in PL/SQL, SQL*Plus, or MySQL. It's a
different dialect of SQL that works in SQL Server, but can't be expected to
be used with other database products.
Is this a profound shortcoming of Access 2003, or is the
information I want just carefully hidden or omitted from Help?

From Access 2003 online help:

Type Conversion Functions


Each function coerces an expression to a specific data type.

Syntax

CBool(expression)

CByte(expression)

CCur(expression)

CDate(expression)

CDbl(expression)

CDec(expression)

CInt(expression)

CLng(expression)

CSng(expression)

CStr(expression)

CVar(expression)

The required expression argument is any string expression or numeric
expression.

Return Types

The function name determines the return type as shown in the following:

Function Return Type Range for expression argument
CBool Boolean Any valid string or numeric expression.
CByte Byte 0 to 255.
CCur Currency -922,337,203,685,477.5808 to 922,337,203,685,477.5807.
CDate Date Any valid date expression.
CDbl Double -1.79769313486231E308 to
-4.94065645841247E-324 for negative values; 4.94065645841247E-324 to
1.79769313486232E308 for positive values.
CDec Decimal +/-79,228,162,514,264,337,593,543,950,335 for zero-scaled
numbers, that is, numbers with no decimal places. For numbers with 28
decimal places, the range is
+/-7.9228162514264337593543950335. The smallest possible non-zero number is
0.0000000000000000000000000001.
CInt Integer -32,768 to 32,767; fractions are rounded.
CLng Long -2,147,483,648 to 2,147,483,647; fractions are rounded.
CSng Single -3.402823E38 to -1.401298E-45 for negative values; 1.401298E-45
to 3.402823E38 for positive values.
CStr String Returns for CStr depend on the expression argument.
CVar Variant Same range as Double for numerics. Same range as String for
non-numerics.



Remarks

If the expression passed to the function is outside the range of the data
type being converted to, an error occurs.

In general, you can document your code using the data-type conversion
functions to show that the result of some operation should be expressed as a
particular data type rather than the default data type. For example, use
CCur to force currency arithmetic in cases where single-precision,
double-precision, or integer arithmetic normally would occur.

You should use the data-type conversion functions instead of Val to provide
internationally aware conversions from one data type to another. For
example, when you use CCur, different decimal separators, different thousand
separators, and various currency options are properly recognized depending
on the locale setting of your computer.

When the fractional part is exactly 0.5, CInt and CLng always round it to
the nearest even number. For example, 0.5 rounds to 0, and 1.5 rounds to 2.
CInt and CLng differ from the Fix and Int functions, which truncate, rather
than round, the fractional part of a number. Also, Fix and Int always return
a value of the same type as is passed in.

Use the IsDate function to determine if date can be converted to a date or
time. CDate recognizes date literals and time literals as well as some
numbers that fall within the range of acceptable dates. When converting a
number to a date, the whole number portion is converted to a date. Any
fractional part of the number is converted to a time of day, starting at
midnight.

CDate recognizes date formats according to the locale setting of your
system. The correct order of day, month, and year may not be determined if
it is provided in a format other than one of the recognized date settings.
In addition, a long date format is not recognized if it also contains the
day-of-the-week string.

A CVDate function is also provided for compatibility with previous versions
of Visual Basic. The syntax of the CVDate function is identical to the CDate
function, however, CVDate returns a Variant whose subtype is Date instead of
an actual Date type. Since there is now an intrinsic Date type, there is no
further need for CVDate. The same effect can be achieved by converting an
expression to a Date, and then assigning it to a Variant. This technique is
consistent with the conversion of all other intrinsic types to their
equivalent Variant subtypes.

Note The CDec function does not return a discrete data type; instead, it
always returns a Variant whose value has been converted to a Decimal
subtype.

HTH.
Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips and tutorials.
Blog: http://DataDevilDog.BlogSpot.com
http://www.Access.QBuilt.com/html/expert_contributors2.html for contact
info.
 
R

Rick Brandt

Allen_N said:
I am trying to compare 2 fields with different data types (text and
single), but I cannot the SQL type-conversion functions that works in
SQL Server aren't recognised. Is this a profound shortcoming of
Access 2003, or is the information I want just carefully hidden or
omitted from Help?

CStr()
CDbl()
CInt()
CLng()
CDate()
 
A

Albert D. Kallal

Allen_N said:
I am trying to compare 2 fields with different data types (text and
single),
but I cannot the SQL type-conversion functions that works in SQL Server
aren't recognised. Is this a profound shortcoming of Access 2003, or is
the
information I want just carefully hidden or omitted from Help?

The really neat thing about sql in ms-access is that you can use any visual
basic function.

While in the code editor, select help menu...1st option (Microsoft Visual
Basic Help)

On the right side..you see a list of options, and in the search box type:

conversion functions

The first hit in the list will give you the functions you can use....

The help in ms-access is split between enc users that don't know code (the
interface side), and the code side...

For more coding questions....use the help from the code editor...you get a
complete different set of information
and results then that if you use the help from the "user" side that is not
for developers.
 
G

Guest

Thanks Gunny,

I thought those functions were only for VBA code, not SQL. Now I'm straight.
 
6

'69 Camaro

O

onedaywhen

On Feb 20, 11:42 pm, "'69 Camaro" > > I am trying to compare 2 fields
with different data types (text and
T-SQL syntax doesn't work the same (or often not at all) in Jet SQL or in
Access VBA, just like it won't work in PL/SQL, SQL*Plus, or MySQL. It's a
different dialect of SQL that works in SQL Server, but can't be expected to
be used with other database products.

CAST is standard SQL-92 syntax and there is a reasonable expectation
that a given SQL product will support it. Sadly, Jet 4.0/ACE is non-
compliant in this regard :(
From Access 2003 online help:

Type Conversion Functions

Each function coerces an expression to a specific data type <<snipped>>

But that is the VBA help. The OP is referring to SQL code. AFAIK there
is no 'official' list of VBA functions supported in Jet SQL (still no
spec for Jet) but broadly it is the VBA5 scalar functions.
CDec(expression)

The CDec function does not return a discrete data type; instead, it
always returns a Variant whose value has been converted to aDecimal
subtype.

The CDEC() function is broken in SQL. See:

ACC2000: "Wrong Number of Arguments" Error When You Use CDec Function
in a Query
http://support.microsoft.com/default.aspx?scid=kb;en-us;Q225931

"The CDec() function is supported in Visual Basic for Applications
code, but not in Access queries."

The article mentions Access 2000 only but the problem still persists
in ACE (Access2007).
You should use the data-type conversion functions instead of Val to provide
internationally aware conversions from one data type to another.

The main reason for avoiding the VAL() function is that it always
return a double floating point value.
A CVDate function is also provided for compatibility with previous versions
of Visual Basic. The syntax of the CVDate function is identical to the CDate
function, however, CVDate returns a Variant whose subtype is Date instead of
an actual Date type.

Jet SQL has no Variant data type.

Jamie.

--
 
O

onedaywhen

The really neat thing about sql in ms-access is that you can use any visual
basic function.

"Any VBA function"? What about this:

? CurrentProject.Connection.Execute("SELECT Join(Array('Jet', 'SQL'),
' ')")(0)

I get an 'Undefined function Join in expression' error. No, rather
than assuming any VBA function will work in SQL code, there should be
a definitive list but there isn't so we have to use trial and error :(

Jamie.

--
 
D

David W. Fenton

The built-in VBA functions can be called by the Jet Expression
Service, so they'll work in your queries, too.

T-SQL isn't SQL, either. T-SQL is just as foreign to SQL Server SQL
as VBA is to Jet SQL. In both cases, there's an expression service
that recognizes and handles calls to non-SQL functions.
 
O

onedaywhen

there's an expression service
that recognizes and handles calls to non-SQL functions.

In the case of Jet, how do you define "non-SQL functions"? When I use
Jet outside of the Access user interface, I can use VBA5 scalar
expressions such as INSTR() but cannot use VBA6 scalar expressions
such as Replace()? Also, some VBA5 scalar expressions have different
functionality when used in Jet SQL e.g.

Jet SQL:
SELECT IIF(1 = 1, 55 / 11, 99 / 0)
returns 5

VBA:
? IIf(1 = 1, 55 / 11, 99 / 0)
errors with 'Division by zero'.

My assumption is that Jet has its own 'implementation' of such
expressions, so these should be considered "Jet SQL functions" (or
perhaps "Jet SQL scalar expressions").
T-SQL is just as foreign to SQL Server SQL
as VBA is to Jet SQL.

Sorry, I don't understand the distinction you are making between "T-
SQL" and "SQL Server SQL". Please clarify.

TIA,
Jamie.

--
 
G

Guest

Hi Jamie,

This is completely unrelated to the actual topic of the post ~ sorry ~ but
(if I may be so bold) I'm curious to the nature (and what it denotes) of your
username: onedaywhen ...
 
D

David W. Fenton

In the case of Jet, how do you define "non-SQL functions"? When I
use Jet outside of the Access user interface, I can use VBA5
scalar expressions such as INSTR() but cannot use VBA6 scalar
expressions such as Replace()? Also, some VBA5 scalar expressions
have different functionality when used in Jet SQL e.g.

Jet SQL:
SELECT IIF(1 = 1, 55 / 11, 99 / 0)
returns 5

VBA:
? IIf(1 = 1, 55 / 11, 99 / 0)
errors with 'Division by zero'.

My assumption is that Jet has its own 'implementation' of such
expressions, so these should be considered "Jet SQL functions" (or
perhaps "Jet SQL scalar expressions").

This is actually a fairly foggy area, from what I understand. It
seems that Jet has its own expression service as well as some things
that can work within Jet itself, but when Jet is called within
Access a more versatile expression service is used. I have never
quite figured out the relationship between the two nor how it works.

I do know that many functions work differently when called in SQL
than when called in VBA (e.g., some of them have optional arguments
in VBA that have to be supplied when used in SQL).
Sorry, I don't understand the distinction you are making between
"T- SQL" and "SQL Server SQL". Please clarify.

SQL is SQL. Functions that aren't part of the SQL language are
something else.
 
O

onedaywhen

Hi Jamie,

This is completely unrelated to the actual topic of the post ~ sorry ~ but
(if I may be so bold) I'm curious to the nature (and what it denotes) of your
username:blush:nedaywhen...

I recently got a bug report entitled, "Little poem in <App.Title>..."
There was a screenshot with the following text in a messagebox: "One
day when the weather is warm | I'll wake up on a hill..." i.e. the
standard piece of text I use to test (I never like that "Lorem ipsum
dolor" one <g>). Remember, a 'search and replace' does not reach hard-
coded textbox values...

It's the first line to an album by Joe Henry; if you are looking for a
recommendation, go straight to his 'Tiny Voices' album (http://
www.joehenrylovesyoumadly.com/), the first line of which is, "On the
afternoon | That the revolution began | I was in a hotel pool with
another kid | And an Australian business man..." You gotta wonder how
that one turns out, right?

I removed the moniker onedaywhen from my deja/google profile years ago
but it still appears in groups of which I'm not a member.

Jamie

--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top