|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
SQL Query - Howto
I have a column for availability in the Options table.
This column contains 2 records 1st record: "Monday, Tuesday, morning" 2nd record: "Monday, evening". How do i write a query that returns" 1st record: if "avail = morning" both records: if "avail = monday" both records: if "avail = monday, morning" |
|
#2
|
|||
|
|||
|
Can you break down those fields ~say~ colums avail_mon , avail_tues etc and use yes or no as values? If not, you could try using LIKE in your query.
"SELECT * WHERE Avail LIKE 'monday%'" , might work. |
|
#3
|
||||
|
||||
|
WHERE Avail IN( list of options)
|
|
#4
|
|||
|
|||
|
very cool, i'm not sure mine would have worked
|
|
#5
|
|||
|
|||
|
well neither seems to be working
the % doesn't work on %monday%morning% cause there's a Tuesday inbetween. the IN operator doesnt work either ... to explain in more detail, i want to query the database based on which multiple checkboxes are selected. So, if someone selects Monday and Thursday. It will have to query (Avail like '%Monday%') AND (Avail like '%Thursday'). This works great..but the problem is the AND. If a single checkbox is selected, i cannot append a 'AND' in the stringbuilder. |
|
#6
|
||||
|
||||
|
If someone selected Monday and Thursday, then the statement would be like this in my example.
Code:
SELECT * FROM Tablename WHERE Avail IN ('Monday', 'Thursday')
|
|
#7
|
|||
|
|||
|
Yeah, It should work but it doesn't return what i need.
Anyway, i figured out a way using LIKE and the string builder to get the result. Here's the code in (C#) (for further reference if someone needs it) StringBuilder sb = new StringBuilder(); { foreach (ListItem li in CheckBoxList1.Items) { if(li.Selected) { sb.Append("%"); sb.Append(li.Text); sb.Append("%"); } } SqlCommand sqlCmd1 = sqlConnection1.CreateCommand(); sqlCmd1.CommandType = CommandType.Text; sqlCmd1.CommandText = "SELECT Info.info_fname, Info.info_ssn, Info.info_lname, Info.info_position " + "FROM Info INNER JOIN Avl_Schedule ON Info.info_ssn = Avl_Schedule.avl_ssn " + "WHERE (Avl_Schedule.avl_daystm LIKE " + "'" + sb.ToString() + "')"; /* Other Code */ } |
![]() |
| Viewing: ASP Free Forums > Database > SQL Development > SQL Query - Howto |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|