Skip to content

Tag: sql-server

Write Condition as a value for the where clause

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

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

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: