Suppose I have 2hrs, 47minutes and 10 second (02:47:10) which is in hh:mm:ss format as a value in Time column. I would like to convert it to minutes and seconds only (mm:ss). Hence the result should be 167minutes and 10 secs in sql. I have tried using the code The result was 167.166666. My desired output should be 167:10 (mm:ss)
Tag: tsql
Calculating distance using geometry of x and y location in SQL
I’m using SQL Server and I need to calculate the distance between the x and y of a frame and the previous x and y of a frame where the day, team, and member are all the same. Currently, I have this code that works but doesn’t accomplish what I need. I’m getting every distance permutation of the x and
How to split string with delimiter and get the first value
I have the following query where I have a table which is storing from information with a delimiter SQL version – Microsoft SQL Azure (RTM) – 12.0.2000.8 Getting the following results Where I need the results to be Answer If you only interested in the first value, you don’t need to use STRING_SPLIT(). You can simply use charindex() to find
How to add a query to a table in SQL?
I have 3 tables. For simplicity I changed them to these sample tables. table1: CorporateActionSmmary table2# ProductMaster table3# OpenPosition First I need to add ISIN from table2 to table1 table1: CorporateActionSmmary So, I used this code Now as you can see the Quantity is missing in Table1 so I have to add-up all the quantities in Table3 for each product
Filter rows of a table based on a condition that implies: 1) value of a field within a range 2) id of the business and 3) date?
I want to filter a TableA, taking into account only those rows whose “TotalInvoice” field is within the minimum and maximum values expressed in a ViewB, based on month and year values and RepairShopId (the sample data only has one RepairShopId, but all the data has multiple IDs). In the view I have minimum and maximum values for each business
How to avoid rerunning a subquery used multiple times?
The following is a simplified example of a problem I’m running into. Assume I have a query “SomeQuery” (SELECT… FROM… WHERE…) that gives an output that looks like this: Status MyDate A 6/14/2021 A 6/12/2021 B 6/10/2021 A 6/8/2021 B 6/6/2021 A 6/4/2021 I need to get the earliest status A date that is greater than the maximum status B
Calculating percentages in SQL query
I have a table in SQL that has two columns of data, first is a week number, second is a value. There can be multiple records for the same week. Here’s some sample data (I added some SQL for this data at the end): id WeekNo MyAmount 1 1 5200.00 2 1 180.00 3 1 100.00 4 2 100.00 5
Why does this query return all the rows?
I have the following code: I want to get all the students who have a min grade 6, and I need to do it in subquery in where (most wanted to do it with exists, but not obligatory). My query right now returns all the students, however subquery gets not all of them. Answer If just one grade satisfies the
How to convert nvarchar string mm/dd/yyyy hh:mm:ss to datetime?
I have a nvarchar column with dates formatted like this: “04/18/2021 13:04:15” (without quotes). I want to change this column to be datetime and migrate all of it data. I’ve looked for format codes and figured out, that closest code to my variant is U.S (101). So I tried to But I get error: Error converting data type nvarchar to
How to use the results of a query to filter a table comparing one field to the result?
I want to filter a table showing only the rows where total is between ± 3 standard deviations from the average. The query I’m using is this: But I’m getting the error: An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being