Sybase timestamp converting :
Please suggest me some nice document for Sybase Date and timestamp converting.
Example :
Converting date format
Coveting short date to long date
Using below query, I need some information for conversion number :
select CONVERT( VARCHAR( 20 ), av.logDatetime , 112 ) + ":"+ CONVERT( VARCHAR( 20 ), av.logDatetime , 108 )
from dbo.ProcessLog av
Please suggest me some doc,Thanks!
Go to http://infocenter.sybase.com/help/index.jsp you will find the documentation for every sybase product.
Related
Below is a script i am trying to run in Presto; Subtracting today's date from an integer field I am attempting to convert to date. To get the exacts days between. Unfortunately, it seems the highlighted block does not always convert the date correctly and my final answer is not correct. Please does anyone know another way around this or a standard method on presto of converting integer values to date.
Interger value in the column is in the format '20191123' for year-month-date
select ms, activ_dt, current_date, date_diff('day',act_dt,current_date) from
(
select ms,activ_dt, **CAST(parse_datetime(CAST(activ_dt AS varchar), 'YYYYMMDD') AS date) as act_dt**, nov19
from h.A_Subs_1 where msisdn_key=23480320012
) limit 19
You can convert "date as a number" (eg. 20180527 for May 27, 2018) using the following:
cast to varchar
parse_datetime with appropriate format
cast to date (since parse_datetime returns a timestamp)
Example:
presto> SELECT CAST(parse_datetime(CAST(20180527 AS varchar), 'yyyyMMdd') AS date);
_col0
------------
2018-05-27
You can use below sample query for your requirement:
select date_diff('day', date_parse('20191209', '%Y%m%d'), current_timestamp);
Using SQL Server 2008 R2 I am trying to compare year values in a where clause like so:
AND year(convert(date, (LEFT(formAuditLog, 10)), 104 )) = year(GETDATE())
But I get this error:
Conversion failed when converting date and/or time from character string
I have also tried this but get that same result:
AND cast(year(convert(date, (LEFT(formAuditLog, 10)), 104 )) as int) = cast(year(GETDATE()) AS INT)
Note - formAuditLog is a string. I am getting the first 10 chars which is always mm/dd/yyyy (I have triple checked this) so I convert as a date and then get the year of this. Based on this info, should I not be able to do this comparison?
Thanks in advance
Added :
Note - When I put this in a select it works. I get the year part of the date I am expecting:
select top 10 year(convert(date, (LEFT(formAuditLog, 10)), 104 )) , * from myTableName
So why would this be allowed in a select but fail in a where clause?
Added : For completeness, there is actually nothing wrong with either of the two SQL bits at the top of this post. As mentioned in the accepted post comment below, it was a leading quote in one formAuditLog records that cause it to fall over
If your formAuditLog column is a string like you suggest and the left 10 characters is equivalent to a date format MM/DD/YYYY. This should have worked for you:
create table mytableName (id int, formAuditLog varchar(50));
insert into mytableName values
(1,'01/02/20141203'),
(2,'01/03/20151203'),
(3,'05/01/20151203');
select
id,
formAuditLog,
cast(left(formAuditLog,10) as date) as DateField,
year(cast(left(formAuditLog,10) as date)) as yearfield
from mytableName
where year(cast(left(formAuditLog,10) as date)) = year(cast(getdate() as date));
Code Demo
for sql server 2008, this should be your where condition
and year(GETDATE()) = case when isdate(LEFT(formAuditLog, 10))=1 then year(convert(date, (LEFT(formAuditLog, 10)))) else 99999 end
For sql server 2012..
AND year( try_parse(LEFT(formAuditLog, 10) as date) ) = year(GETDATE())
I'm new to crystal report. I have a date in string format like 2015-03-25 (Wed) and I want to convert it to date format like 03/25/2015. I tried with CDate and DateValue but it returned bad date string format. Any suggestions to convert such date string to proper date format?
If you have a DateTime field in Crystal Reports, you will see Date and Time tab option on the Format Editor when you right click on the field and select Format Field menu item. From the Date and Time tab, you may select the desired format and select OK.
It would be recommended to use the formats you want to use.
For eg : if you are giving string format for money or decimal you may not be able to use it at its full,like you may not be able to auto sum and other properties related to the datatype you intend to use
Not to do any thing in the code, Crystal Report have facility to this type of simple format.
#utility, you are near to answer.
As above image, in last Custom Format option, where you just go in Date tab and give format as
http://www.c-sharpcorner.com/UploadFile/mahesh/DateFormatInCR06132007092248AM/DateFormatInCR.aspx
Updated : sorry for above answer, that will work if you have valid date string.
In your case, where any arbitrary string need to convert into other date format. There is 2 option. In both case you have to extract the date and then format as you need and again combined with other sub-string.
Second you already done ie. crsytal report side, grab the date , format it and concatenate. this will slow down as need to process for each row.
SqlServer side - This option is faster from first option.
declare #t nvarchar(16) = '2015-03-25 (Wed)'
--get the acual date select SUBSTRING ( #t, 1, charindex('(' , #t ) -1 )
--above result give the charter datatype, so you first convert into date and then convert into other format select cast( SUBSTRING ( #t,
1, charindex('(' , #t ) -1 ) as date) --convert into date select
convert (varchar(15) , cast( SUBSTRING ( #t, 1, charindex('(' , #t )
-1 ) as date) , 103) --convert into dd/mm/yyyy format
--Above is for your understand, this is the actual execution of your code (Only write the below line) select convert (varchar(15) , cast(
SUBSTRING ( #t, 1, charindex('(' , #t ) -1 ) as date) , 103) + ' ' +
datename(dw, getdate() )
I suggest, go with Sqlserver side.
I am trying to port data from a flat file to TD via BTEQ.
The table definition is :
CREATE MULTISET TABLE _module_execution_log
(
system_id INTEGER,
process_id INTEGER,
module_id INTEGER,
julian_dt INTEGER,
referral_dt DATE FORMAT 'YYYY-MM-DD',
start_dt_tm TIMESTAMP(6),
end_dt_tm TIMESTAMP(6),
ref_s_cnt INTEGER,
ref_d_cnt INTEGER)
PRIMARY INDEX ( module_id );
Following are 2 sample records that i am trying to load in the table :1|1|30|2007073|Mar 14 2007 12:00:00:000AM|Mar 15 2007 1:27:00:000PM|Mar 15 2007 1:41:08:686PM|0|0
1|1|26|2007073|Mar 14 2007 12:00:00:000AM|Mar 15 2007 1:27:00:000PM|Mar 15 2007 1:59:40:620PM|0|0
Snippet for my BTEQ script
USING
( system_id INTEGER
,process_id INTEGER
,module_id INTEGER
,julian_dt INTEGER
,referral_dt DATE FORMAT 'YYYY-MM-DD'
,start_dt_tm TIMESTAMP
,end_dt_tm TIMESTAMP
,ref_s_cnt INTEGER
,ref_d_cnt INTEGER
)
INSERT INTO _module_execution_log
( system_id
,process_id
,module_id
,julian_dt
,referral_dt
,start_dt_tm
,end_dt_tm
,ref_s_cnt
,ref_d_cnt
)
VALUES (
:system_id
,:process_id
,:module_id
,:julian_dt
,:referral_dt
,:start_dt_tm
,:end_dt_tm
,:ref_s_cnt
,:ref_d_cnt);
I get the following error during import :
*** Failure 2665 Invalid date.
Statement# 1, Info =5
*** Failure 2665 Invalid date.
Statement# 1, Info =5
The issue is surely with the exported date in 5th column. I cannot modify the export query. I tried the following in the bteq but still failed : cast(cast(substr(:referral_dt,1,11) as date format 'MMMBDDBYYYY') as date format 'YYYY-MM-DD')
Your data is pipe-delimited variable length characters and the USING should match the input data, e.g.
system_id VARCHAR(11)
referral_dt VARCHAR(26)
The VarChars will be automatically casted to the target datatypes using a default format. For your Timestamps you need to cast manually adding a format:
referral_dt (TIMESTAMP(3),FORMAT 'mmmBddByyyyBhh:mi:ss.s(3)T')
But this will fail for a single digit hour, Teradata always wants two digits.
If you're on TD14 you better utilize the Oracle TO_DATE/TO_TIMESTAMP UDFs which allow single digit hours:
TO_TIMESTAMP(referral_dt,'MON DD YYYY HH:MI:SS:FF3AM')
You do not have a date indicated by your data.
First 4 values expected are integer, then a date, then a timestamp:
system_id INTEGER,
process_id INTEGER,
module_id INTEGER,
julian_dt INTEGER,
**referral_dt DATE FORMAT 'YYYY-MM-DD'**,
start_dt_tm TIMESTAMP(6), ...
Your data doesn't match:
1|1|30|2007073|Mar 14 2007 12:00:00:000AM|Mar 15 2007 1:27:00:000PM|Mar 15 2007 1:41:08:686PM|0|0
you are missing the date:
1|1|30|2007073|**????-??-??**| Mar 14 2007 12:00:00:000AM|...
Is there any way that I can convert the DateTime SQL value of this:
2011-10-11 00:00:00.000
to a string formatted exactly the same?
'2011-10-11 00:00:00.000'
I tried doing cast(fl_from_dt as varchar(50) but it converts it to something readable - ex. 'Oct 11 2011 12:00AM'
Are you using SqlServer?
Take a look at the style parameter of CONVERT() here: http://msdn.microsoft.com/en-us/library/ms187928.aspx - in particular have a look at the ODBC canonical (with milliseconds) format.
For short you should use style 121 in the CONVERT() command.
Style: 121
ODBC canonical (with milliseconds) default for time, date, datetime2, and datetimeoffset
yyyy-mm-dd hh:mi:ss.mmm(24h)
Try this one !
select CONVERT(VARCHAR(10), GETDATE(), 112)
+ right('0'+cast(datepart(hh, GETDATE()) as varchar(2)),2)
+ right('0'+cast(datepart(mi, GETDATE()) as varchar(2)),2)
+ right('0'+cast(datepart(ss, GETDATE()) as varchar(2)),2) as 'DateTime_STR2'