根据月份从sql server获取数据 [英] Getting data from sql server based on month

查看:100
本文介绍了根据月份从sql server获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hai这是我的sql server表..请你知道。



Articleid(int) ArticleTitle(varchar) CreatedDate(datetime)



1 Test1 2013-09-06 16:18:48.107



2 Test2 2013-10-06 16:34:50.000









Hai这是我的桌子...在这里我想找出基于月份的文章标题...



任何人都可以帮我编写这个存储过程的方法。

Hai this is my sql server table.. Please chek it.

Articleid(int) ArticleTitle(varchar) CreatedDate(datetime)

1 Test1 2013-09-06 16:18:48.107

2 Test2 2013-10-06 16:34:50.000




Hai this is my table... Here i want to find out the Article Titles Based on the month...

Can any one help me how to write the stored procedure for this one.

推荐答案

你可以使用 MONTH功能



这样的东西应该有效:

You could use the MSSQL server built in MONTH Function.

Something like this should work:
SELECT * FROM TableName WHERE MONTH(CreatedDate) = 10;





或者如果你想根据年和月查询:





or if you want to query based on year and month:

SELECT * FROM TableName WHERE YEAR(CreatedDate) = 2013 AND MONTH(CreatedDate) = 10;





...希望它有所帮助。





要通过指定月份名称进行查询(与按月号查询相比,这不是一个有效的解决方案),请查看内置数字功能



这样的事可以让你入门:



... hope it helps.


To query by specifying a month name, (which is not as an efficient solution compared with querying by month number), have a look at the MSSQL server built in DATENAME Function.

Something like this should get you started:

SELECT * FROM TableName WHERE DATENAME(month, CreatedDate) = 'october';


declare @month varchar(50)
set @month = 'january';

set @month = @month + ' 01 2000'; -- dummy line (don't bother about 01 2000 )
select  @month = DATEPART(MM,@month)
set @month = RIGHT('0' + @month,2)
print @month
SELECT * FROM TableName  WHERE ArticleTitle LIKE '%-' +@month + '-%';

这篇关于根据月份从sql server获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