Sql server assistance require

ASHMITP 141 Reputation points
2021-09-09T05:06:53.463+00:00

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
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,817 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,559 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Olaf Helper 41,006 Reputation points
    2021-09-09T05:24:01.583+00:00

    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.

    0 comments No comments

  2. MelissaMa-MSFT 24,176 Reputation points
    2021-09-09T06:43:30.503+00:00

    Hi @ASHMITP ,

    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.

    0 comments No comments