简体   繁体   中英

how to get current month in T-SQL sql server(2008)

CREATE TABLE _Transaction (
transaction_id int,
date_time datetime default sysdatetime(),
amount real,
description varchar(500),
responsible_party varchar(50),

CONSTRAINT _transaction_pk PRIMARY KEY(transaction_id),
CONSTRAINT check_res_prty CHECK(responsible_party IN ('[ATM]','[Teller]','[Bank]','[Standing Order]','[Cheque]','[On-Line]','[Other]'))
 );

CREATE TABLE _Transaction_account(
account_no int,
transaction_id int,
affect_type varchar(50),

CONSTRAINT _Transaction_account_pk PRIMARY KEY(account_no,transaction_id),
CONSTRAINT _Transaction_account_fk1 FOREIGN KEY(transaction_id) REFERENCES _transaction(transaction_id),
CONSTRAINT _Transaction_account_fk2 FOREIGN KEY(account_no) REFERENCES account(account_no),
CONSTRAINT check_affect_type CHECK(affect_type IN ('[credit]','[debit]'))
);

I want to write a stored procedure to above tables which takes the account number and returns the number of cheques processed for that account during the current month. so I wrote the below stored procedure

CREATE PROCEDURE pro_check_cheques (@accountNo int)
AS
BEGIN
    DECLARE @chques int
    SELECT @chques=COUNT(t.responsible_party)
    FROM _Transaction t JOIN _Transaction_account ta ON
        ta.account_no=@accountNo AND ta.transaction_id=t.transaction_id
    WHERE t.responsible_party = '[Cheque]' AND t.date_time=MONTH(GETDATE())
    print  @chques;
END

but this is not works. always get the 0 counts when I execute the sproc, but when I delete the part that "AND t.date_time=MONTH(GETDATE())" is, it works fine. please can anyone help me? thanks

MONTH simply returns the month number of the year, so if you are comparing so you end up with something like WHERE 2014-03-26 = 3 , which is false. You can use this to get the start of the current month:

SELECT DATEADD(MONTH, DATEDIFF(MONTH, '19000101', GETDATE()), '19000101')

And this to get the start of next month:

SELECT DATEADD(MONTH, DATEDIFF(MONTH, '19000101', GETDATE()), '19000201')

You can then apply this to your query, ie where the date is greater than or equal the start of the current month, and less than the start of the next month:

SELECT @chques=COUNT(t.responsible_party)
FROM _Transaction t JOIN _Transaction_account ta ON
    ta.account_no=@accountNo AND ta.transaction_id=t.transaction_id
WHERE t.responsible_party = '[Cheque]' 
AND t.date_time >= DATEADD(MONTH, DATEDIFF(MONTH, '19000101', GETDATE()), '19000101')
AND t.date_time < DATEADD(MONTH, DATEDIFF(MONTH, '19000101', GETDATE()), '19000201')

ADDENDUM

In response to your comment, I have only passed 3 arguments to the DATEDIFF funciton, and also 3 to the DATEADD function. This works by first getting the number of months between now and 1900-01-01 (this is currently 1370)).

SELECT DATEDIFF(MONTH, '19000101', GETDATE())

Because DATEDIFF only counts the number of boundaries crossed this will return the same number for all days in the month, whether it is the first or last. It then adds this number of months to 1900-01-01 , which will bring you back to the first of the current month.

DATEADD(MONTH, <datediff calculation>, '19000101')

You could actually use any date you like for this as long as it is the first of a month, and you use the same date in both the date add and the datediff.

So this will still return the start of this month:

SELECT DATEADD(MONTH, DATEDIFF(MONTH, '20500101', GETDATE()), '20500101')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM