If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Limit amount of characters returned in query - SQL

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old
Registered User
 
Join Date: Jan 2002
Location: Nottingham - UK
Posts: 113
Cool Limit amount of characters returned in query - SQL

Hi folks...

Can anyone tell me how to limit the amount of characters returned in a query, I need to bring dispaly the first 20 characters only from a 255 varchar field ?

Thanks in advance if you can.

Donald
Reply With Quote
  #2 (permalink)  
Old
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 12,056
Depending on what SQL engine you are using the LEFT function may do what you want

eg select left(<mycolumn>,20) as <myColumnsFirst20Characters> from <mytable>
HTH
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #3 (permalink)  
Old
Registered User
 
Join Date: Jul 2003
Posts: 34
use convert

Quote:
Originally Posted by donaldt
Hi folks...

Can anyone tell me how to limit the amount of characters returned in a query, I need to bring dispaly the first 20 characters only from a 255 varchar field ?

Thanks in advance if you can.

Donald
select convert(varchar(20), <field>) from <table name> should give you first 20 characters from a field.
Reply With Quote
  #4 (permalink)  
Old
Lost Boy
 
Join Date: Jan 2004
Location: Croatia, Europe
Posts: 3,980
Or, in Oracle, use the SUBSTR function:

SELECT SUBSTR(column, start_position, desired_length) FROM some_table;
which would then be
SELECT SUBSTR(column, 1, 20) FROM some_table;
Reply With Quote
  #5 (permalink)  
Old
Registered User
 
Join Date: Dec 2003
Posts: 39
If u want to show max 20 characters of all query results, u can change the options at Query Analyzer :
Tools - Options - Results - Maximum characters per column
__________________
Link Link
Reply With Quote
  #6 (permalink)  
Old
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,126
The "standard SQL" way to do this is
Code:
CAST(my_column AS CHAR(20))
This should also be the most efficient way of doing things since CAST is not a scalar function but a built-in functionality.
Of course, most people will use a scalar function like SUBSTR, SUBSTRING or LEFT, but for one thing these names are not standardised so your SQL becomes less portable, and secondly there may be a performance hit when calling scalar functions.
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 On
Pingbacks are On
Refbacks are On