Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm just new to sql server query. I have a column named duedate. I want to filter duedate data within the current month.
SELECT * FROM TABLE1 WHERE duedate = within current month only
Thanks in advance.
You should use a range query rather than wrapping the column in a function call so that an index may be used.
SELECT *
FROM TABLE1
WHERE duedate >= DATEADD(month, DATEDIFF(month, 0, getdate()), 0)
AND duedate < DATEADD(month, 1 + DATEDIFF(month, 0, getdate()), 0)
Related
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 2 years ago.
Improve this question
I have an data which includes Processeddate, start time and end time. I need to get minimum starttime and max endtime for each day.
Process date should be the first coulmn
Just use min() and max() function as shown below.
;WITH cte
AS (
SELECT Cast(processedDate AS DATE) AS processedDate
,min(startTime) minStartTime
,max(endTime) maxEndTime
FROM mytable
GROUP BY Cast(processedDate AS DATE)
)
SELECT *
FROM cte
WHERE month(processedDate) = 1
AND year(processedDate) = 2020
ORDER BY processedDate
db<>fiddle
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 6 years ago.
Improve this question
Okay, I have a situation when I need to compare a column from a table with today's date. If the value in a particular row is earlier than today (in other words, the date has already passed, I need to mark a virtual column with the phrase 'lapsed'
Below is the SQL (SQL Server 2012) that I have been using:
SELECT
datediff(day, sysdatetime(), policy_expiration_dt) As 'DayDiff'
,Case When 'DayDiff' < 0 Then 'Lapsed'
FROM TABLE_NAME
The first column that includes the datediff function makes the comparison and the second column marks it as lapsed if the date has passed.
basically the order of how an sql statement is executed does not allow you to use the alias. so you will need to use the datediff() function in the CASE statement.
The order of sql queries execution is:
1. FROM
2. ON
3. OUTER
4. WHERE
5. GROUP BY
6. CUBE | ROLLUP
7. HAVING
8. SELECT
9. DISTINCT
10. ORDER BY
11. TOP
you need to do this:
SELECT DATEDIFF(day, SYSDATETIME(), policy_expiration_dt) AS 'DayDiff'
, CASE
WHEN DATEDIFF(day, SYSDATETIME(), policy_expiration_dt) < 0 THEN 'Lapsed'
END
FROM TABLE_NAME;
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I Need help to select a number of data per month, there are 4 retailers and I need to select a total of transactions per month and the total amount for that month showing each month separately in sql.
Extract the month from the date and group by it. Then you can use aggregate functions to sum and count
select month(date_column),
sum(amount) as total_amount,
count(*) as transaction_count
from your_table
group by month(date_column)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I'm trying to display hotel bookings from the 1st of every month.
Hotel (hotelNo,hotelName,hotelAddress)
Room (hotelNo,roomNo,type,price)
Guest (guestNo,guestName,guestAddress)
Booking (hotelNo,guestNo,dateFrom,dateTo,roomNo)
Here's what I have so far. Obviously it isn't working. How do I select the first of any given month?
SELECT hotelNo, dateFrom
FROM booking
WHERE datefrom >= to_date('01', 'dd');
If you want records that are on the first day of any month, then you can EXTRACT the day part of the date and compare it with 1:
SELECT hotelNo, dateFrom
FROM booking
WHERE EXTRACT(DAY FROM datefrom) = 1;
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a table
ID______credit________paydate
------------------------------
101_____10____________2012/01/01
101_____13____________2012/02/01
101_____8 ____________2012/03/01
101_____3 ____________2012/03/01
101_____22____________2012/05/01
..._____..____________..........
..._____..____________..........
999_____13____________2012/07/01
999_____38____________2012/08/01
I want to select all records for last 3 months(it is different last paydate for every ID) and where credit < 10
can someone help me with this??
thx and regards
You could use the DATEADD function to get the desired result:
SELECT *
FROM table
WHERE paydate >= DATEADD(month, -3, GETDATE())
AND credit < 10