vendredi 31 juillet 2015

Single Select SQL Statement for 2 different Values

I have a SQL Server table with a column called Category. In my user interface, I have a dropdown list of the category. User can select a category and click on a Search button to filter the results by the category. In the dropdown, the first option is blank. Means if the user wants to see all records from all categories, he can select blank.

In my SQL Select I have 2 statements for this

IF @Catg IS NULL
Begin
    Select * 
    From Table
End
Else
Begin
    Select * 
    From Table 
    Where Catg = @Catg
End

The Catg column in the table will have either a NULL or a category. Is this possible to do in a single SQL statement?

Aucun commentaire:

Enregistrer un commentaire