convert(varchar(10), field name, conversion format) The
third parameter (time format) of the convert function under sybase
:
1.select user_id, convert(varchar(10),dayts,11) as dates from tb_user (under sybase And the statement under sql server)
2.select to_char(sysdate,'yy/mm/dd') as d from dual (oracle下语句)
Conversion format:
0 or 100 month dd yyyy hh:miampm
1 mm/dd/yy
2 yy.mm.dd
3 dd/mm/yy
4 dd.mm.yy
5 dd-mm-yy
6 dd month yy
7 month dd, yy
8 hh:mi:ss
9 or 109 month dd yyyy hh:mi:ss:mmmampm
10 mm-dd-yy
11 yy/mm/dd
12 yymmdd
also has 100, 101, 102... and the above corresponding, only But change yy to yyyy.
101 mm/dd/yyyy
102 yyyy.mm.dd
103 dd/mm/yyyy
104 dd.mm.yyyy
105 dd-mm-yyyy
106 dd month yyyy
107 month dd,yyyy
108 hh:mi :ss
110 mm-dd-yyyy
111 yyyy/mm/dd
112 yyyymmdd
You can also combine several formats:
select rtrim(convert(char,getdate(),111))+''+(convert(char,getdate(),108)) -- yyyy/mm/dd hh:mi:ss
convert(varchar(10), field name, conversion format)
cast(cad.id as varchar) // convert the id field of the cad table to a varchar field, where the id field is an integer For
example:
select user_id,convert(varchar(10 ),date,11) as date from tb_user
dateadd(date part, numeric expression, date)
select dateadd(dd,10,time) from testDate //Calculated in days, add 10 days to the current time
转换格式:
0或100 month dd yyyy hh:miampm
1 mm/dd/yy
2 yy.mm.dd
3 dd/mm/yy
4 dd.mm.yy
5 dd-mm-yy
6 dd month yy
7 month dd,yy
8 hh:mi:ss
9或109 month dd yyyy hh:mi:ss:mmmampm
10 mm-dd-yy
11 yy/mm/dd
12 yymmdd
101 mm/dd/yyyy
102 yyyy.mm.dd
103 dd/mm/yyyy
104 dd.mm.yyyy
105 dd-mm-yyyy
106 dd month yyyy
107 month dd,yyyy
108 hh:mi:ss
110 mm-dd-yyyy
111 yyyy/mm/dd
112 yyyymmd
Sybase time and date function
06:54 PM on Wednesday, May 9, 2010
Sybase date function
-------------------------------- ------------------------------------------------
The date function
getdate()
gets the current time and can be set to get various time formats.
datepart (date part, date)
takes a certain part of the specified time, year, month, day, hour, minute and second.
datediff (date part, date 1, date 2)
calculation What is the time difference between the specified date 1 and date 2.
dateadd (date part, numeric expression, date)
calculates the specified time, plus the length of time specified by the expression.
- take a certain part of the time
select datepart(yy,getdate()) --year
select datepart(mm,getdate()) --month
select datepart(dd,getdate()) --day
select datepart(hh,getdate()) --hour
select datepart(mi,getdate()) --min
select datepart(ss,getdate()) --sec
--Take the day of the week
set datefirst 1
select datepart(weekday,getdate()) --weekday
--String time
select getdate() -- '10/11/12'
select convert(char,getdate(),101) -- '09/27/2010'
select convert(char,getdate(),102) -- '2010.11.12'
select convert(char,getdate(),103) -- '27/09/2010'
select convert(char,getdate(),104) -- '27.09.2010'
select convert(char,getdate(),105) -- '27-09-2010'
select convert(char,getdate(),106) -- '27 Sep 2010'
select convert(char,getdate(),107) --'Sep 27, 2010'
select convert(char,getdate(),108) --'11:16:06'
select convert(char,getdate(),109) --'Sep 27 2010 11:16:28:746AM'
select convert(char,getdate(),110) --'09-27-2010'
select convert(char,getdate(),111) --'2010/09/27'
select convert(char,getdate(),112) --'20100927'
select rtrim(convert(char,getdate(),102))+' '+(convert(char,getdate(),108)) -- '2010.11.12 11:03:41'
In the sybase database, suppose the time period of table A is 2005-8-19 4:20,
but the result is "Aug 19 2005 4:20:04" after searching with the select statement.
How can I convert it to 2005-8? -9 4:20 this format.
2How does BeSybPro get the time format you need
select substring(convert(char(8),getdate(),112),1,4)+'-'+substring(convert(char(8),getdate(),112),5,2)+'-'+substring(convert(char(8),getdate(),112),7,2)+''+convert(char(5),getdate(),108)
select substring(convert(char(8),getdate(),112),1,4)+'-'+substring(convert(char(8),getdate(),112),5,2)+'-'+substring(convert(char(8),getdate(),112),7,2)+''+convert(char(8),getdate(),108)
--Integer time
select convert(int,convert(char(10),getdate(),112)) -- 20031112
select datepart(hh,getdate())*10000 + datepart(mi,getdate())*100 + datepart(ss,getdate()) -- 110646
--The time format "YYYY.MM.DD HH:MI:SS" is converted to "YYYYMMDDHHMISS"
declare @a datetime,@tmp varchar(20),@tmp1 varchar(20)
select @a=convert(datetime,'2004.08.03 12:12:12')
select @tmp=convert(char(10),@a,112)
select @tmp
select @tmp1=convert(char(10),datepart(hh,@a)*10000 + datepart(mi,@a)*100 + datepart(ss,@a))
select @tmp1
select @tmp=@tmp+@tmp1
select @tmp
--The last day of the month
declare
@tmpstr varchar(10)
@mm int,
@premm int,
@curmmlastday varchar(10)
begin
select @mm=datepart(month,getdate())--当月
select @premm=datepart(month,dateadd(month,-1,getdate())) --上个月
if (@mm>=1 and @mm<=8)
select @tmpstr=convert(char(4),datepart(year,getdate()))+'.0'+convert(char(1),datepart(month,dateadd(month,1,getdate())))+'.'+'01'
else if (@mm>=9 and @mm<=11)
select @tmpstr=convert(char(4),datepart(year,getdate()))+'.'+convert(char(2),datepart(month,dateadd(month,1,getdate())))+'.'+'01'
else
select @tmpstr=convert(char(4),datepart(year,dateadd(year,1,getdate())))+'.0'+convert(char(1),datepart(month,dateadd(month,1,getdate())))+'.'+'01'
select @curmmlastday=convert(char(10),dateadd(day,-1,@tmpstr),102) - the last day of the month
end
declare @st datetime,
@ft datetime
set @st = dateadd(dd,-day(dateadd(month,-1,getdate()))+1,dateadd(month,-1,getdate())), --上个月一号
@ft = dateadd(dd,-day(getdate())+1,getdate()) ---本月一号
select rtrim(convert(char,@st,111))+''+(convert(char,@st,108)) -- yyyy/mm/dd hh:mi:ss
select rtrim(convert(char,@ft,111))+''+(convert(char,@ft,108)) -- yyyy/mm/dd hh:mi:ss
--- Converted to data format 20091011181960
select cast(datepart(yy,getdate()) as varchar)+right('00'+cast(datepart(mm,getdate())as varchar),2)+right('00'+cast(datepart(dd,getdate())as varchar),2)+
2> right('00'+cast(datepart(hh,getdate())as varchar),2)+right('00'+cast(datepart(dd,getdate())as varchar),2)+right('00'+cast(datepart(ss,getdate())as varchar),2)
*************************
CONVERT function [Data type conversion]
--------------------------------------------------------------------------------
The function returns an expression converted to the provided data type.
语法 CONVERT ( data type, expression_r_r_r_r_r_r [ , format-style ] )
Parameter data type The data type to which the expression will be converted.
expression_r_r_r_r_r_r The expression to be converted.
format-style For converting a character string to a date or time data type and vice versa, format-style is a style code describing the date format string to be used. The value of the format-style parameter has the following meanings:
Without century (yy) With century (yyyy) Output
-0 or 100 Mmm dd yyyy hh:nn:ss:sss AM (or PM)
1 101 mm/dd/yy[yy]
2 102 [yy]yy.mm. dd
3 103 dd/mm/yy[yy]
4 104 dd.mm.yy[yy]
5 105 dd-mm-yy[yy]
6 106 dd Mmm yy[yy]
7 107 Mmm dd, yy[yy]
8 108 hh:nn:ss
-9 or 109 Mmm dd yyyy hh:nn:ss:sssAM (or PM)
10 110 mm-dd-yy[yy]
11 111 [yy]yy/mm/dd
12 112 [yy]yymmdd
13 113 dd Mmm yyy hh:nn:ss:sss (24-hour clock, European default time + milliseconds, 4-digit year)
14 114 hh:nn:ss:sss (24-hour clock)
20 120 yyyy-mm-dd hh :nn:ss:sss (24-hour clock, ODBC specification, 4-digit year)
21 121 yyyy-mm-dd hh:nn:ss.sss (24-hour clock, ODBC specification plus milliseconds, 4-digit year)
ps: In some conversion types of convert, different results are output in sql service and sybase.