Converting String to Date in BigQuery? - google-bigquery

I have a timestamp in my big query looking like this: 30/01/2020 00:14:05
date is one of the column names of the table
I have already tried:
1. cast(PARSE_DATE('%Y%m%d', date) as DATE)
2. CAST(date as DATE)

In your case you need SELECT PARSE_DATETIME('%d/%m/%Y %H:%M:%S','30/01/2020 00:14:05')
or SELECT PARSE_DATE('%d/%m/%Y',SUBSTR('30/01/2020 00:14:05',1,10)) if you only need the date

Related

Custom ORDER BY in SQLite for string date

I have a column "date_time" and many other.
date_time is a string date (because in SQLite there is no date format), e.g. "2020.2.1" means 1st of February of 2020th.
I want to sort by this date 2020.1.1-2020.1.2-...-2020.12.31
I am using:
SELECT *
FROM Table
ORDER BY date_time
And I am getting sorted as a string:
here is an example:
2020.1.10
...
2020.1.29
2020.1.3
...
How I can sort it as a date if I am using SQLite and my date is string?
select *
from Table
order by cast(date_time as datetime)

Invalid datetime string when CAST As Date

I have Time column in BigQuery, the values of which look like this: 2020-09-01-07:53:19 it is a STRING format. I need to extract just the date. Desired output: 2020-09-01.
My query:
SELECT
CAST(a.Time AS date) as Date
from `table_a`
The error message is: Invalid datetime string "2020-09-02-02:17:49"
You could also use the parse_datetime(), then convert to a date.
with temp as (select '2020-09-02-02:17:49' as Time)
select
date(parse_datetime('%Y-%m-%d-%T',Time)) as new_date
from temp
How about just taking the left-most 10 characters?
select substr(a.time, 1, 10)
If you want this as a date, then:
select parse_date('%Y-%m-%d', substr(a.time, 1, 10))
select STR_TO_DATE('2020-09-08 00:58:09','%Y-%m-%d') from DUAL;
or to be more specific as your column do as:
select STR_TO_DATE(a.Time,'%Y-%m-%d') from `table_a`;
Note: this format is applicable where mysql is supported

Using Cast to Convert DateTime to Date and select today's date

I'm trying to count the number of accounts opened on today's date in the SELECT Statement. I'm doing so with an IIF statement and using CAST to convert the DateTimeStamp to Date. That said, I'm having trouble figuring out where to date column (Open_Date) and how to check to see if it's is today's date. Would I place -1 right after the ) following as date or???
COUNT(IIF(CAST(GETDATE() AS date))), SHARE.MEMBER_NBR, null)) AS ALLNEWACCOUNTSTODAY
You can use case expression with Open_Date :
COUNT(CASE WHEN CONVERT(DATE, GETDATE()) = Open_Date THEN SHARE.MEMBER_NBR END) AS ALLNEWACCOUNTSTODAY
Can you provide the architectur please ?
Without it I would suggest that :
SELECT COUNT(*) FROM Accounts Where CONVERT(date,accountDate)=CONVERT(DATE,getdate());
It assumes that you have a column accountDate containing the date you added the account and that this column is into an accounts table.

SQL: How To Select Earliest Date

I have a date column & I am simply trying to know the earliest date. I use the command:
select Min(Install_date) From PocketGemsSchema.pocketgemstable2;
This returns 1-Dec-17
But the minimum date from my sample data is actually 1-Nov-17.
Can anyone help please?
Try this:
If your Install_date contain datatype varchar than
SELECT MIN(CAST(Install_date AS DATE))
FROM PocketGemsSchema.pocketgemstable2
SELECT FORMAT(MIN(CAST(Install_dateAS DATE)), 'dd-MMM-yy ')
FROM PocketGemsSchema.pocketgemstable2
If your Install_date contain datatype date or datetime than your query will work
I think its the data type issue , you can try two approach
convert the field to datatime and your query should work
cast it on the run time like below
Mysql
SELECT Min(Str_to_date(Install_date, '%m/%d/%Y'))
FROM pocketgemsschema.pocketgemstable2;
SQL server
SELECT Min(Cast(Install_date as datetime))
FROM pocketgemsschema.pocketgemstable2;
I would change the column to a date or a datetime type and sort out any bugs that arise.

Convert/get varchar variable to YYYYMM

I have 4 CTE's in this table and the third one contains a DATETIME converted to VARCHAR (with format based on the requirement) as startDate in DD/MM/YYYY format. The last cte does calculations based on the data generated and one of the columns needs to store YYYYMM date based on startDate.
The problem it's getting the year and the month from this converted DATETIME, using convert() it shows this:
IDPER
-------
01/01/ --DD/MM/
These 2 show YYYYMM correctly when startDate isn't converted:
Select *, left(convert(nvarchar(6),new_ini,112),6) as IDPER from table
Select *, convert(nvarchar(6),new_ini,112) as IDPER from table
How could I get YYYYMM format having startDate converted? Or what could be a more smart approach to the requirement
If you have a string in the format DD/MM/YYYY and you want YYYYMM, then use string operations:
select right(new_ini, 4) + substring(new_ini, 4, 2)
You should be storing date values as dates or a related type, not as string. But given that you have already stored this as a string, string operations can do what you need.
My way would be slightly different
SELECT CONVERT(NVARCHAR(6), CONVERT(DATE, new_ini, 103), 112);
Here, I first converted it to date and then formatted to YYYYMMDD and taken 6 chars only
declare #date DATE = GETDATE();
select REPLACE(LEFT(CONVERT(DATE,#date,112),8),'-','') -- 1st approach
select FORMAT(#date,'yyyyMM') --2nd approach