In many data assimilation problems, the state variables are subjected to inequality constraints. These constraints often contain valuable information that must be taken into account in the estimation process. However, with linear estimation methods (like the Kalman filter), there is no way to incorporate optimally that kind of additional information. In this study, it is shown that an optimal filter dealing with inequality constraints can be formulated under the assumption that the probability distributions are truncated Gaussian distributions. The statistical tools needed to implement this truncated Gaussian filter are described. It is also shown how the filter can be adapted to work in a reduced dimension space, and how it can be simplified following several additional hypotheses. As an application, the truncated Gaussian assumption is shown to be adequate to deal with the condition of hydrostatic stability in ocean analyses. First, a detailed evaluation of the method is made using a one-dimensional z-coordinate model of the mixed layer: particular attention is paid to the parameterization of the probability distribution, the accuracy of the estimation and the sensitivity to the observation system. In a second step, the method is applied to a three-dimensional hybrid coordinate ocean model (HYCOM) of the Bay of Biscay (at a 1/15° resolution), to show that it is efficient enough to be applied to real size problems. These examples also demonstrate that the algorithm can deal with the hydrostatic stability condition in isopycnic coordinates as well as in z-coordinates.