BigQuery, transform INTEGER variable to DATE - sql

I have a dataset with a date variable coded as NUMERIC.
e.g. "01/03/2005" appears as "1032005"
I want to transform the variable to STRING, pad it with leading zeroes, and then transform in to a DATE format. In BigQuery casting appears not to be allowed, so I create a new variable ad hoc, but I am unable to report there the original values.
How should I do it?

You can format as a string and then parse as a date:
SELECT PARSE_DATE('%d%m%Y', FORMAT('%08d', int_date))
FROM (SELECT 1032005 AS int_date)
This gives:
2005-03-01

Related

I have a Date in a String format and I can't convert it to date in BigQuery

I started with a date in a string format from a JSON extraction using this: json_value(answer, '$.date_created') and got an output 2020-01-02T10:26:47.056-04:00.
From there, I transformed the output (because I couldn't change it to date using a series of functions like regexp_replace, left and CAST) to a date-like string: 2020-01-02 10:26:47
I need to be able to transform this new string to a date. So far, I've tried with FORMAT_DATETIME and FORMAT_TIMESTAMP but I'm getting an error: Failed to parse input string bigquery
Your original timestamp string is just fine to do this:
select Date(timestamp("2020-01-02T10:26:47.056-04:00"))
Only thing here to check is: you have -4 offset from UTC so as long as you take care of timezone etc, above style should work fine.

Getting null while converting string to date in spark sql

I have dates in the format '6/30/2020'. It is a string and I want to convert it into date format.
List of methods I have tried
Cast('6/30/2020' as date) #returns null
to_date('6/30/2020','yyyy/MM/dd') #returns null
I also tried splitting the string and then concatenating it into data.
After trying all this and putting all the possible combinations in the to_date function, I am still getting the answer as null.
Now I am confused as I have used all the functions to convert string to date.
Thanks in advance for your help.
The date format you used was incorrect. Try this:
select to_date('6/30/2020', 'M/dd/yyyy')
If you want to format your result, you can use date_format:
select date_format(to_date('6/30/2020', 'M/dd/yyyy'), 'yyyy/MM/dd')
Note that to_date converts a given string from the given format, while date_format converts a given date to the given format.

How to convert string to date type in the Pentaho?

I am try to convert string value to date. The string has this format : yyyy-MM-dd. But when I try to convert using select values (in meta-date I selected fildname, type = Date and currency = dd/MM/yyyy I got this error :
String : couldn't convert string [2017-01-30] to a date using format [yyyy/MM/dd HH:mm:ss.SSS] on offset location 4
If I do in calculator step : Create a new field, Final_date as a Copy of field A; on Field A put the name of your input string; Data type is date and on Conversion mask choose the yyyy-MM-dd format (you don't have to pick one from the dropdown menu, you can write your own). I got the same error.
I am using the Pentaho Data Intagration 9.
I am try to convert the string format in yyyy-MM-dd to date type format in dd/MM/yyyy. For this case, how to convert string to date ?
When converting from string to date you specify the source format that the string is using, so in this case yyyy-MM-dd. That should be in the format selection list, but you can also manually type in any format needed.
Once the field is in date format, it will be correctly output to most database types. For files, you can define the new format (dd/MM/yyyy) in the output step like Text File Output or Excel Writer. Alternatively, you convert the date back into a string with the desired format using Select Values.

Varchar2 to date in Oracle

Can somebody help me with how to convert varchar column into Date data type?
My createDate column has VarChar2 data type in format example 20050923
I would like to convert this column into format example 2005/09/23
ALTER TABLE test ADD (new_create_date DATE);
UPDATE test SET new_create_date=TO_DATE(createDate,'MM/DD/YYYY');
When I run these sql I get format 23-aug-2005. Why?
Internally date is stored as a 7 byte value. Formatting is not something that is stored there. You get formatting when you pull the data out of the table.
Try this.
SELECT TO_CHAR(NEW_CREATE_DATE,'MM/DD/YYYY')
FROM TEST
You could do this:
update test
set new_create_date to_date(createDate, 'YYYYMMDD');
You just need to give to_date() the right format string.
This is because your database NLS_DATE_FORMAT must be in DD-MON-YYYY format, you can check that by using the below mentioned query.
SELECT *
FROM nls_session_parameters
WHERE parameter = 'NLS_DATE_FORMAT';
Also by definition to_date(x,format) converts a string i.e. x to a datetime, the format part is an optional part which indicates the format that is present for the string value x.
You can further use to_char(x,format) function in your case which converts the x datetime to a string as mentioned by the EvilTech.

character_length Teradata SQL Assistant

I have to run column checks for data consistency and the only thing that is throwing off my code is checking for character lengths for dates between certain parameters.
SEL
sum(case when ( A.date is null or (character_length(A.date) >8)) then 1 else 0 end ) as Date
from
table A
;
The date format of the column is YYYY-MM-DD, and the type is DA. When I run the script in SQL Assistant, I get an error 3580 "Illegal use of CHARACTERS, MCHARACTERS, or OCTET_LENGTH functions."
Preliminary research suggests that SQL Assistant has issues with the character_length function, but I don't know how to adjust the code to make it run.
with chareter length are you trying to get the memory used? Becuase if so that is constant for a date field. If you are trying to get the length of the string representation i think LENGTH(A.date) will suffice. Unfortanatly since teradata will pad zeros on conversions to string, I think this might always return 10.
UPDATE :
Okay so if you want a date in a special 'form' when you output it you need to select it properly. In teradata as with most DBs Date are not store in strings, but rather as ints, counting days from a given 'epoch' date for the database (for example the epoch might be 01/01/0000). Each date type in teradata has a format parameter, which places in the record header instructions on how to format the output on select. By default a date format is set to this DATE FROMAT 'MM/DD/YYYY' I believe. You can change that by casting.
Try SELECT cast(cast(A.date as DATE FORMAT 'MM-DD-YYYY') as CHAR(10)) FROM A. and see what happens. There should be no need to validate the form of the dates past a small sample to see if the format is correct. The second cast forces the database to perform the conversion and use the format header specified. Other wise what you might see is the database will pass the date in a date form to SQL Assitant and sql assitant will perform the conversion on the application level, using the format specified in its own setting rather then the one set in the database.