如何基于字符串中的月份名称(一月,二月,三月等)获取月份差异作为整数?
例如,我有一个变量declare @month varchar(20) = 'May'
;
并选择DATEDIFF(month, month(getdate()), @month)
为7(
十月和五月之间的差异)。
答案 0 :(得分:2)
在SQL Server中,您将使用datediff()
:
select datediff(month, '2010-01-15', '2011-02-28')
这将计算两个日期之间的月边界数。这听起来像是对您问题的合理解释。
答案 1 :(得分:2)
Declare @M1 varchar(25) = 'October'
Declare @M2 varchar(25) = 'May'
Select DateDiff(MONTH,@M1+' 01 1980',@M2+' 01 1980')
+case when DateDiff(MONTH,@M1+' 01 1980',@M2+' 01 1980')<0 then 12 else 0 end
返回7
答案 2 :(得分:1)
DECLARE @M1 as varchar(20) = 'October';
DECLARE @M2 as varchar(20) = 'May';
SELECT ABS(datediff(mm,
convert(datetime, @M1 + ' 1 2012 11:01AM', 100),
convert(datetime, @M2 + ' 1 2012 11:01AM', 100)
)
);
这不允许很多年了,您以前没有提到
答案 3 :(得分:1)
假设input1,input2,就可以了:
declare @input1 nvarchar(max)='October'
declare @input2 nvarchar(max)='May'
select
case
when DATEPART(MM,@input1+' 01 2011')>DATEPART(MM,@input2+' 01 2011')
then 12
else 0
end-DATEPART(MM,@input1+' 01 2011')+DATEPART(MM,@input2+' 01 2011')
答案 4 :(得分:0)
尝试
select DATEPART(MM,'march 01 2011') - DATEPART(MM,'january 01 2011')
答案 5 :(得分:0)
不确定您要做什么,但是类似的事情可能起作用:
select MONTH(STR_TO_DATE('October', '%M')) - MONTH(STR_TO_DATE('May', '%M')) -- returns 5
答案 6 :(得分:0)
尝试一下
class CustomWebviewClient : WebViewClient() {
private val charsetPattern = Pattern.compile(".*?charset=(.*?)(;.*)?$")
override fun shouldInterceptRequest(view: WebView, request: WebResourceRequest): WebResourceResponse? {
try {
val connection: HttpURLConnection = URL(request.url.toString()).openConnection() as HttpURLConnection
connection.requestMethod = request.method
for ((key, value) in request.requestHeaders) {
connection.addRequestProperty(key, value)
}
connection.addRequestProperty("custom header key", "custom header value")
var contentType: String? = connection.contentType
var charset: String? = null
if (contentType != null) {
// some content types may include charset => strip; e. g. "application/json; charset=utf-8"
val contentTypeTokenizer = StringTokenizer(contentType, ";")
val tokenizedContentType = contentTypeTokenizer.nextToken()
var capturedCharset: String? = connection.contentEncoding
if (capturedCharset == null) {
val charsetMatcher = charsetPattern.matcher(contentType)
if (charsetMatcher.find() && charsetMatcher.groupCount() > 0) {
capturedCharset = charsetMatcher.group(1)
}
}
if (capturedCharset != null && !capturedCharset.isEmpty()) {
charset = capturedCharset
}
contentType = tokenizedContentType
}
val status = connection.responseCode
var inputStream = if (status == HttpURLConnection.HTTP_OK) {
connection.inputStream
} else {
// error stream can sometimes be null even if status is different from HTTP_OK
// (e. g. in case of 404)
connection.errorStream ?: connection.inputStream
}
val headers = connection.headerFields
val contentEncodings = headers.get("Content-Encoding")
if (contentEncodings != null) {
for (header in contentEncodings) {
if (header.equals("gzip", true)) {
inputStream = GZIPInputStream(inputStream)
break
}
}
}
return WebResourceResponse(contentType, charset, status, connection.responseMessage, convertConnectionResponseToSingleValueMap(connection.headerFields), inputStream)
} catch (e: Exception) {
e.printStackTrace()
}
return super.shouldInterceptRequest(view, request)
}
private fun convertConnectionResponseToSingleValueMap(headerFields: Map<String, List<String>>): Map<String, String> {
val headers = HashMap<String, String>()
for ((key, value) in headerFields) {
when {
value.size == 1 -> headers[key] = value[0]
value.isEmpty() -> headers[key] = ""
else -> {
val builder = StringBuilder(value[0])
val separator = "; "
for (i in 1 until value.size) {
builder.append(separator)
builder.append(value[i])
}
headers[key] = builder.toString()
}
}
}
return headers
}
}
答案 7 :(得分:0)
我将获得所需月份(在您的情况下为5月)与当前月份之间的差额,如果差额小于0,则将其加上月数(12)进行计算,否则将无任何结果添加。这只是以上更好的答案之一
declare @RequiredMonth varchar(25) = 'February';
declare @difference as int;
set @difference = MONTH(cast(@RequiredMonth+'1 2016' as datetime)) - MONTH(getdate())
select case when @difference >= 0 then @difference else @difference + 12 end
答案 8 :(得分:0)
您可以使用类似这样的内容:
DECLARE
@month varchar(20) = 'May'
SELECT DATEDIFF(MONTH, GETDATE(), EndDate)
FROM (SELECT CAST(@month + CAST( YEAR(GETDATE()) + 1 AS VARCHAR(4)) AS DATE) EndDate) D