Get year and month from SQL

后端 未结 3 769
小鲜肉
小鲜肉 2020-12-22 05:14

I want to know how can I get the year and month into my database. For example, suppose it’s August, 2011. The output that I need is like this: CAB 11 08 001 (CA

相关标签:
3条回答
  • 2020-12-22 05:27

    It looks like you're making separate columns for YEAR, MONTH, etc.

    Most (all?) DBMSs I'm aware of have a Date type. Use it. They put a lot of useful stuff in it, like accepting date inputs in different formats and giving them as output in pretty much any format you can think of.

    For example, if you have a DT column of type Date in Oracle, you can output month as

    SELECT TO_CHAR(DT, "MM") FROM MYTABLE;
    

    and the month will always be displayed as 2 digits (01, 02, ... 12)

    0 讨论(0)
  • 2020-12-22 05:38
    SELECT 
        'CAB'
        + RIGHT(YEAR(GetDate()),2)
        + RIGHT('0' + CONVERT(VARCHAR, MONTH(GetDate())),2)
        + Right('00000' + Cast(CurrentNo as varchar(10)), 5)
    

    That might work..

    0 讨论(0)
  • 2020-12-22 05:39

    Using your method \ logic..

    Right('0' + Cast(Month(GetDate()) as varchar(10)),2)
    
    0 讨论(0)
提交回复
热议问题