ms-access query pattern matching [duplicate] - ms-access

This question already has an answer here:
Checking string pattern in MS Access query
(1 answer)
Closed 4 years ago.
I have strings like
'Day 2',
'Day 21',
'Post-Surgery Visit -Observational Cohort',
'Day 10 -Observational Cohort'
How can I write an Access query to get strings that are like 'Day' and followed by only digits. In my example, it should return only 'Day 2', 'Day 21'. And then in the select query I want to extract the numbers 2 from 'Day 2' and 21 from 'Day 21'. How to accomplish this? It would be great if someone could help me with this

Try:
SELECT [FolderName], val(mid([FolderName],5)) As NumberPart FROM [tbl]
WHERE [FolderName] LIKE 'Day 2*'
Like expression 'Day 2*' filters records with 'Day 2', 'Day 21'.
And expression 'val(mid([FolderName],5))' will cut out 'Day ' and evaluate the rest of the string to number.

Related

Is there way to output no decimal using mysql AVG() function [duplicate]

This question already has answers here:
How can I make the decimal places of AVG function in sql limit to 2 only?
(3 answers)
Closed 6 days ago.
SELECT AVG(
TIMESTAMPDIFF(MINUTE,
customer_service_ticket_raise_time,
CURRENT_TIMESTAMP()
)
) AS avg_waiting_time
FROM customer_service_ticket;
I am writing this SQL query to display the average waiting time customers have been waiting but it outputs as a decimal value e.g. 29.8333. Is there any way I want get rid of the decimals and display the minutes as a whole value.
you can wrap your value with the CEILING function
https://www.w3schools.com/sql/func_sqlserver_ceiling.asp

Is there a way in sql to get month names from data [duplicate]

This question already has answers here:
Format date in MySQL to return MonthName, Year
(7 answers)
Closed 3 years ago.
I am selecting a data range, which is working fine with my code and returning expected data. The problem is when i add code to retrieve by months i get null.
https://www.sqltutorial.org/sql-date-functions/extract-month-from-date-sql/
SELECT SUBSTRING(date_time,1,10), SUM(Total), sum(Total_ly))
FROM Transaction
WHERE Substring(date_time,1,10) between '01/03/2019' AND '04/04/2019'
Group By date_time
Having sum(Total)<>0;
So in my table is the following:
date_time \ Total
01/03/2019 45.00
02/03/2019 34.00
23/03/2019 12.00
01/04/2019 90.00
25/04/2019 32.00
Expected output:
March 2019 91.00
April 2019 122.00
All I am doing to the total column is summing it.
Plus i know someone will ask why i am substring(date_time) for another application i need time but for this one i just need date.
Anyway what i was trying was using the MonthName function and passing the date but this will only work for one date not a range.
Can anyone point me in the right direction
use date_format() function
SELECT DATE_FORMAT(SUBSTRING(date_time,1,10),"%M %Y"), SUM(Total), sum(Total_ly))
FROM Transaction
WHERE Substring(date_time,1,10) between '01/03/2019' AND '04/04/2019'
Group By DATE_FORMAT(SUBSTRING(date_time,1,10),"%M %Y")
Having sum(Total)<>0

What can one do in a SQL SUM expr? [duplicate]

This question already has answers here:
need to return two sets of data with two different where clauses
(2 answers)
Closed 5 years ago.
The documentation for the SQL SUM function lists its "signature" like this:
SUM([DISTINCT] expr)
The only examples listed for expr are single column names (like units, price, etc.), but can one do more with it? expr sounds very broad and open...
I'm especially interested in if it's possible to do any sort of selection of what exactly to sum. For example, I'd really like to do the following right now:
SELECT
SUM(hours) 'Hours total',
SUM(hours WHERE date BETWEEN '2017-10-01' AND '2017-10-31') 'Hours last month'
But that doesn't run... Probably too much of an expr dream, but maybe...? Maybe not?
Is anything like that possible with the SUM function? What can expr be?
Maybe you could use query like:
SELECT
SUM(hours) 'Hours total',
SUM( CASE WHEN date BETWEEN '2017-10-01' AND '2017-10-31'
THEN hours
ELSE 0 END) 'Hours last month'
from table

Automatically naming columns in MySQL [duplicate]

This question already has answers here:
How can I return pivot table output in MySQL?
(10 answers)
Closed 7 years ago.
I've created table like that:
sim_mcc_mnc January February March ....
232-10 1234 4321 5678 (these numbers are number of records)
By naming each column manually:
SELECT
sim_mcc_mnc,
sum(year(time_utc)=2013 AND month(time_utc)=1) as 'January 2013',
sum(year(time_utc)=2013 AND month(time_utc)=2) as 'February 2013',
sum(year(time_utc)=2013 AND month(time_utc)=3) as 'March 2013',
sum(year(time_utc)=2013 AND month(time_utc)=4) as 'April 2013',
...
...
...
FROM
table
where
sim_mcc_mnc like '232-%' OR
sim_mcc_mnc is null
GROUP BY
sim_mcc_mnc
;
My question is. Is there any possibility to name these columns automatically? I've tried to use CONCAT, but it seems to me that you can't use any function after AS.
Is there any other possibility?
I don't know what you are trying to do with the following line...
sum(year(time_utc)=2013 AND month(time_utc)=1) as 'January 2013',
This will return the number of rows which match, not the sum of the values...
To answer your question, you have two possibilities:
using a stored procedure, which let you generate also the whole query
renaming the column names in the application part, which is the easiest way.

Truncate on a select [duplicate]

This question already has answers here:
Format number to 2 decimal places
(6 answers)
Closed 7 years ago.
I have the following query:
SELECT
SUM(`SPEND_AMOUNT`) AS 'Total Spend Amount',
SUM(`SPEND_AMOUNT`) / COUNT(DISTINCT `INVOICENUMBER`) AS 'Average Spend Amount'
FROM
`TABLE`
I would like to truncate(?) the results for "Average Spend Amount" to two decimal places.Thanks for the help.
Round(SUM(SPEND_AMOUNT) / COUNT(DISTINCT INVOICENUMBER), 2)