Is there a way to search the SQL query with month numbers instead of month name?

I am trying to use an actual numeric month value for a sql query to output the results. Is there a way to do this without having a function to change the numbers to the actual names of the months and then to numbers up to the month? The following code works for names, what works for numbers?

datename (month, (convert (DateTime, DTSTAMP))) = 'October'

+1


source to share


4 answers


month, (convert (datetime, DTSTAMP)) should do this, but why, in fact, are you not storing the data as datetime correctly? Anything that adds conversion stuff for using dates adds unnecessary load to your server and slows down your application.



+1


source


Datepart is an alternative to the month command and is more flexible as you can extract other parts of a date.



DATEPART(mm, convert(datetime,DTSTAMP))

      

0


source


Gets the month Number of days

CONVERT (VARCHAR (2), DTSTAMP, 110)

0


source


here you can search with diff options like:

DATE_SUB (CURDATE (), INTERVAL 0 DAY)

DATE_SUB (CURDATE (), INTERVAL 1 MONTH)

DATE_SUB (CURDATE (), INTERVAL 1 YEAR)

as per your requirement.

0


source







All Articles