当前位置:网站首页>SQL get current time
SQL get current time
2022-06-25 05:30:00 【qq_ forty-one million five hundred and fifty-four thousand six 】
1. MySQL
1) MySQL Provided in NOW() function , Used to get the current date and time ,NOW() Sweat 、SYSDATE()、CURRENT_TIMESTAMP() Other aliases are as follows :
SELECT NOW(), SYSDATE(), CURRENT_TIMESTAMP
2) If you want to get the current date without the time part , You can use CURDATE() function ,CURDATE() Functions and CURRENT_DATE Equal alias . as follows :
SELECT CURDATE(), CURRENT_DATE
3) If you want to get the current time without the date part , You can use CURTIME() function ,CURTIME() Functions and CURRENT_TIME Equal alias
SELECT CURTIME(), CURRENT_TIME
2.Oracle
stay Oracle There is no function to get the current date and time in , But we can go to the system table DUAL Query in SYSTIMESTAMP To get the current timestamp . as follows :
SELECT SYSTIMESTAMP FROM DUAL
Again , We can go to the system table DUAL Query in SYSDATE To get the current date and time . as follows :
SELECT SYSDATE FROM DUAL
Again , stay Oracle There is no special provision in the to obtain the current date 、 Function of current time , But we can SYSDATE Value for processing , We need to rely on TO_CHAR() function , This function will be described in detail in the following chapters , Here we only introduce its application in date processing .
Use TO_CHAR( Time date value , 'YYYY-MM-DD') You can get the date part of the date time value , So the following SQL Statement to get the current date value :
SELECT TO_CHAR(SYSDATE, 'YYYY-MM-DD') FROM DUAL
Use TO_CHAR( Time date value ,'HH24:MI:SS') You can get the time part of the date time value , So the following SQL Statement to get the current time value :
SELECT TO_CHAR(SYSDATE, 'HH24:MI:SS') FROM DUAL
Compare the time size of string type
The time in the database is varchar Type of ,MySql Use CURDATE() To get the current date ,SqlServer adopt GETDATE() To get the current date
1. Use strings directly to compare
Be careful : Make sure that the two data types are exactly the same , Otherwise abnormal
such as A:"2016-09-01", If B The data is :"2016-9-2", Then there is no comparison
2. Through the type conversion function convert(),
Be careful : To ensure that the time value of the string is correct , Otherwise abnormal ,
such as “2016-2-30”,2 No, I haven't 30 Number , So the conversion will be abnormal
CONVERT (<data_ type>[ length ], <expression> [, style])
1)data_type by SQL Server System defined data types , User defined data types cannot be used here .
2)length Used to specify the length of the data , The default value is 30.
3) use CONVERT() Functional style Option to display the date and time in different formats .style Yes, it will DATATIME and SMALLDATETIME When data is converted to a string
Selected by SQL Server Conversion style number provided by the system , Different style numbers have different output formats . If you use this method to judge a field ,
Then the string format of the saved date field as long as it can make sql server Just complete the date conversion , It doesn't have to be as strict as the first method .
Still say adddate Field , For example, compare whether it is greater than the current date , You can write like this :where (CONVERT(varchar, adddate) >= CONVERT(varchar, GETDATE())) .
select convert(varchar(10),getdate(),120) -- Get current date
SELECT CONVERT(DATE,GETDATE(),110) -- Get current date
SELECT CONVERT(DATETIME,GETDATE(),110) -- Get the current time date
SELECT CONVERT(VARCHAR(50),GETDATE(),108) -- Get the current time
My application scenarios :
select * from jy_card where (SELECT CURDATE())< end_date and end_date is not null
————————————————
My application scenarios
select * from waf_ac_log where logintime >=to_char(trunc(next_day(sysdate-8,1)+1),'YYYY-MM-DD')
and logintime<=to_char(trunc(next_day(sysdate-8,1)+7),'YYYY-MM-DD') and userid='fc4a3e483f514abcb8ebd1355e46d2fb'
Order By logintime Desc;
oracle SQL Statement to get the data of this week, this month and this year
-- Take the data of this week
select * from table where DTIME >=trunc(next_day(sysdate-8,1)+1) and DTIME<=trunc(next_day(sysdate-8,1)+7)+1 ;
select * from table where DTIME >=trunc(next_day(sysdate-8,1)) and DTIME<=trunc(next_day(sysdate-8,1)+7);-- foreign
select * from table where DTIME >=TRUNC(SYSDATE, 'MM') and DTIME<=last_day(SYSDATE);
-- This month's
select * from table where to_char(DTIEM,'yyyy')=to_char(sysdate,'yyyy');
-- This year's
-- So what we take is On the day of the week , It starts on Sunday
select to_char(to_date('20130906','yyyymmdd'),'d') from dual;
-- result :6 notes :2013.09.06 It's Friday , It's the sixth day of the week
select to_char(sysdate+(2-to_char(sysdate,'d'))-7,'yyyymmdd') from dual;--- last Monday
select to_char(sysdate+(2-to_char(sysdate,'d'))-1,'yyyymmdd') from dual;--- Last Sunday
-- A simpler way to write , return date type
select trunc(sysdate,'iw') - 7 from dual;--- last Monday
select trunc(sysdate,'iw') - 1 from dual;-- Last Sunday
-- Take the last day of last month SELECT TO_CHAR(LAST_DAY(ADD_MONTHS(SYSDATE, -1)),'YYYYMMDD') FROM DUAL; -- Take the first day of last month SELECT TO_CHAR(LAST_DAY(ADD_MONTHS(SYSDATE, -2)) + 1,'YYYYMMDD') FROM DUAL;
Link to the original text :https://blog.csdn.net/weixin_39816740/article/details/80616844
边栏推荐
- JS verification code input number auto skip
- Jason learning
- About the definition of pointer variables (personal notes)
- 1.6.3 use tcpdump to observe DNS communication process
- Detailed summary of flex layout
- Semantic segmentation cvpr2019-advance: advantageous enterprise minimization for domain adaptation in semantic segmentation
- What happens when you use "-fno exceptions", "new T"- With “-fno-exceptions”, what happens with “new T”?
- First blog
- XSS (cross site script attack) summary (II)
- Even if you are not good at anything, you are growing a little bit [to your 2021 summary]
猜你喜欢

