question

carolmaga8 avatar image
carolmaga8 asked

Convert varchar to int in a where clause

Hi, guys! I am trying to conver a variable that is varchar to int in a where clause, but every time I try I get an error message: 'Conversion failed when converting the varchar value 'Total Number of Seats' to data type int.' select * from ( select maker as 'Maker', name as 'Name', type as 'Type', start_date as 'Start Date', end_date as 'End Date', model as 'Model', CASE TYPE WHEN 'NARROW' THEN (N_N_SEATS_SECTOR_L+N_N_SEATS_SECTOR_R)*N_ROWS ELSE (W_N_SEATS_SECTOR_C+W_N_SEATS_SECTOR_L+W_N_SEATS_SECTOR_R)*N_ROWS END as 'Total Number of Seats', CASE TYPE WHEN 'NARROW' THEN (N_N_SEATS_SECTOR_L+N_N_SEATS_SECTOR_R)*N_ROWS_w ELSE (W_N_SEATS_SECTOR_C+W_N_SEATS_SECTOR_L+W_N_SEATS_SECTOR_R)*N_ROWS_w END as 'Number of Seats Class' from aircraft) a where cast( 'Total Number of Seats' as int) >= 200 can anyone help me?? thanks!
sqlwherevarcharint
2 comments
10 |1200 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Try wrapping it in [square brackets] instead of 'apostrophes' in that cast statement
1 Like 1 ·
Thanks, both worked!!
0 Likes 0 ·

1 Answer

· Write an Answer
cornpoppy avatar image
cornpoppy answered
You need to change the where clause : where (CASE TYPE WHEN 'NARROW' THEN (N_N_SEATS_SECTOR_L+N_N_SEATS_SECTOR_R)*N_ROWS ELSE (W_N_SEATS_SECTOR_C+W_N_SEATS_SECTOR_L+W_N_SEATS_SECTOR_R)*N_ROWS END) >= 200
10 |1200 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.