I have a table with a column LastUpdated of type DateTime, and would like to add a column LastUpdated2 to this table. I would like to populate it with whatever LastUpdated is for each existing row in the table. Eg: LastUpdated 12:01 PM 5:00 PM Becomes: LastUpdated LastUpdated2 12:01 PM 12:01 PM 5:00 PM 5:00 PM Quite simply as you
Tag: sql-server
How do I auto-increment a alphanumeric string primary key?
Let’s say I have table A with an alphanumeric string primary key. The code used to create the table and how the table looks are shown below. I want to insert a new row into Table A and I don’t want to type out C000003 or C000004 every time. Is there a way to auto increment this? I have thought
How to split a string which contains delimiter into multiple rows using OPENJSON function
I am trying to split the below string using OPENJSON function in SQL Server 2019: But the result is returned as: I want to display it as this: Thanks Answer This is where my thinking brings me. Note: Unexpected results of the “‘s are NOT balanced. Example or dbFiddle Results
SQL Calculating Hourly Sales
I am trying to create a SQL report that shows hourly sales. This is from my POS system. There’s a built in table that has basically all the data I need, I’m just struggling to calculate the sum of a column if they are in the same hour. Table vRPTHourlySalesPerformance (stripped down to only show relevant info): dt_when create_hour_ordinal c_ticketitem_net_price
Counts of re-purchase same product by the same customer in certain days or within certain duration days
I need to have the counts of re-purchase same product by the same customer in tow scenarios: in certain days and within certain duration. Here is the sample data: Duration is the difference between the last EndDay and the next StartDay for same customer purchasing same product. For example: For customer A, the EndDay at the first time he purchased
Using Subqueries to remove duplicate IDs [closed]
Closed. This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed last month. Improve this question I have 2 Tables. Table 1 holds ID1 and ID2. Table 2 holds ID2 and ID3. Table 1 has unique cases for ID1 and multiple cases
Calculate date difference between dates based on a specific condition
I have a table History with the columns date, person and status and I need to know what is the total amount of time spent since it started until it reaches the finished status ( Finished status can occur multiples times). I need to get the datediff from the first time it’s created until the first time it’s with status
SQL Query for insert many values in a table and take only a value from another table
I’m looking for insert many values in a table and take the ID refernce from another table. I have tried diffent ways, and finaly I have found this that works. This works, but I’m looking for a “easy query” because I need to write the command from Visual Studio Answer The link I noted earlier should have sufficed to explain
Query using a column which contains the value of another column
I have the following table in SQL Server 2019 Note: ManagerUserID is nothing but the userid The data is How can we get user details and their manager details (they are just another user) by using one query? These are my scripts Answer You can use a subquery or a self JOIN to achieve this: Result: Optionally, you can add
How to compare two tables and if the values match, update one of the tables with the values from a third table?
I have 3 tables, the MAIN_TABLE, the SUB_TABLE and the ID_TABLE. I need to compare the CODE in the MAIN_TABLE with the CODE in the SUB_TABLE, and if they match, search for the SUB_ID in the ID_TABLE and update the ID in the MAIN_TABLE with that ID. In the example shown below, the query should update the MAIN_TABLE with the