IN - sql? (Full Version)

All Forums >> [Web Development] >> ASP, PHP, and Database



Message


siask -> IN - sql? (7/1/2009 3:05:23)

I'm struggling....

Have a field with many numbers listed.... ie: 4280, 3456, 3788.... and so on...
I was under the impression that a simple IN will pick that require number from list and display...

I've used this very simple custom code

SELECT* FROM CamoBasket
WHERE CN_No IN ('::CN_No::')

CN_no is the field name
CamoBasket the Table name....

I can't change from Text to number as some manufacturers may use text characters.

Now this does work if the required number is the only number in the field.... as soon as it notes other numbers listed (comma separated) it doesn't work!

Wha'd ya reckon?...




TexasWebDevelopers -> RE: IN - sql? (7/1/2009 10:28:26)

Is there an error message and if so what is it?
How are you handling the comma spearators and the single quotes?
This will work:
WHERE CN_No IN ('123','124','125','126')
but his, with the trailing comma after 126, will not:
WHERE CN_No IN ('123','124','125','126',)
and this will not:
WHERE CN_No IN ('123,124,125,126')

Note that all the variables have to have single quotes around them.

You might want to response.write(your_sql) to see exactly what your sql statement looks like.

We use a function to strip out the trailing comma after adding in the single quotes something like this:

<script language='jscript' runat='server'>
function commaKiller(x){ return x.replace(/,*$/,''); };
</script>

<%
str = str & "'" & Rs("CN_No") & "'," ' this bit adds single quotes and commas
newCN_No=commaKiller(str) ' this function call strips out the last comma in the array
%>

sql = "SELECT * FROM [table-name] WHERE CN_No IN (" & newCN_No & ");"




siask -> RE: IN - sql? (7/1/2009 11:22:23)

Thanks for the response TWD...

I'm not getting any error messages at the moment. It's just not finding the records that have more than one set of numbers in the field....

for example... if I select 4280 from a dropdown, i get all the records where 4280 is the only info in the CN_No field..... but i know one record has '4280','1111' within it....

I've been scratching my head (and other bits) for the last 2 days trying to figure it out....

I can't really use CONTAINS or LIKE as we have 820 as a CN_No as well.... but it must not be displayed due to data issues within the company.... only the records linked to 4280....

I have thought about a different field for each number, which would be easy..... but i know of one client that may have over 80 CN_no's to add.... so, comma separated list is the only way i can think to go?

This will only ever have one or two end users within our company, so its easy to instruct them to enter this data in a specific format... no real need to make it idiot-proof (famous last words!)

Ta muchly Si




TexasWebDevelopers -> RE: IN - sql? (7/1/2009 13:13:58)

OK--maybe it would have been better if I had read your original post more carefully...you have A field (one field) with a string of numerals in it.

Well CONTAINS or LIKE would be the standard way to go, as you already noted.

But really the way to go, as you also noted, is to have a table with individual CN_no values that relate back to the client name.

From your post it sounds like your client is entering the CN_no values in a form that posts to the db?

Or are you importing the data from a CSV?

If from a form you could separate the string at the commas and update the data as separate fields "on the fly"... there are a lot of ways to do this.







siask -> RE: IN - sql? (7/1/2009 14:10:25)

Yep....

'If from a form you could separate the string at the commas and update the data as separate fields "on the fly"... there are a lot of ways to do this.

The data is selected from a DRW dropdown on a separate page... i've then set it up to post to a final page that consists of 4 different DRW's, on of them being this problem one.... the other 3 all need the same info (CN_no)....

I'm a newbee, if you hadn't guessed... I think the problem may lay with the ('::CN_No::') ... as i understand.... if i select 4820 from the DRW dropdown (using this because it'll only show the CN_No's already in the DB and nothing else)....
Anyway, select 4820 and the 'on-the-fly- statement posted is bascially

SELECT * FROM CamoBasket WHERE CN_No IN (4820) ?.....

If this is right, i can see that the ' ' are missing.... I cant add them as the other 3 DRW won't work...

Sorry for being thick... and thanks for your time to answer...




Page: [1]

Valid CSS!




Forum Software © ASPPlayground.NET Advanced Edition 2.4.5 ANSI
0.078125