I need to query a MS SQL table of events by a specific month. In this scenario, I want to return all events which took place during May 2020. I would expect to return ID’s 1, 6 and 9. I’m finding this query quick tricky, so thanks in advance for any help. Answer This article explains why you can u…
Tag: sql-server
Error :: “Conversion failed when converting the nvarchar value ‘cereals’ to data type int” in Dynamic SQL
I am trying to “Order by” my table by Id in Dynamic SQL, though it works with Name and Description (Both Type Nvarchar) as shown but not Working with Id (type INT), Here is my Dynamic SQL Query The Line Gives Following Error Conversion failed when converting the nvarchar value ‘cereals’…
INSERT SELECT statement with only one column for SELECT
I’m using SQL Server Management Studio 18 and I’ve got the following tables with the following columns: TableA TableB I want to do an INSERT SELECT into TableB, maybe something like the following: Basically, I just want to make an INSERT SELECT where I’m grabbing a column from another table …
How to transform my existing table into this specific format using T-SQL?
I am using SQL Server 2014. I have a table T1 (extract shown below): I would like to have the following output using a T-SQL query: The logic here is that data related to a specific ProfileID is to be transformed into the wide format. The StayID column is always unique. I have been looking into the pivot func…
Window functions in SQL: why are they called window functions?
I understand window function but don’t understand why they are called window functions. When we say the “window” is it both the partition and the order by clauses? Answer Window functions operate on a range of rows, defined by the OVER clause. It is like you are looking through the window, y…
How to format query result to show result in a specific json output
I am trying to learn how to format my result from SQL query to have my output as JSON. I will really appreciate if someone could guide as I will generate my results in different types of charts. For instance, one chart need to have the following JSON structure: and I am using Northwind stored procedure With t…
Proper way of getting rows since a date accounting for DST?
I have a datetime column, changedate, that I need to use to get rows that have changed in the last week since 1PM. This column is unfortunately in local time (EST). The server is Microsoft SQL Server 2016. Here is the query I have now: Since I’m using AT TIME ZONE for both the column and @since, will th…
What is the syntax of a string literal in T-SQL?
What is the full syntax of a string literal in T-SQL? I could not find any explicit definition of string syntax in the official documentation, and any blog post I could find only mentioned certain aspects (like the N prefix). Additionally, any posts on Stack Overflow or other sites that asked how to escape a …
SQL Query SELECT same data from different tables, show all records, but show / display matches
I have two tables where I would like to compare information (In order to get from the initial table to the other I need to go through a reference table). I am getting the results I am looking for …
SELECT the patients that have similar disease name EF core 3.1
I have the following tables Patients ID PatientName PatientIDNumber —- ————- —————– 1 Jo 411420607 2 Mark 206047758 …