This is the table created and inserted values into it in SQL server And using WHILE loop, I achieved the below query in SQL Server. Now I want the same above result with the usage of FOR loop, I know in SQL server FOR loop is not used, but with WHILE loop we can simulate the FOR loop in SQL
Tag: sql-server-2008
How to select 2 boolean columns and retrieve 1 boolean result (both must be true = true)
In SQL Server I have multiple bit columns. If either the product = False or the Category = False, I want to return False. My test code: But all 3 selects crash. My actual code snippet: Of course I can do this with two If-Thens but my current query is pleasantly one clean query so I was hoping to do
group by category to show difference by month in SQL
I have a view that brings the total number of members in each class for each month however I want to show the growth by subtracting the total_count of the previous month with the current month in a separate column. For example, I would like to view the data as mentioned in the attached image. Here on that table, you
Need output by combining DATEDIFF(hh,StartTime,EndTime) + ‘Minutes’ in SQL Server
When running this query, I need output by combining I’m getting this error Conversion failed when converting the varchar value ‘Minutes’ to data type int I need to achieve the output like 15 Minutes. (15 represents the difference between StartTime and EndTime) Answer DATEDIFF function returns a INT values. So you need to CAST it to a string. Try something
Updating a table in SQL Server with information from another table
I have a “claims” table. Each claim has a client attached to it which is represented by a few letters “ABED”. For some reason, the actual clientID is nowhere to be found in the claims table so I want to fix this. The “Clients” table DOES contain both the code AND clientID. How do I run a query where I
Showing conditional Where Condition in SQL SERVER
I am trying to achieve something like this. SELECT Name, Age From Employee WHERE ID=1 AND CASE(WHEN backguid=’00000000-0000-0000-0000-000000000000′ THEN (State=@state and City = @city) ELSE (…
Conditionally convert a decimal value to integer
It is showing a result like this But I want result like this when type is 1 then result should be in integer format: on else condition (@Type rather than 1) I want result like this I have tried this query Answer This will work for you. When @type is 1, the result will be 10 for both values. Otherwise
How to split one column with array merge value to array and join other table then merge back
There’s a table WaitCheckBil like : and a table Users like : Expected result : What I’ve tried & think : It’s a old system’s table , so I can’t change the struct. And the table logic is select * from WaitCheckBil where usrs like ‘%;’ + @usr + ‘;%’; to get user’s wait check bil_NO. I think it could
Show all varchar values with PIVOT
I need to show all varchar values with pivot but I’m not sure if that is possible? I have OCCUPATIONS table: Pivot query: Query result: Above query gives only 1 record in each column but I want to get all. Answer You ‘ll need an extra column, e.g. with ROW_NUMBER():
Need help to build a sales funnel report by sql query
I have created a view for sales.In this view, there are relations among lead, opportunity and quotation. We can see not every lead turns to opportunity and quotation. LeadID OfferingID QuotationID Product Salesperson Department Date Salesprice My final output for the sales funnel for all department will be like [total number of leads (6)]->[total number of offering(5)]->[total number of quotations(3)].