Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Database > SQL Language
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old July 3rd, 2003, 10:58 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Cardiff, , United Kingdom.
Posts: 231
Thanks: 0
Thanked 0 Times in 0 Posts
Default CAST or CONVERT in Access

The CAST and CONVERT column functions do not seem to work in MS Access. How can I convert a text column to an integer column?

For example, I have read that in SQL2 you can use the function CAST(Col1 AS int) and in T-SQL you can use CONVERT(Col1, int) to convert Col1 to an integer.

Neither of these seem to work in Access, am I doing something wrong, is it not possible, or is there another function I can use?

Regards
Owain Williams
__________________
Regards
Owain Williams
Reply With Quote
  #2 (permalink)  
Old July 3rd, 2003, 11:05 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Naperville, IL, USA.
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Try the CInt or the CLng function, depending upon whether you want an integer (16 bit) or a long integer (32 bit).

Rand
Reply With Quote
  #3 (permalink)  
Old July 3rd, 2003, 11:09 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Cardiff, , United Kingdom.
Posts: 231
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you for your swift reply. That worked a treat ;)

Regards
Owain Williams
Reply With Quote
  #4 (permalink)  
Old July 3rd, 2003, 11:11 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Naperville, IL, USA.
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Since CInt and CLng are VB functions, they WILL NOT WORK in SQL Server directly.

Rand
Reply With Quote
  #5 (permalink)  
Old July 4th, 2003, 04:01 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Cardiff, , United Kingdom.
Posts: 231
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the advice, however I have hit another problem. The field can contain NULL values, so using CLng can raise a run-time error '94': Invalid use of Null. Just to clarify, the statement I am using is:

Code:
SELECT COUNT(*) FROM Image WHERE CLng(User1) > 0
I have tried adding the clause 'User1 IS NOT NULL' to the statement:

Code:
SELECT COUNT(*) FROM Image WHERE User1 IS NOT NULL OR CLng(User1) > 0
however the run-time error still occurs. Is there any way round this?

Regards
Owain Williams
Reply With Quote
  #6 (permalink)  
Old July 4th, 2003, 04:26 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Cardiff, , United Kingdom.
Posts: 231
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I have found a solution. Because the field is a text field that can contain Null values, and I am only interested in the fields that contain a number I am using the following SQL statement:

Code:
SELECT COUNT(*) FROM Image WHERE Val(User1 & "") > 0
So a Null length string is concatenated to the field to eliminate any Null values, then it is Val'ed to produce a number.

Regards
Owain Williams
Reply With Quote
  #7 (permalink)  
Old July 6th, 2003, 10:18 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Sydney, NSW, Australia.
Posts: 111
Thanks: 0
Thanked 0 Times in 0 Posts
Default

SELECT COUNT(*) FROM Image WHERE IsNumeric(User1) AND User1 IS NOT NULL

will work. I don't think you want to use VAL() if you can avoid it.

Cheers
Ken

www.adOpenStatic.com
Reply With Quote
  #8 (permalink)  
Old July 7th, 2003, 03:48 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Cardiff, , United Kingdom.
Posts: 231
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ken, your query will work, however the acceptable values for my column are a positive integer and -1. I need to be able to differentiate between the two so I am using 2 queries:

Code:
SELECT COUNT(*) FROM Image WHERE Val(User1 & "") > 0
to test for positive integers and

Code:
SELECT COUNT(*) FROM Image WHERE Val(User1 & "") = -1
to test for -1. The query you suggested will only test for the existence of a number.

Regards
Owain Williams
Reply With Quote
  #9 (permalink)  
Old July 7th, 2003, 08:55 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Hudson, MA, USA.
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

You could also try the 'nz' function:
Code:
SELECT COUNT(*) FROM Image WHERE nz(User1, 0) > 0

or

SELECT COUNT(*) FROM Image WHERE nz(User1, 0) = -1
Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
Reply With Quote
  #10 (permalink)  
Old July 7th, 2003, 09:14 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Cardiff, , United Kingdom.
Posts: 231
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Jeff that works very well, however I have not got a clue what it is doing! Would you mind pointing me in the direction of the syntax and meaning of the 'nz' function?

Regards
Owain Williams
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert Excel to Access MrQwest Access 3 February 5th, 2007 07:59 AM
Convert access reports to pdf Sara Access VBA 3 December 29th, 2004 04:51 PM
Different between CAST and CONVERT functions jdang67 SQL Server 2000 2 October 24th, 2004 12:00 PM
Convert from xml to Access database lxu XSLT 0 June 3rd, 2004 01:24 PM
Query String Cast or Convert function hoffmann Access 6 February 25th, 2004 12:58 PM



All times are GMT -4. The time now is 11:58 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.