// A - calculate the sum of payments for one day 2021-01-02
SELECT SUM(PAYMENT) AS TOT_PAYMENTS
FROM PAYMENTS
WHERE (PAY_DATE::timestamp)::date = '2021-01-02'
The datatype of PAY_DATE is TIMESTAMP, so i found a way in another question to convert it to DATETIME while filtering and it seems to be working. My question is where can i find the documentation for this type of method? I could only find references to CAST and PARSE methods, can somebody explain this syntax to me?
Hi 👋🏻 Hope you are doing well!
I know that Amazon Redshift has such syntaxes for the data types converting, e.g. you can find it in the documentation (see the first section CAST):
https://docs.aws.amazon.com/redshift/latest/dg/r_CAST_function.html
expression :: type
expression - an expression that evaluates to one or more values, such as a column name or a literal. Converting null values returns nulls. The expression cannot contain blank or empty strings.
type - one of the supported data types.
return type - the data type specified by the type argument.
Related
I have a problem with conversion of datatypes in hiveql. Data type of a column is int, it should be converted to date, but there is no direct conversion function from int to date. At first I converted int into string and it worked as below:
From this moment I based on the second column where I have my data converted to string. When I try to use cast(string as date), then the third column returns only nulls:
What is interesting, there is no problem when the string value to be converted was typed manually by a user:
Does anyone of You know how to deal with this problem?
your method in the comment is quite good.
One more approach using regexp_replace:
select date(regexp_replace(cda_date,'^(\\d{4})(\\d{2})(\\d{2})','$1-$2-$3'))
This question already has answers here:
CAST and IsNumeric
(11 answers)
Closed 8 years ago.
Why does the following Query in SQL return true??
I expected it to be false as it cannot be converted into an int or numeric value
select ISNUMERIC(',')
return 1???
select ISNUMERIC('0,1,2')
select ISNUMERIC(',,,')
also returns 1
What could I do for strict numeric checking in SQL?
Because ISNUMERIC answers a question that nobody has ever wanted to ask:
Can this given string be converted into any of SQL Server's numeric data types? And I don't care which of those types it can or cannot be converted into.
This is why TRY_CONVERT was finally introduced into 2012 - to answer a question about a specific data type that you may care about.
For earlier versions, the best you can usually do is to use LIKE to identify the string patterns you do want to attempt to convert.
E.g. if you just want to detect digits, use Value NOT LIKE '%[^0-9]%', which asks for Value strings that do not contain any character which is not a digit.
IsNumeric returns true for "," and "."
ISNUMERIC returns 1 if the string can be converted to any one of ints, numeric/decimal, float, or money. In this particular case, converting ',.' to money succeeds and returns 0.0000, therefore ISNUMERIC returns 1.
Enhancement to ISNUMERIC:
We have now added a new scalar function called TRY_CONVERT that will allow you to convert from a string to type using optional style. If the conversion fails then it will return NULL. The signature of the function is:
TRY_CONVERT(data_type[(length)], expression [,style])
http://msdn.microsoft.com/en-us/library/ms186272.aspx
Microsoft: "It's not a bug, it's a feature "
Certain currency and mathematic symbols return 1 on ISNUMERIC.
As per the manual page:
ISNUMERIC returns 1 for some characters that are not numbers, such as plus (+), minus (-), and valid currency symbols such as the dollar sign ($). Also, it returns 1 for a range of datatypes like:
int
bigint
smallint
tinyint
decimal
So while bigint accepts money values like 230,000, a comma(,) also falls in the set of character which is considered to be a part of a numeric datatype similar to a dot(.) which is a character but is a part of the decimal datatype.
I have a table and need to verify that a certain column contains only dates. I'm trying to count the number of records that are not follow a date format. If I check a field that I did not define as type "date" then the query works. However, when I check a field that I defined as a date it does not.
Query:
SELECT
count(case when ISDATE(Date_Field) = 0 then 1 end) as 'Date_Error'
FROM [table]
Column definition:
Date_Field(date, null)
Sample data: '2010-06-27'
Error Message:
Argument data type date is invalid for argument 1 of isdate function.
Any insight as to why this query is not working for fields I defined as dates?
Thanks!
If you defined the column with the Date type, it IS a Date. Period. This check is completely unnecessary.
What you may want to do is look for NULL values in the column:
SELECT SUM(case when Date_Field IS NULL THEN 1 ELSE 0 end) as 'Date_Error' FROM [table]
I also sense an additional misunderstanding about how Date fields, including DateTime and DateTime2, work in Sql Server. The values in these fields are not stored as a string in any format at all. They are stored in a binary/numeric format, and only shown as a string as a convenience in your query tool. And that's a good thing. If you want the date in a particular format, use the CONVERT() function in your query, or even better, let your client application handle the formatting.
ISDATE() only evaluates against a STRING-like parameter (varchar, nvarachar, char,...)
To be sure, ISDATE()'s parameter should come wrapped in a cast() function.
i.e.
Select isdate(cast(parameter as nvarchar))
should return either 1 or 0, even if it's a MULL value.
Hope this helps.
IsDate takes a character string or exression that yeilds a character string as it's argument
The problem is this method ISDATE() only admits arguments of type datetime and smalldatetime within the "time" types, so it won´t work if you are using date type.
Also if you use date as type for that field, you won´t have to check the information there because it won´t admit other type of field.
You shoul only check for null values in your column, that´s all.
I want to use Like operator in a column of datetime. The values of the column is as follows:
2013-08-31 17:54:52.000
2013-08-31 17:54:52.000
My query is as below:
SELECT * FROM table where created_date Like '%54%'
It works fine. but when I search for '%52%' instead of '%54%', it gives me nothing. (It is working when I search till the minutes, but when I search for seconds or milli seconds it does not work.)
I have looked at the following url and it is working
SQL LIKE Statement on a DateTime Type
I want to know the reason, why this is happening and how like operator works with datetime type column.
I think it would be a better idea to use the DATEPART operator of SQL SERVER to extract the portion of date.
And example could be like:-
SELECT * FROM table
where DATEPART(minute,created_date)=54
EDIT:-
I want to know the reason, why this is happening and how like operator
works with datetime type column.
Actually there is no direct support given by SQL Server for LIKE operator for DATETIME variable but you can always cast the DATETIME to a VARCHAR and then try to use the LIKE operator as you want.
On a side note:-
MSDN says:-
DATEPART can be used in the select list, WHERE, HAVING, GROUP BY and
ORDER BY clauses.
In SQL Server 2012, DATEPART implicitly casts string literals as a
datetime2 type. This means that DATEPART does not support the format
YDM when the date is passed as a string. You must explicitly cast the
string to a datetime or smalldatetime type to use the YDM format.
The 'LIKE' operator and any regular expression operators provided by other databases are used to process text values. A date is definitely not a text value, it is a separata type by itself.
It makes little sense to apply a text operator to a non-text type (like int or DATETIME or DATETIMEOFFSET), which is why you can't use LIKE on dates in any database. First of all, the values are not stored as text but in an implementation-specific binary form.
Then, while you can use LIKE on a specific text representation of a date, eg using CAST you have to absolutely certain what that representation is. Different locales display dates differently, with year first, year last, month first or last or whatever. What would you search against?
Moreover, what is 54? 54 minutes, 54 seconds or 654 milliseconds?
The only sensible solution is to use DATEPART to check specific parts of a date, or the BETWEEN operator to check for ranges.
I am trying to do a comparison based on a column. Let's say, if column>5.
select * where column>5
The column contains non digits. I thought Oracle allows one to compare strings (like Java).
Apparently this is not allowed.
ORA-01722: invalid number
01722. 00000 - "invalid number"
Is there a way to do comparisons with non numeric fields?
Thanks
Yes, you have to put the 5 in quotes :
select * from table where column > '5'
To shed a bit more light on why it doesn't work.
When using a number literal 5 instead of a character literal '5' Oracle does an implicit data type conversion and tries to convert all values in your table to a number. That's why you get that error.
You should never rely on implicit data type conversion. That is bound to give you trouble sometime.
Now if you correctly compare a character literal ('5') against a character column, no data type conversion is needed and no error occurs.
However: if you expect Oracle to actually do a numeric comparison then you are mistaken. Character string are based on ASCII values. Therefor the (character) value '10' is lower than the (character) value '2' because the first character '1' is ranked lower than '2'.
If the column is varchar2, then this:
select * from some_table where some_column > 5
... does an implicit conversion of all the column values to numbers, so you're really doing:
select * from some_table where to_number(some_column) > 5
It's the to_number() that's causing the ORA-01722, even though you can't see it, when it hits a value that is not numeric. The function being called on the column value also stops any index being used (oversimplifying a bit).
You can stop it failing, and let it use the index if there is one, by doing where some_column > '5' as other have said, or where some_column > to_char(5). But you need to be careful doing the comparison as it will still be a string comparison, not a numeric one; so '10' will not be seen as > '5'; and your NLS sorting parameters might produce results you aren't expecting. Or more importantly, someone else's NLS parameters - when you put this live for example - might product results you aren't expecting and which don't match the ones you got in your environment. See the documentation for more.
You should use number columns to hold numeric values, date columns to hold dates, etc., and varchar2 only to hold text values.
you can use to_char function
select * from table where column > to_char(5)
You are missing the table:
select * where FROM tablename column>5
But this only works if column is a number. If not, you can't use >.
To compare strings, you can use LIKE or STRCMP(), check examples of them HERE.
As stated by #Gerrat, you can also use > and < but the types of both sides must be compatible (number with number or text with text). To find more about it, check THIS.
Be aware that in text comparison it will compare each character individually so '11' will be < that '2'.