简体   繁体   中英

How To Select SQL Server Records Based On A Specific Month From Custom-Formated-Date Column

I am working with SQL Server 2008 and I want to show some records based on a specific month within the date field from the target database table in my Windows Form Application. I have used a custom format (dd-MM-yyyy) for my date field and the datatype of storing date is nvarchar while inserting the record into the database.

This is how my database table looks like:

PaymentID    PaymentAmount       Date          Comments
      1        30000             27-05-2020    Some Comments
      2        45000             22-06-2020    Some Comments
      3        65000             29-06-2020    Some Comments
      4        33000             30-07-2020    Some Comments
      5        22000             30-07-2020    Some Comments

For record selection, I have passed the specific month, say June and I want to check if any of payment record is saved in the month of June 2020. I want to show this record in a datagridview.

Is there any way for getting the required Month from the nvarchar date format? Any help will be highly appreciated.

Don't store dates as strings: This is bad practice for many reasons. efficiency and data integrity immediately come to mind. Your first effort should go into converting your column to the date datetype. Something like this should work:

update mytable set [date] = try_convert(date, 105);
alter table mytable alter column [date] date;

The first statement attempts to convert each value to a date , and re-assigns the result to the string column; SQL Server will translate back to a string according to the server's regional settings. Invalid values are turned to null s. You can then safely change the column's datatype.

You can then easily filter the table for June 2020 like so:

select *
from mytable
where [date] >= '20200701' and [date] < '20200801'

If you are stucked with you current string datatype, and you can't change it for some reason, then you can use string functions. For your current format, I would recommend:

where [date] like '%-07-2020'

You're so close. The month() function requires an integer. The datename() function will use a string. Take a look at this;

https://sqlhints.com/2015/06/07/how-to-get-month-name-from-date-in-sql-server/

Since your data type is nvarchar you'll need to use the substr() function in a select to parse out the month and year. If you want to search using numeric values you'll need to convert the substrings to int otherwise you can compare the substrings to string values eg

cast(substr(date, 4, 2)) = 7

OR

substr(date, 4, 2) = '07'

But really, if you have the option, as Dan says, you should change the data type of your date column to a date. SQL server has a lot of useful functionality for working with dates that you can't really take advantage of if your date is in an nvarchar

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM