SqlServer查询根据获取的时间范围,获取范围内及包含的取值时间
在 C# 中,若要获取从 2025-02-01 到 2025-04-01 之间包含起始和结束月份的所有月份,可以借助 DateTime 类型以及循环来实现。以下是具体的实现代码:
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
DateTime startDate = new DateTime(2025, 2, 1);
DateTime endDate = new DateTime(2025, 4, 1);
List<string> months = new List<string>();
// 从起始日期开始,逐月递增,直到超过结束日期
for (DateTime currentDate = startDate; currentDate <= endDate; currentDate = currentDate.AddMonths(1))
{
// 将当前月份添加到列表中,格式为 "yyyy-MM"
months.Add(currentDate.ToString("yyyy-MM"));
}
// 输出结果
foreach (string month in months)
{
Console.WriteLine(month);
}
}
}
代码解释:
- 定义起始和结束日期:使用 DateTime 构造函数分别创建起始日期 startDate(2025 年 2 月 1 日)和结束日期 endDate(2025 年 4 月 1 日)。
- 创建存储月份的列表:使用 List 来存储获取到的月份字符串。
- 循环遍历月份:使用 for 循环从起始日期开始,通过 AddMonths(1) 方法逐月递增当前日期,直到当前日期超过结束日期为止。在每次循环中,将当前日期按照 “yyyy-MM” 格式转换为字符串,并添加到 months 列表中。
- 输出结果:使用 foreach 循环遍历 months 列表,并将每个月份字符串输出到控制台。
实战操作
public override PageGridData<V_SB_UPLEEPSCHEME> GetPageData(PageDataOptions options)
{
string[] a = { "1", "4", "7", "10" };
string[] b = { "2", "5", "8", "11" };
string[] c = { "3", "6", "9", "12" };
var d = "0";
var e = "0";
var item1 = "";
var year = "";
string output = "";
var DateList = "";
var f = DateTime.Now.ToString("yyyy-MM");
List<string> months = new List<string>();
List<string> FYEARS = new List<string>();
List<string> QUARTERS = new List<string>();
JArray jo = (JArray)JsonConvert.DeserializeObject(options.Wheres);
if (jo.Count() != 0 && !jo[0]["value"].ToString().IsNullOrEmpty() && jo[0]["value"].ToString() != "")
{
DateTime startDate = DateTime.Parse(jo[0]["value"].ToString());
DateTime endDate = DateTime.Parse(jo[1]["value"].ToString());
// 从起始日期开始,逐月递增,直到超过结束日期
for (DateTime currentDate = DateTime.Parse(startDate.ToString("yyyy-MM")); currentDate <= endDate; currentDate = currentDate.AddMonths(1))
{
// 将当前月份添加到列表中,格式为 "yyyy-MM"
months.Add(currentDate.ToString("yyyy-MM"));
}
// 输出结果
foreach (string month in months)
{
e = DateTime.Parse(month).Month.ToString();
f = DateTime.Parse(month.ToString()).ToString("yyyy-MM");
if (a.IndexOf(e) != -1)
{
d = "1";
}
else if (b.IndexOf(e) != -1)
{
d = "2";
}
else
{
d = "3";
}
QUARTERS.Add(d);
FYEARS.Add(e);
DateList += month + ',';
Console.WriteLine(month);
}
DateList = DateList.Substring(0, DateList.ToString().Length - 1);//日期
string[] parts = DateList.Split(',');
// 处理每个分割后的元素,添加单引号
for (int i = 0; i < parts.Length; i++)
{
parts[i] = $"'{parts[i]}'";
}
// 使用逗号连接处理后的元素
output = string.Join(",", parts);
Console.WriteLine(output);
foreach (var years in FYEARS) //年
{
year += years + ',';
}
year = year.Substring(0, year.ToString().Length - 1);
foreach (var item in QUARTERS) //季度
{
item1 += item + ',';
}
item1 = item1.Substring(0, item1.ToString().Length - 1);
Console.WriteLine(QUARTERS);
//f = DateTime.Parse(jo[0]["value"].ToString()).ToString("yyyy-MM");
jo.Clear();
options.Wheres = jo.ToString();
}
QuerySql = $@"SELECT * from V_SB_UPLEEPSCHEME WHERE FID IS NOT NULL ";
if (d != "0")
{
QuerySql += $@" AND ( CONVERT (varchar (7),FDATE, 120) in ({output}) OR QUARTER in ({item1}) or FYEAR in ({year}) )";
}
return base.GetPageData(options);
}
附带内容
‘2025-03,2025-04’ 如何转成 ‘2025-03’,‘2025-04’,
在 C# 中,要将 “2025-03,2025-04” 这样的字符串转换成 ‘2025-03’,‘2025-04’ 格式,可以按以下步骤进行:
- 使用 Split 方法将原字符串按逗号分割成字符串数组。
- 对数组中的每个元素添加单引号。
- 使用 string.Join 方法将处理后的元素用逗号重新连接成一个新的字符串。
using System;
class Program
{
static void Main()
{
string input = "2025-03,2025-04";
// 按逗号分割字符串
string[] parts = input.Split(',');
// 处理每个分割后的元素,添加单引号
for (int i = 0; i < parts.Length; i++)
{
parts[i] = $"'{parts[i]}'";
}
// 使用逗号连接处理后的元素
string output = string.Join(",", parts);
Console.WriteLine(output);
}
}
代码解释:
- 定义输入字符串:定义了需要处理的原始字符串 input。
- 分割字符串:使用 Split(‘,’) 方法将 input 按逗号分割成一个字符串数组 parts。
- 添加单引号:通过 for 循环遍历 parts 数组,使用字符串插值 $“‘{parts[i]}’” 为每个元素添加单引号。
- 重新连接字符串:使用 string.Join(“,”, parts) 方法将处理后的元素用逗号连接成一个新的字符串 output。