Cast and convert
Hi Team,
could you please tell me where do we use cast and convert functions.
What is the main difference between these two with examples.
Thanks in advance.
Mamatha
I do remember using CAST when creating views with NULL values as columns (For some purpose it was required).
If you dont use CAST when used null as column, the view column will be created as VARCHAR2(0). This will be an issue in some reporting tools.
So using CATS you can change the data type to NUMBER or VARCHAR(50) (Example)
SQL> create or replace view v1 as select null as col1 from dual;
View created.
SQL> desc v1
Name Null? Type
COL1 VARCHAR2
SQL> create or replace view v1 as select cast(null as varchar2(50)) as col1 from dual;
View created.
SQL> desc v1
Name Null? Type
COL1 VARCHAR2(50)CAST is also used with MULTISET, when working with OBJECT TYPEs..
Read Here
CONVERT is used when you want to see a particular text or column in a diffrent character set than the stored one. I have never used it , though...
Edited by: jeneesh on Oct 12, 2012 6:05 PM
Similar Messages
-
Error when using cast and convert to datetime
I run a stored procedure
usp_ABC
as
begin
delete from #temp1
where #temp1.ResID not in
( select Col1
from TableA
where ( @I_vId = -1 or Doc_Field_ID = @I_vId)
and ColA1 = 2 and FieldValue !=''
and cast ( FieldValue as DATETIME ) = cast (@strvalue_index as DATETIME)
and ResID = #temp1.ResID
and TypesId = @I_vTypeId
end
But return message "Conversion failed when converting date and/or time from character string.'
Format of Column FieldValue is nvarchar(400)
@strvalue_index = '05/05/2013'
So I see in article http://technet.microsoft.com/en-us/library/ms174450.aspx MS say about MS SQL Server and SQL Server Compact.
Then I re-write proc:
delete from #temp1
where #temp1.ResID not in
( select Col1
from TableA
where ( @I_vId = -1 or Doc_Field_ID = @I_vId)
and ColA1 = 2 and FieldValue !=''
and cast ( cast(FieldValue as nvarchar(200)) as DATETIME ) = cast (@strvalue_index as DATETIME)
and ResID = #temp1.ResID
and TypesId = @I_vTypeId
so stored run success.
I don't understand how?
When i run only statement in MS SQL Studio Management, the statement run success.
Thanks all,No bad dates in my data.
Apparently you have. Or, as Johnny Bell pointed out, there is a clash with date formats. Did you try the query with isdate()?
For SQL 2008, you need
CASE WHEN isdate(FieldValue) = 1
THEN CAST (FieldValue AS datetime)
END =
CASE WHEN isdate(@strvalue_index) = 1
THEN CAST (@strvalue_index AS datetime)
END
Erland Sommarskog, SQL Server MVP, [email protected]
I think date formats is OK. So when I re-write stored:
and cast ( FieldValue as DATETIME ) = cast (@strvalue_index as DATETIME)
=>
and cast ( cast(FieldValue as varchar(200)) as DATETIME ) = cast (@strvalue_index as DATETIME)
the stored procedure will run success. I see in http://technet.microsoft.com/en-us/library/ms174450.aspx MS
has an IMPORTANT:
Important
When using CAST or CONVERT for nchar, nvarchar, binary, and varbinary,
SQL Server truncates values to maximum of 30 characters. SQL Server Compact allows 4000 for nchar and nvarchar, and 8000 for binary and varbinary. Due
to this, results generated by querying SQL Server and SQL Server Compact are different. In cases where the size of the data types is specified such as nchar(200), nvarchar(200), binary(400), varbinary(400), the results are consistent across SQL Server and
SQL Server Compact.
I can't explain it in this case -
Hi,
I'm trying to create a adhoc report and need some help as i think the CONVERT and CAST aren't working the way am thinking. Please see the below SQL, When am executing this am getting the error :"
Msg 8114, Level 16, State 5, Line 1
Error converting data type varchar to numeric." The reason is i want the (1.5*cnt) to be shown just once per day and not repeat for each hour of the day as the revenue in the table is for the whole day
Thanks
SELECT l.*,
CASE
WHEN l.rn = l.cnt THEN l.erev
ELSE ''
END AS erev1
FROM (
SELECT
k. [plotnumber],
k.date,
CASE
WHEN k.rn = k.cnt THEN k.rev
ELSE ''
END AS rev,
1.5 * k.cnt)AS erev,
k.rn,
k.cnt
from (
SELECT [plotnumber],
[date],
[rev],
row_number() OVER( partition BY [plotnumber], date ORDER BY [plotnumber], date) AS rn,
count([spacenumber]) OVER( partition BY [plotnumber], date ) AS cnt
FROM mytbl)k)lSorry SQL-PRO
here it is, i used CAST as well but am just posting the CONVERT
SELECT L.*,
CASE
WHEN L.rn = L.cnt THEN L.erev
ELSE ''
END AS EREV1
FROM (SELECT k.zone,
k. [plotnumber],
k.date,
CASE
WHEN k.rn = k.cnt THEN k.rev
ELSE ''
END AS Rev,
1.5 * convert(numeric,K.cnt))AS EREV,
K.rn,
K.cnt
FROM (SELECT
[plotnumber],
[date],
[rev],
Row_number()
OVER(
partition BY [plotnumber], date
ORDER BY [plotnumber], date)
AS rn,
Count([spacenumber])
OVER(
partition BY [plotnumber],
date
) AS cnt
FROM MYTBL)k)L -
CASE and CONVERT in Q of Q / CF7
Am converting CF5 code to CF7, and from documentation,
understand you can do more things in Query-of-Queries in CF7 than
you could in CF5.
But one QofQ that worked in CF5 won't work in CF7, returns an
error. It uses a variable that on occassion causes a division by
zero. This was never a problem with CF5, but CF7 won't take it. In
a normal query, I would handle by using a CASE statement. So my
question is: CAN YOU USE "CASE" statements in CF7 QofQ's? And what
about CONVERT or CAST? Can you use those as well, in CF7 QofQ's?
Thanks for any help/advice.
GaryThanks to you both for confirming what my testing and
doc-reading seemed to confirm. That you can't use CASE or CONVERT
in a CF7 QofQ. I think you're right about CAST but not sure if that
will solve my problem. QofQ in CF5 was "flaky" enough to permit a
division by zero. But I had to "account" for it in my
<CFOUTPUT>, in order to get the report to run and print 0%.
Here's my situation. Current CF5 (and CF7) in main query, I'm
calculating the values of a number of variables, let's say one of
them is A, the other B. In the old CF5 QofQ, I could do an "A
divided by B" as C, where C was my Percent Complete. And if B was
ever ZERO, the CF5 QofQ would still run. For some reason, QofQ in
CF5 was "flaky" enough to permit this.
But in my <CFOUTPUT>, I would have to check the value
of B. If it was equal to zero, then I'd set C = to "0%" or "N/A",
and output the results, which printed fine on the report. If B was
not = to ZERO, then I'd re-calculate the percentage, and set C = A
divided by B, and output "C%" in the report.
But with CF7, the template throws an error in the QofQ, when
it encounters an instance where B = 0. The error message is:
"Query Of Queries runtime error. java.lang.Double ->
BigDecimal "
I can resolve by adding a HAVING statement to first query,
only accepting records where B > 0. Problem with that, is I lose
the display of the record that I would like to have shown, but
showing 0%, versus not bein there at all. I did read in the docs,
before posting message, that CAST can be used in CF7 QofQ, but not
sure if that will resolve the problem.
I'll figure something out, but the main reason for posting
the initial question was to verify from others, that CASE and
CONVERT can NOT be used in CF7's QofQ's. The docs simply didn't
mention one way or the other regarding these 2 query functions.
If you or anyone else has any other suggestions, I would be
most grateful. Otherwise, as mentioned, will hopefully be able to
figure something out. Thanks again,
Gary -
Cast and Concat functions error
Hi
I am getting below errors while doing cast and concat
The error message is nQSError: 22020] Function Concat does not support non-text types.
and my concat code is
CAST (cast(day(current_date) as char)||'-'||month(current_date)||'-'||cast(year(Core."Dim - Date"."Date") as char) AS DATE )
Error when tried it differently [nQSError: 27002] Near <)>: Syntax error [nQSError: 26012] .
CAST (concat(cast(day(current_date) as char)||'-'||month(current_date)||'-'||cast(year(Core."Dim - Date"."Date") as char)) AS DATE )
Please suggest.Hi,
CASE WHEN Core."Dim - Date"."Year" = 'year(current_date)' THEN FILTER( AGO(Core."Fact - Spend and AP Invoice Distribution"."Spend YTD", Core."Date"."Year", 1)
USING Core."Dim - Date"."Date" = VALUEOF(NQ_SESSION."R_Current_Date")) WHEN Core."Dim - Date"."Year" <> 'year(current_date)' THEN FILTER( AGO(Core."Fact - Spend
and AP Invoice Distribution"."Spend YTD", Core."Date"."Year", 1) USING Core."Dim - Date"."Date" = CAST ( TRIM(BOTH ' ' FROM CAST ( EXTRACT( DAY FROM CURRENT_DATE )
AS CHARACTER ( 30 ))) || '/' || TRIM(BOTH ' ' FROM CAST ( EXTRACT( MONTH FROM CURRENT_DATE ) AS CHARACTER ( 30 ))) || '/' || TRIM(BOTH ' ' FROM
CAST (Core."Dim - Date"."Year" AS CHARACTER ( 30 ))) AS DATE )) ELSE 0 END
Here I have taken this part of the code from above code and started testing from answers
CAST ( TRIM(BOTH ' ' FROM CAST ( EXTRACT( DAY FROM CURRENT_DATE )
AS CHARACTER ( 30 ))) || '/' || TRIM(BOTH ' ' FROM CAST ( EXTRACT( MONTH FROM CURRENT_DATE ) AS CHARACTER ( 30 ))) || '/' || TRIM(BOTH ' ' FROM
CAST (Core."Dim - Date"."Year" AS CHARACTER ( 30 ))) AS DATE )) ELSE 0 END
what I could make out is when I was casting it to date I was getting the error
ORA-01830: date format picture ends before converting entire input string at OCI call OCIStmtExecute:
So When I have casted it to char and removed the year function from concatenation it worked out.
With the same idea I have used this part of the code in my main code above but here char was not working out, so I need to use date and there were no compilation
errors as it used to be before.
CASE WHEN Core."Dim - Date"."Year" = 'year(current_date)' THEN FILTER( AGO(Core."Fact - Spend and AP Invoice Distribution"."Spend YTD", Core."Date"."Year", 1)
USING Core."Dim - Date"."Date" = VALUEOF(NQ_SESSION."R_Current_Date")) WHEN Core."Dim - Date"."Year" <> 'year(current_date)' THEN FILTER( AGO(Core."Fact - Spend
and AP Invoice Distribution"."Spend YTD", Core."Date"."Year", 1) USING Core."Dim - Date"."Date" = CAST ( TRIM(BOTH ' ' FROM CAST ( EXTRACT( DAY FROM CURRENT_DATE )
AS CHARACTER ( 30 ))) || '/' || TRIM(BOTH ' ' FROM CAST ( EXTRACT( MONTH FROM CURRENT_DATE ) AS CHARACTER ( 30 ))) || '/' || TRIM(BOTH ' '
FROM CAST (Core."Dim - Date"."Year" AS CHARACTER ( 30 ))) AS DATE )) ELSE 0 END
Now when I check results in reports. I get the error ORA-01830: date format picture ends before converting entire input string at OCI call OCIStmtExecute
The problem here happening is
Core."Dim - Date"."Date" data type is DATETIME
and the concat string whole date type is DATE.
Its like datetime=date
Since becuase of this mismatch I assume I am getting this error.
So I thought of changing the data type of Core."Dim - Date"."Date" data type in rpd. Even though I checked out to do the changes I am unable to do though I login with
Admin account.
Secondly I thought of changing the concat string whole date type to DATETIME then I get the syntax error.
Any help on this would be much appreciated.
Thanks.
Edited by: 949675 on May 7, 2013 5:33 AM -
Trying to find epoch dates and convert them to dates
I'm trying to find every epoch time in a string and convert them to a date. I have tried the below with no luck.
SELECT REGEXP_REPLACE('1315617396271 Impacting: CISCO SAN SWITCH1315617396271 Impacting: CISCO SAN SWITCH'
,'([[:digit:]]{10,10})',unixts_to_date('\1'))
FROM dual
Result
Error - ORA-01722: invalid number
I know the dates are good and the function works fine from the below test.
select unixts_to_date('1315617396') from dual
result
10 September 2011 01:16:36 AM
Any help with this would be much appreciated!Hi,
I was not able to copy-and-paste your example due to unknown characters (which are displayed as squares on my Ubuntu laptop).
I modified them replacing those characters by spaces.
Maybe that won't totally fit your needs, or might behave differently on your system.
Anyway, I propose the following :Scott@my11g SQL>l
1 with t as (
2 select '1315617396 271 Impacting: CISCO SAN SWITCH 1315617396 271 Impacting: CISCO SAN SWITCH ' str from dual
3 union all select '1315417396 123 Impacting: CISCO SAN SWITCH 1315317396 123 Impacting: CISCO SAN SWITCH ' from dual
4 union all select 'I went to France from 1123456789 to 1234567890 and it was great !' from dual
5 )
6 ------ end of sample data ------
7 select
8 str
9 ,listagg(strdt) within group (order by n) newstr
10 from (
11 select
12 str
13 ,n
14 ,regexp_replace(sstr,'(.*?)\d*$','\1') || (to_date('1970101','yyyymmdd') + regexp_substr(sstr,'\d{10}',1)/3600/24) strdt
15 from (
16 select
17 str
18 ,n
19 ,regexp_substr(v.str,'.*?(\d{10}|$)',1,v.n) sstr
20 from (
21 select t.str,
22 m.column_value n
23 from t
24 ,table(cast(multiset(select level
25 from dual
26 connect by level <= regexp_count(t.str,'.*?\d{10}')+1) as sys.OdciNumberList)) m
27 ) v
28 )
29 )
30* group by str
Scott@my11g SQL>/
STR NEWSTR
1315417396 123 Impacting: CISCO SAN SWITCH 1315 06 June 2012 05:43:16 PM 123 Impacting: CIS
317396 123 Impacting: CISCO SAN SWITCH CO SAN SWITCH 05 June 2012 01:56:36 PM 123
Impacting: CISCO SAN SWITCH
1315617396 271 Impacting: CISCO SAN SWITCH 1315 09 June 2012 01:16:36 AM 271 Impacting: CIS
617396 271 Impacting: CISCO SAN SWITCH CO SAN SWITCH 09 June 2012 01:16:36 AM 271
Impacting: CISCO SAN SWITCH
I went to France from 1123456789 to 1234567890 and I went to France from 07 May 2006 11:19:49 P
it was great ! M to 13 November 2009 11:31:30 PM and it was grea
t ! -
My manipulated images come out with a green color cast and poor quality
I manage my photos with ACDSee, but use Photoshop CS3 for much of my editing. The image leaves ACDSee with rich true colors, and while I'm editing in Photoshop the image looks like I want it. But when I save the image and view it in ACDSee, it now is faded, has a green color cast and looks faded, dull and shallow. What do you think is happening? Thanks. Rickrans
One other place to look.... If you shoot raw images, there's a separate configuration for the color space the Camera Raw converter delivers its results in. Click the little link at the bottom-center of the Camera Raw dialog. If you want to work in the sRGB color space, change it as shown here:
-Noel -
TRY CAST and setting the type according to destination column
Hi,
I am loading data from different sources. I have to do data quality checks for data I am loading into destination. For Decimal Values I have destination data types Decimal(28,2) and Decimal(28,6)
I would like to check the source data and covert the type according to destination column. How can I use the try cast in this scenario?
SELECT TRY_CAST(REPLACE('100,500.000',',','') AS DECIMAL(28,2))
this statement will convert every thing to two decimal places. but if destination column is decimal(28,6) i would like to convert it to 100500.657899
What is the best way of doing it?
MHHi MH,
According to your description, you need to CAST and setting the type according to destination column which data types is Decimal(28,2) and Decimal(28,6), right?
"this statement will convert every thing to two decimal places. but if destination column is decimal(28,6) i would like to convert it to 100500.657899" If in this case, then there are different data types on the same column which is not
supported in current version. So as per my understanding, there is no such a functionality to achieve your requirement. What we can do is convert to corresponding datatype (Decimal(28,2) or Decimal(28,6)), and then convert it to nvarchar datatype.
CREATE TABLE #TEMP(A NVARCHAR(50))
INSERT INTO #TEMP VALUES('1538.21'),('1635.326541'),('136.235')
SELECT
A,
CASE
WHEN (LEN(RIGHT(A,LEN(A)-PATINDEX('%.%',A))))>2
THEN
CAST(A AS DECIMAL(28,6))
WHEN (LEN(RIGHT(A,LEN(A)-PATINDEX('%.%',A))))<=2
THEN
CAST(A AS DECIMAL(28,2))
END AS B,
CASE
WHEN (LEN(RIGHT(A,LEN(A)-PATINDEX('%.%',A))))>2
THEN
CAST(CAST(A AS DECIMAL(28,6)) AS VARCHAR(99))
WHEN (LEN(RIGHT(A,LEN(A)-PATINDEX('%.%',A))))<=2
THEN
CAST(CAST(A AS DECIMAL(28,2)) AS VARCHAR(99) )
END AS B2
FROM #TEMP
DROP TABLE #TEMP
Regards,
Charlie Liao
If you have any feedback on our support, please click
here.
Charlie Liao
TechNet Community Support -
Rounding Differences between TSQL Cast and Data Conversion component
This is using SSIS 2012. We are using an Ole Db Connection manager using the Microsoft.ACE.OLEDB.12.0 provider.
I have an Access database (accdb). It has several columns that are coming into SSIS as real data types. One column has a value of 68.8335558900872.
In the past we used the Data Conversion to convert from real to numeric 18, 6. The result was 68.833555. Rather than rounding on the last decimal place, it is returning the floor at six decimal places.
We have converted the process to load the data into a real data type column and then use a view to cast the value to a Decimal 18,6 (CAST (Col1 as Decimal(18,6))). We then load the data into a decimal 18,6 column. The result is 68.833556, which
is the rounded version of the real value.
Personally I prefer the rounded version rather than the truncated version.
I am trying to explain to explain to the QA people why the revised process is producing different results and thus I want to know if these are known behaviors for TSQL Cast and the Data Conversion component.
Russel Loski, MCT, MCSE Data Platform/Business Intelligence. Twitter: @sqlmovers; blog: www.sqlmovers.comI think such a post is more suitable for an MS Connect bugs section as I see it a defect. And it seems no floor, but simply cutting off. Try in .Net when you convert, does it work the same? Data Conversion is a wrapper around the .net
Arthur My Blog -
How to edit a Animated Gif file and convert to SWF
I am using the creative cloud with fireworks. I chose the free trial with buying in mind if I saw it work properly. I simply want to upload an animated GIF file and then download it as a SWF file. I saw someone on youtube do this and it's not that if I get on the correct page I would not know how to do that but I just cannot find how to get the GIF into the software to edit. It has just simply put them in the creative cloud folder which can open them on IE> How do i make it available to edit and convert to SWF please? Thanks in advance.
You will likely get better program help in a program forum
The Cloud forum is not about using individual programs
The Cloud forum is about the Cloud as a delivery & install process
If you will start at the Forums Index https://forums.adobe.com/welcome
You will be able to select a forum for the specific Adobe product(s) you use
Click the "down arrow" symbol on the right (where it says All communities) to open the drop down list and scroll -
Is it wise to keep the Nikon camera files "DSC's" after downloading them and converting to DNG files via Adobe converter for lightroom use. In other words do the DNG files have all the raw data I would ever need in processing or should I save the camera's DSC files?
DNG files do not contain some metadata supplied by the camera, which can be used by the manufacturer's software. Thus, if you don't keep the original Raw photo, you will lose this information.
If your 1000% sure you're never going to use the manufacturer's software, then this isn't a problem. But who can be sure what software you will be using 10 years from now? -
Open and converting raw in ps 7
how can i open and convert raw nef images from my 35mm nikon into pdf using ps 7? Raw is listed under OPEN as a category but i cannot make it happen.
You need to first open the NEF file in Photoshop, and if you are really talking about the very old version 7, there is every chance that will be a problem. I don't even know if Version 7 supported Camera RAW, but I think it did. So I am having to guess because of the age of the version you are using, but you will probably need to download the free DNG converter.
http://www.adobe.com/nz/products/photoshop/extend.displayTab2.html -
Open Photoshop 3 files and convert to TIFF or JPEG --- on the cheap!
Hello,
I have about a hundred large scans in PS3 format on DVD.
I never use PS or other pro photo SW; only iPhoto.
My last PS is v7.
I run the latest Snow Leopard on my new MBP 17 iCore7.
All I want is to recover these files and convert them to the highest quality and most accessible format without having to spend my $, only my time.
Can anyone put me near this goal?
---hI'm not sure what you mean by "PS3 format"--do you mean they are psd files that were created in Photoshop 3? As far as I remember Photoshop 3 simply created standard Photoshop files that, if one chose to have extensions (which you didn't have to way back then) had a .psd extension. If the files are indeed Photoshop files, but without the extension, you can simply copy them off the DVD and add the .psd extension to them. Preview will usually open psd files. I do have at least one VERY old psd file that Preview won't open, it displays a note in English and French, that Photoshop 3 is required. It does open with Photoshop Elements 8 and the free GIMP. Why it won't open in Preview I don't know, perhaps because it has three layers, two of which are text layers. Other psd files with layers DO open in Preview, although they often look a bit peculiar, and files with just one layer, created at the same time, open fine. Also, iPhoto will generally open psd files, and even display one with layers correctly (as if it had been flattened), but will not handle files with alpha channels very gracefully, nor will Preview. The GIMP has no problem with layers nor alpha channels.
Francine
Francine
Schwieder -
How to upload a PDF file and convert it to OTF format
We have come across rquirements like converting OTF to PDF but my requirement is to read a PDF file in SAP and convert it to OTF format for printing.
Can anyone please help me with the Function Modules to do so.Hello,
Try the following FM:
CONVERT_OTF Convert SAP documents (SAPScript) to other types.
Example:
CALL FUNCTION "CONVERT_OTF"
EXPORTING FORMAT = "PDF"
IMPORTING BIN_FILESIZE = FILE_LEN
TABLES OTF = OTFDATA
LINES = PDFDATA
EXCEPTIONS ERR_MAX_LINEWIDTH = 1
ERR_FORMAT = 2
ERR_CONV_NOT_POSSIBLE = 3
OTHERS = 4.
Hope this helps.
Reward if helpful.
Regards,
Vasanth -
Please, I have a PDF attachment in an email on Russian language, how can I open it and convert to WORD (doc.) on Russian?
Hi Senior gal,
If you have purchased the ExportPDF subscription, then just save the PDF to your desktop and follow any of the two mathod mentioned in the article below to convert the PDF to word
http://forums.adobe.com/docs/DOC-2412
~Pranav
Maybe you are looking for
-
My iSync doesnt work at all, please help
Ok, my iSync does not work anymore. Everytime i open it, it freezes up And have to force quited it all the time. Is there anyway to fix this problem? Thx.
-
How configure the auditory columns for the user in Session?
I want to have my page of login to enter the application. After validating to the login/password according to user table create table usuario( id number not null, login varchar2(15) not null, pasword varchar2(15) not null, );..., set to user in HttpS
-
In Change Request Making a default subject
Dear all, We are using SOLMAN 4 and SCM functionality.When we create a change request from charm_create transcation,i want to set in the subject line Urgent correction as default.How can we this?please help Thanks in advance, Avinash
-
Hi all, Is there any general flow for the re-work process for the REM scenario?
-
EAP-Fast or PEAP ??
Dear All, we are not sure if we should use EAP-FAST as authentication method or if we should use PEAP or EAP/TTLS. Could you please inform us which one is safer ? For PEAP or EAP/TTLS we would need a Radius Server such as ACS while we could assign an