based on other forum entries, I believe that I need to utilise the below code to convert a date in Informix to a month:
select
MONTH(DATE_FIELD) as "Month Conversion"
from TABLE
Unfortunately when I run it, I get this error
Error: ERROR Mixed numeric and alpha operands in expression -
job_cost_master.jcm_start_date SQLState: 42000 ErrorCode: -1
You cannot use a column ALIAS with spaces and/or in quotes.
This should work:
select
MONTH(DATE_FIELD) as month_conversion
from TABLE
Regards
Related
I have a table that has stored columns time_attended and time_departed from a site.
for example time_attended = "16:00" and time_departed = "17:00"
They are stored as character varying fields - how would I find the time difference?
Doing a conversion eg -
SELECT
time_attended::date - time_departed::date
FROM table
throws up the syntax error 'invalid input syntax for type date: "16:00"'
Any ideas? thanks
If those are time values, cast them to a time not a date
select time_attended::time - time_departed::time
from the_table
I'm analyzing the data of New York City taxi trips of yellow cars in 2018. (You need a Google BigQuery account to access this data set.)
The schema says that most of the columns are numeric. However, when I tried to calculate the sum of the key dollar figures (tip_amount, tolls_amount, total_amount), I got an error message saying that they are string variables.
SELECT sum(total_amount)
FROM [bigquery-public-data:new_york_taxi_trips.tlc_yellow_trips_2018]
WHERE month(dropoff_datetime) = 12
Error: Field total_amount is of type STRING which is not supported for SUM
I then tried to use the cast() function to convert it to a numeric variable, but that did not work.
SELECT sum(total_amount_numeric) FROM
(
SELECT cast(total_amount as numeric) as total_amount_numeric
FROM [bigquery-public-data:new_york_taxi_trips.tlc_yellow_trips_2018]
WHERE month(dropoff_datetime) = 12
)
Error: Field total_amount_numeric is of type STRING which is not supported for SUM
How can I analyze these numeric variables as I intended, instead of the string variables as they are erroneously set in the database?
Below is for BigQuery Standard SQL
#standardSQL
SELECT SUM(total_amount)
FROM `bigquery-public-data.new_york_taxi_trips.tlc_yellow_trips_2018`
WHERE EXTRACT(MONTH FROM dropoff_datetime) = 12
The problem you had is because NUMERIC data type is not supported by BigQuery Legacy SQL and rather is treated as STRING and cannot CAST to neither FLOAT nor INTEGER
So, the workaround is to use BigQuery Standard SQL as in above example - and as you see here you don't need to do any CAST'ing as this field is already NUMERIC
Your query will run as follows in Standard SQL:
SELECT sum(total_amount_numeric)
FROM (SELECT cast(total_amount as numeric) as total_amount_numeric
FROM `bigquery-public-data.new_york_taxi_trips.tlc_yellow_trips_2018`
WHERE EXTRACT(month FROM dropoff_datetime) = 12
) x;
You can include this hint before the query to ensure that it is run using standard SQL:
#standardSQL
I need to take the average of this data set but the column is formatted as money/text.
I have tried:
SELECT
sfo_calendar.calender_date,
AVG(CAST(sfo_calendar.price AS int) avg_price
FROM
sfo_calendar
GROUP BY sfo_calendar.calender_date;
Yet keep getting this error:
ERROR: invalid input syntax for integer: "$101.00"
SQL state: 22P02
The issue is the $ sign, it is not convertible to int so you can try replacing that by zero since it is in the beginning of the price and won't affect the value.
SELECT
sfo_calendar.calender_date,
AVG(CAST(replace(sfo_calendar.price,'$',0) AS int) avg_price
FROM
sfo_calendar
GROUP BY sfo_calendar.calender_date;
I have been trying to extract the year from my column entries that are in text format.
SELECT CONVERT(datetime, CONVERT(varchar(55), startdttm)) as correct,
EXTRACT(year from correct)
FROM table1
When running the code I've been getting the following error: SQL Error [42601]: ERROR: syntax error at or near "," which highlights the comma between the arguments of the inner convert. What is wrong with my conversion? Thanks
Your code makes no sense. You seem quite confused.
In Postgres, convert() is used to convert between different character encodings (see here). That is, it is almost never used.
Second, Postgres does not have a datetime data type. It uses timestamp, available in flavors with and without a timezone.
You could phrase the logic using CAST():
SELECT CAST(CAST(startdttm as varchar(255)) as timestamp) as correct, -- you can leave off the (255)
More commonly in Postgres, this is expressed using :::
SELECT startdttm::varchar(255)::timestamp as correct -- you can leave off the (255)
And, further, you cannot use a table alias in the SELECT where it is defined. So, you would seem to want:
SELECT EXTRACT(year FROM startdttm::varchar::timestamp) as yyyy
pgAdmin III always gives an error on year
ERROR: function year(date) does not exist
LINE 1: SELECT YEAR(geboortedatum) as date_part
.............................^^^^.........
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
********** Error **********
ERROR: function year(date) does not exist
SQL state: 42883
Hint: No function matches the given name and argument types. You might need to add explicit type casts.
Character: 8
I can get the whole date that I need but I need only the year can someone help me it needs to be the youngest year from the table.
As you mentioned pgAdmin, I assume that you are using Postgres, so you need write code like this (using EXTRACT constructure):
SELECT EXTRACT(YEAR FROM TIMESTAMP '2001-02-16 20:38:40');
Detailed documentation for the datetime functions