I have a variable called _gender thats going to be either 0, 1 or 2 I want to achieve the case where _gender is everyone, then I want the gender to be 0 or 1 But if it was 0 or 1, then I should match with users that are only as the variable Answer You have to Denote Variable
Tag: sql-server
how to filter database data in sql server by per hour
if i have the data like the picture above, let’s say 1 day data per minute, and i want to filter it to be per hour, so i will have 24 data, because i take the data per hour. how to do that i have tried some queries, like this one using group by, but the result is not like
Trying to assign the value of an SQL Query to a variable SET
I am trying to get date in format yyyymm and then use that date in some operation. ERROR: The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value. Or if I use below: I get same error as above. So I want if today’s date is 17-05-2020, I want 202005 as output and
How to create a table address and query it by states?
How can I create a table with following schema: I wrote the above query for creating the table. But I’m not able to link this table to another table for the query shown below and fetch all state names and total of account balance for the users belonging to those states. Answer You should be able to use …
Use smaller than on like clause
I have some number like These numbers are only an example I have a lot of 14… a lot of 15… etc. Now in WHERE clause of a stored procedure, I want to exclude all numbers who start with 15 or less so I can do something like: Is there a way to change this and use something like TestColumn
Create a combined primary key with a one column accepting NULL
I have a table CustomerLocation with the following columns : Country Region City I want to create a composite primary key with the 3 columns mentioned above knowing that the Region column is NULLABLE. Answer You can’t. The definition of primary key specifies that only non-NULL values are allowed. This i…
Was column ever equal to x in sql
I have a dataset that looks similar to this: I want to check if the description was ever equal to X, grouped by unique ref. I would also like it to have a separate column for the date in which description was X. If the unique ref has been X more than once then the date would be the most
Set prefix in SQL CASE expression
I have problem setting a prefix in a case statement. Data set: When I run this query my result is this: Desired result is this: As one can see, the first zero is not shown in the result. How can I achieve this? Answer CASE expression would always return one type so, you need to do conversion:
SQL How to filter table with values having more than one unique value of another column
I have data table Customers that looks like this: I would like to filter the table so that only IDs with more than 1 distinct count of Sequence No remain. Expected output: I tried but I’m getting error. How to solve this? Answer You can get the desired result by using the below query. This is similar to…
How to get all rows from a SQL table before a change in column string?
I have these 2 tables and their inserts: I have this query to partition them by JobId and order them by JobId and StatusDtTm: I want to fetch only the rows where the Job last finished per job. Basically get all the records per job before it last finished (highlighted in green). I can’t seem to figure ou…