Skip to content

Tag: sql-server

SQL Server Lag and Lead

I’m using SQL Server 2014. I have a table: I have some data: If I perform a LAG and LEAD using the below for one Job, I get the desired result: Result: Notice the NULL for the first FromActionCode and LastToCode, which is correct. However, If I look at all records, it messes it up: What am I missing? An…

SQL between date range returning a wrong result

I’m writing a SQL query to get rows between a date range format: mm/dd/yyyy I used the between operator but the result shows out of the range dates The expected result : dates of year 2020 only The actual result is: Answer I assume that you use SQL Server. Maybe you think that % is a wildcard char in th…

Query to update duplicates

I have the following query that returns duplicates that i am running on sql server. So I need to write a query that will update second instance of the email address based on the select query that retrieves dulicates to for example imports@rohnis.com to imports@rohnis.com.duplicate. If it is info@eps.ws then i…

How to calculate a group average in SQL?

I have a sql data set which look something like: I would like to be able to select the data in such a way that the query will return: With the average result calculated over the 3 years shown for each person. How could I achieve this in SQL Server? Answer I think you want a window function:

Extract substring address SQL

I have a table with column Address which is like city. City_Name, citysector. CitySector, Street. Street_Name, StreetNumber. Street_Number EXAMPLE Address c. Paris, s. ParisDowntown, str. Rue Étienne Marcel, nr. 50 How can I substract 4 different columns like city, sector, street, streetNumber from Address co…