question

ASHMITP-0361 avatar image
0 Votes"
ASHMITP-0361 asked MelissaMa-msft commented

Sql server assistance require

Hi there,

How to write the below query in ms sql

If(Num(Month(TempDate))>6,Year(TempDate)&'/'&Year(addmonths(TempDate,12)),Year(Addmonths(TempDate,-12))&'/'&Year(TempDate)) AS FinancialYear

Many Thanks

sql-server-generalsql-server-transact-sql
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @ASHMITP-0361,

Could you please validate all the answers so far and provide any update?

Please remember to accept the answers if they helped. Your action would be helpful to other users who encounter the same issue and read this thread. 

Thank you for understanding!

Best regards,
Melissa

0 Votes 0 ·
OlafHelper-2800 avatar image
0 Votes"
OlafHelper-2800 answered

That's to less on information.

Please post table design as DDL, some sample data as DML statement and the expected result.

Additional, in T-SQL you can use for an IF statement the CASE/WHEN/ELSE condition.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MelissaMa-msft avatar image
0 Votes"
MelissaMa-msft answered

Hi @ASHMITP-0361,

Please refer below:

 create table #temptable
 (TempDate date)
    
 insert into #temptable values
 ('2019-03-01'),
 ('2020-06-01'),
 ('2021-08-01')
    
 select iif(MONTH(TempDate)>6,cast(year(TempDate) as char(4))+'/'+cast(year(TempDate)+1 as char(4)),
 cast(year(TempDate)-1 as char(4))+'/'+cast(year(TempDate) as char(4))) AS FinancialYear
 from #temptable

Output:

 FinancialYear
 2018/2019
 2019/2020
 2021/2022

Best regards,
Melissa


If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.