当前位置:网站首页>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

原网站

版权声明
本文为[qq_ forty-one million five hundred and fifty-four thousand six ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202210507362381.html