-3

How to Extract Month and year from SYSDATETIME :

Pass from "2019-04-26 12:13:52.1683125" TO "201904"

Then Convert "201904" to INT.

kOe
  • 35
  • 6
  • Please provide evidence of what you have tried so far. – IronAces Apr 26 '19 at 09:12
  • 1
    Hello, please [take the tour](https://stackoverflow.com/tour) and read [how to ask](https://stackoverflow.com/help/how-to-ask) – TheWildHealer Apr 26 '19 at 09:13
  • 1
    Possible duplicate of [Convert getdate() to int](https://stackoverflow.com/questions/13571054/convert-getdate-to-int) – Udhay Titus Apr 26 '19 at 09:14
  • 1
    how is date represented as 201904?? – Nikhil S Apr 26 '19 at 09:21
  • I am using SQL SERVER. The date 201904 (YYYYMM) is what i want to have. i want to convert the current date of my system to YYYYMM then convert this to int. So i could compare it to a date that i have in my Database in this format – kOe Apr 26 '19 at 09:31
  • 1
    201904 is not a valid date. Are you storing those values in a char/varchar column? – jarlh Apr 26 '19 at 09:36
  • 1
    `SYSDATE` doesn't exist in SQL Server. There is `SYSDATETIME()`, however, that returns a `datetime2(7)`, not a `(var)char(6)`. Are you *sure* you're using SQL Server? As mentioned above, `201904` is not a `date`. That is either a integer or a `(var)char` value. A `date`, in SQL Server, holds data for the year, month **and** day. – Thom A Apr 26 '19 at 09:41
  • I am sorry it is SYSDATETIME(). I want to get the month and the year from the SYSDATETIME() and this result to convert it to INT so i can compare it to variables stored as INT – kOe Apr 26 '19 at 09:45
  • Fix your schema and do not store dates as strings or integers but use some date/time type. Then they're comparable too. – sticky bit Apr 26 '19 at 09:51

2 Answers2

0

If you are using SQL server as Database then below script should work for you

SELECT CAST(YEAR(getdate()) AS VARCHAR)+ RIGHT('00'+CAST(MONTH(getdate()) AS VARCHAR),2)
Jaydip Jadhav
  • 12,179
  • 6
  • 24
  • 40
0

If you want a numeric representation of the date as YYYYMM in SQL Server, I would recommend:

select year(getdate()) * 100 + month(getdate())

You can use sysdatetime() if you prefer.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786