我一直试图将日期和时间合并到datetime中。我的日期是varchar,我需要有一列datetime(作为datetime)
我的输入是这样的:
create table t1 (
date1 varchar (12),
time1 varchar (39));
INSERT INTO t1 VALUES
('01.Mar.2019', '11:30'),
('02.Mar.2019', '1:30'),
('03.Mar.2019', '0:30'),
('03.Mar.2019', '10:30');
预期结果如下:
datetime1
01.Mar.2019 11:30:00
02.Mar.2019 01:30:00
03.Mar.2019 00:30:00
03.Mar.2019 10:30:00
答案 0 :(得分:1)
SELECT STR_TO_DATE(CONCAT(date1,' ',time1),'%d.%b.%Y %H:%i') x FROM t1;
+---------------------+
| x |
+---------------------+
| 2019-03-01 11:30:00 |
| 2019-03-02 01:30:00 |
| 2019-03-03 00:30:00 |
| 2019-03-03 10:30:00 |
+---------------------+
4 rows in set (0.00 sec)
答案 1 :(得分:1)
尽管我认为您存储这些值的方法不是最佳解决方案,但是您可以使用class ViewMaskQRCode(
context: Context,
text: String,
colorMask: String,
alphaMask: Int,
typeface: Typeface,
sizeText: Int
) : View(context) {
private var mFramingRect: Rect? = null
private lateinit var maskBackground: Paint
private var textBottom: String? = text
private var mTypeface: Typeface? = typeface
private var mSizeText: Int = sizeText
init {
setupViewMask(colorMask, alphaMask)
}
private fun setupViewMask(colorMask: String, alphaMask: Int) {
maskBackground = Paint()
maskBackground.color = Color.parseColor(colorMask)
maskBackground.alpha = alphaMask
calculateSize()
invalidate()
}
private fun calculateSize() {
val viewResolution = Point(width, height)
var width: Int
width = (getWidth() * 0.65).toInt()
if (width > getWidth()) {
width = getWidth() - 50
}
val leftOffset = (viewResolution.x - width) / 2
val topOffset = (viewResolution.y - width) / 5
mFramingRect =
Rect(leftOffset, topOffset, leftOffset + width, topOffset + width)
}
override fun onSizeChanged(xNew: Int, yNew: Int, xOld: Int, yOld: Int) {
calculateSize()
}
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
drawViewMask(canvas)
if (textBottom != null && !textBottom!!.isEmpty())
drawViewText(canvas)
}
private fun drawViewMask(canvas: Canvas) {
val width = canvas.width
val height = canvas.height
val framingRect = mFramingRect
canvas.drawRoundRect(RectF(
0F,
0F,
width.toFloat(),
framingRect!!.top.toFloat()),
0F, 0F,
maskBackground
)
canvas.drawRoundRect(RectF(
0F,
framingRect.top.toFloat(),
framingRect.left.toFloat(),
(framingRect.bottom + 1).toFloat()),
0F, 0F,
maskBackground
)
canvas.drawRoundRect(RectF(
(framingRect.right + 1).toFloat(),
framingRect.top.toFloat(),
width.toFloat(),
(framingRect.bottom + 1).toFloat()),
0F, 0F,
maskBackground
)
canvas.drawRoundRect(RectF(
0F,
(framingRect.bottom + 1).toFloat(),
width.toFloat(),
height.toFloat()),
0F, 0F,
maskBackground
)
}
@Suppress("DEPRECATION")
private fun drawViewText(canvas: Canvas) {
val framingRect = mFramingRect
val offsetTop = 90
val padding = 100
val paint = TextPaint()
paint.style = Paint.Style.FILL
paint.color = Color.WHITE
paint.textSize = mSizeText.toFloat()
paint.isFakeBoldText = true
paint.typeface = mTypeface
val width = canvas.width - padding
val textLayout = StaticLayout(
textBottom,
paint,
width,
Layout.Alignment.ALIGN_CENTER,
1.0f,
0.0f,
false)
canvas.save()
canvas.translate(
(framingRect!!.centerX() - width / 2).toFloat(),
(framingRect.bottom + offsetTop).toFloat())
textLayout.draw(canvas)
}
}
来加入单独的{{ 1}}在一起,然后DATETIME
将字符串转换为CONCAT
:
VARCHAR
这分为两部分:
STR_TO_DATE
将日期和时间以及一个空格连接到一个看起来像这样的单个字符串中:
DATETIME
然后,SELECT STR_TO_DATE(CONCAT(date1, ' ', time1), '%d.%b.%Y %k:%i') FROM t1;
使用格式说明符将其转换为CONCAT(date1, ' ', time1)
:
01.Mar.2019 11:30
02.Mar.2019 1:30
03.Mar.2019 0:30
03.Mar.2019 10:30
因此,格式为:
%d-月份中的一天,如果是1(01、02、03等)数字,则前导零。
%b-三个字符的月份缩写名称(1月,2月,3月等)
%Y-四位数的年份(2018、2019等)
%k-24小时格式的小时,前导零(0、1、2等)
%i-分钟前导零(00、01、02,依此类推)
此输出将是:
STR_TO_DATE
我只想补充一点,我认为在数据库中存储日期和时间信息的最佳方法几乎肯定是使用DATETIME
列。