简体   繁体   English

sql查询选择每月的一天

[英]sql query to Select Day of month

I want a query where the output should be only day of month for.eg Consider today's date(04-07-2016).The output should be "04" 我想要一个查询,其中输出应仅是一个月的一天。例如,考虑今天的日期(04-07-2016)。输出应为“ 04”

I have been trying with multiple queries which returns complete date.Whereas I need simply date(01-30). 我一直在尝试返回完整日期的多个查询,而我只需要date(01-30)。 please guide for the same. 请同样指导。

For SQL-Server (using a DateTime column): 对于SQL Server(使用DateTime列):

select datepart(day, datecolumn)

For SQL-Server (using today): 对于SQL Server(今天使用):

select datepart(day, getdate())

If you want a 2 digit date ( 04 and not just 4 ), then use this: 如果您想要一个两位数的日期( 04而不仅仅是4 ),请使用以下命令:

select right('0' + rtrim(day(getdate())), 2); 

Try the followig 试试Followig

  SELECT datepart(dd, GETDATE())

It will helps to format the date also. 它还将有助于格式化日期。 if you want left pad with zero for single digit then try the following query 如果您希望左键盘零个位数,请尝试以下查询

  SELECT RIGHT('0'+ convert(varchar,datepart(DD, GETDATE())) ,2)

You can use the below query for today, 您今天可以使用以下查询,

SELECT RIGHT('00'+CAST (datepart(DAY, getdate()) AS VARCHAR(2)),2)

For a column in DB, 对于数据库中的列,

SELECT RIGHT('00'+CAST (datepart(DAY, <<YourColumn>>) AS VARCHAR(2)),2) From <<YourTable>>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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