Getting: INVALID_FUNCTION_ARGUMENT: Invalid format: "" when trying to date_parse a string formatted date - sql

Query:
SELECT date_parse(start_date, '%Y/%m/%d')
FROM sql_question_five_2 ;
date format looks like this in csv: 20210531
being read into table as string.
Have tried a few different things to get it to convert to a date YYYY-MM-DD

date_parse expects all inputs to look like the format string, if they don't it will throw an error. You can do something like this to avoid the problem:
SELECT IF(start_date = '', NULL, date_parse(start_date, '%Y/%m/%d'))
FROM sql_question_five_2
This guards against the case where the string is empty, which is the case when you get the error. If you have other strings that don't conform to the format you would have to guard against those too.
If that is the case you can use the TRY function which captures errors and returns NULL:
SELECT TRY(date_parse(start_date, '%Y/%m/%d'))
FROM sql_question_five_2

Your format string needs to match the source value. Your source doesn’t have /‘s in it so your format string shouldn’t either

Related

Bigquery converting string to timestamp

Whats the best way to convert a string value like this to a timestamp?
"2021-05-11T02:00:03.4897671"
I tried the below
select PARSE_TIMESTAMP("%Y-%m-%d %H:%M:%E#S", "2021-05-11T02:00:03.4897671")
But I get the following error
Failed to parse input string "2021-05-11T02:00:03.4897671"
Try this:
select PARSE_TIMESTAMP("%Y-%m-%dT%H:%M:%E*S", "2021-05-11T02:00:03.4897671")

Add thousand seperators for string value in SQL server

I am parsing a string which contains a (money) value in a varchar format.
Formatting is always like this: 12345,75 (varchar). Another input value could thus be 32323232,98 and so on...
Desired output = 12.345,75 (doesn't have to be output as a varchar).
So what I need is dots as thousand separators, and a comma for separating the two decimals (input value always has 2 decimals).
My attempt:
DECLARE #Num varchar(50)
SELECT FORMAT(CONVERT(numeric(10,2), REPLACE(#Num,',','.')), #Num, '#.00')
Error:
The culture parameter '#.00' provided in the function call is not supported.
Using MS SQL Azure 2019
The 'nl-nl' culture does exactly what you want. So, try using the third argument to format():
select format(1234567.888, '#,#.00', 'nl-nl')

CASE statement where conditional includes an IN statement redshift

CASE
WHEN code IN ('FJS354', 'JDF334')
THEN 'Lower_form'
ELSE 0
END AS format
This returns an error in Redshift
invalid input syntax for integer: "Lower_form"
I know if I change 'Lower_form' to an integer it will work however I want this column to be a string. Is there a way to do this?
I want this column to be a string.
All branches of a case expression must return the same datatype. You are giving two literal values whose datatype is not the same (string vs integer): the database makes the decision to turn them both to integers - which is not what you want.
Rremove the ambiguity by being explicit about the datatype you want to return. That is, make this literal 0 a string:
CASE WHEN code in ('FJS354','JDF334')
THEN 'Lower_form'
ELSE '0'
END as format

Standard Big query Not removing String

I'm trying to filter out "null" from a string column using Standard Big query and for whatever reason it is not being filtered out
so my where statement goes:
where d_transaction_dt <> "null"
d_transaction_dt is a string column that I'm trying to cast as date and remove anything in there that is "null"
I'm getting the error: Invalid date: 'null'
Please help..
Is it literally the string "null", or is the field null? If you're doing something like a SAFE_CAST to convert strings to a DATE type, you're probably getting NULL values.
WHERE d_transaction_dt <> "null"
is an entirely different filter predicate than
WHERE d_transaction_dt IS NOT NULL

Why TSQL convert a function's result in one way and a character string to other way?

I try this command in SQL Server 2005 to obtain a MD5 from '123':
select SUBSTRING(sys.fn_sqlvarbasetostr(HASHBYTES('MD5', '123' )), 3, 32)
and I get this result:
202cb962ac59075b964b07152d234b70
I want to convert to binary format,
select
convert(varbinary(16), SUBSTRING(sys.fn_sqlvarbasetostr(HASHBYTES('MD5', '123')), 3, 32))
And I get this result:
0x32003000320063006200390036003200
Why does this code:
select convert(varbinary(16), '202cb962ac59075b964b07152d234b70')
result in a different value?
0x32303263623936326163353930373562
"Regular Character Type" vs Unicode
This performs a conversion from Nvarchar(Unicode) to Varbinary
select convert(varbinary(16),SUBSTRING(sys.fn_sqlvarbasetostr(HASHBYTES('MD5', '123' )),3,32))
By default, putting text in single quotes uses regular character types like Char or Varchar. This performs a conversion from Varchar("Regular Data Type") to Varbinary
select convert(varbinary(16),'202cb962ac59075b964b07152d234b70')
Try this:
SELECT CONVERT(varbinary(16), N'202cb962ac59075b964b07152d234b70')
The "N" before the quote defines the value as Nvarchar(Unicode) and you get your desired value
0x32003000320063006200390036003200
Hope this helps!