I have a query for the last 10 days. I also have a variable declared. If the variable is empty string I want to query the last 10 days (works fine). If the variable string is not empty I want to search all rows not just the last 10 days. I cannot put an if condition around the WHERE clause.
Tag: sql-server
SQL Server 2008 R2 split one row of results into two rows
In simple terms the table (t1) looks like this: I’d like to get results from the table that looks like this: Thanks Answer You may try simple query, using operator Union:
Sort by year as nvarchar and then numerically
I have an nvarchar column that in essence stores the following data: The first two numbers represent the year created and the last numbers represent the id of the unit made that year. Whenever I sort I get the 99’s first and 19 last. I’d like to sort by latest year first and then numerically. I ha…
Can this be done as a SQL VIEW
I have a SQL Server table of Customer’s Events: There can be many EventTypes for the same customer in one day. EventTypes are like 1 – CheckIn 2 – CheckOut 3 – ExamStart 4 – ExamEnd Now I want to select Customers that are currently (today) on premises. That’s Clients who ha…
Can I SELECT all the bult-in functions in SQL Server?
Is it possible to display every built-in function (like LEFT, SIN, IIF etc.) with a query? Answer Documentation or syntax highlighting definition from official tool: Azure Data Studio – syntax support Searching for: “name”: “support.function Idea – Not tested: Maybe it is possibl…
Is there a way to look at the previous evaluation within a CASE expression?
I’ve created the following table in Excel as an example. I would like to do a similar expression (like the formula shown in Excel) in SQL Server on a similar looking table. The columns row_num and switch were already created based on previous CASE statements. Click for Excel sheet Is there any way to ac…
SQL request from 2 different databases with same structure
I have 2 really similar databases, with exact same structure (one of them is a backup of the other so some values changed which is why they are similar but not the exact same). So here is what I would …
Condition within aggregate query vs math
I have these two query examples with small difference that i thought would be performance optimization but there is no difference. The small change is that in one of the queries there is conditional logic within the aggregate while in the other one i use simple math to get the same result. I would have though…
Reserve a range of values in a table in SQL Server
I want to reserve a range of values for the system like [1-10,000]. The user values should be inserted after 10,000. eg. There is a table that will have values inserted by the system & will also have values inserted by the user. So, when a system inserts, the id’s assigned to it will have to be betw…
If Row from left joined table is null use another row
I have two table that I am trying to join(see below). Each have the same four columns on which I need to join. The second is the complete list, while the first may be missing some records. Where the records are missing I want to use the lowest ROWNUM that matches the other three. I am failing to see a