且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Sql Server中的月相关查询

更新时间:2023-02-26 20:30:29

添加DATENAME(月,DOB)AS''出生月''并按月(DOB)(月份)进行订购返回一个数字,datename会按字母顺序给你几个月)


使用它:



  SELECT  * 
FROM tbl_userwritereviews
ORDER BY DATEPART(mm,日期


您好,



请尝试以下方法,>


要进行排序,请使用以下内容,而不是每年使用以下内容,



选择CONVERT (varchar(11),d.dob,106),DATENAME(MONTH,d.dob),DATEPART(MONTH,d.dob)来自DATEPART(MONTH,d.dob)asc的驱动程序d 





按年份,日期和月份排序使用以下内容,



从d.dob asc 


i have one column in database that is DOB(Date of Birth)

in that database all employee details of DOB has inserted records are there.

DOB(Date of birth format as mm/dd/yyyy)

Example as follows;

DOB( IN Database DOB has name like that) Format as mm/dd/yyyy)

9/16/1945
4/10/1952
6/30/1978
4/19/1981
4/5/44
4/11/1948
3/12/77
12/13/1985
6/14/1974
1/3/1944
8/7/1950
11/5/1977
8/11/1974
2/16/1941
7/23/1944
7/23/1968
3/7/1974
6/27/1945
11/3/1952
4/10/1976
3/29/1987
11/2/1956
12/14/1987
5/29/1989
9/24/1973
10/26/1978
9/25/2001
5/1/1984
8/4/1977
10/21/2008

i want the output as MONTH to be arranged in Ascending Order All Months.
From January to Decmeber in Ascending Order.

for that how to write the query.

"select Name, CONVERT(varchar(11), DOB, 106)  AS Date_of_birth,Mobileno as Mobile_Number, CONVERT(varchar(11), Weddingday, 106) AS Wedding_Day,EMail as E_Mail from BirthDayWish where Active = 'A' ORDER BY Name";



from the above query how to write the Month to be Displayed in ascending order.

add DATENAME(month, DOB) AS ''Birth Month'' and they do an order by month(DOB) ( month returns a number, datename would give you months alphabetically )


Use This:

SELECT  *
FROM         tbl_userwritereviews
ORDER BY DATEPART(mm, date)


Hi,

Try the following,

To sort, Instead of year only by month use the following,

Select CONVERT(varchar(11),d.dob,106), DATENAME(MONTH,d.dob), DATEPART(MONTH,d.dob) from driver d order by DATEPART(MONTH,d.dob) asc



To sort by year, date and month use the following,

Select CONVERT(varchar(11),d.dob,106), DATENAME(MONTH,d.dob), DATEPART(MONTH,d.dob) from driver d order by d.dob asc