Summary of SQL injection (I)

TeeChart Pro ActiveX 2022.1
![[relax's law of life lying on the square] those poisonous chicken soup that seem to be too light and too heavy, but think carefully and fear](/img/12/d41f8d5abcb61d2632a8b117bf1604.jpg)
[relax's law of life lying on the square] those poisonous chicken soup that seem to be too light and too heavy, but think carefully and fear

Monkey test of APP automation

Activereportsjs V3.0 comes on stage

Svg code snippet of loading animation

2.20 learning content

JSON Library Tutorial from scratch (I): starting to learn and organize notes

Everything is an object

UVA816 Abbott’s Revenge
随机推荐
PHP calls map API
Double recursion in deep analysis merge sort
Array: force deduction dichotomy
In depth understanding of line height and vertical align
电子协会 C语言 1级 28 、字符菱形
Native JS high risk reminder pop-up code snippet, "are you sure you want to do this?" and "it cannot be recovered after deletion. Do you want to continue“
Edge loss interpretation
Example of dynamic programming 3 leetcode 55
Array introduction plus example 01
Database query optimization method
Object creation and invocation code example
2021-04-02
1.5.3 use tcpdump to observe ARP communication process
Large number operation (capable of square root, power, permutation and combination, logarithm and trigonometric value)
Dynamic programming Backpack - 01 Backpack
MySQL prevents Chinese garbled code and solves the problem of Chinese garbled code
Analysis of IM project framework
Edge loss 解读
3.2.3 use tcpdump to observe TCP header information (supplement common knowledge of TCP protocol)
Laravel Vonage SMS sending