How to CONCAT quotes while selecting any data from the table - sql

The file (date.sql) has the below commands
date=2014-12-03
SET VAR vDate $date
insert into (Some_table) (date,location)
select $vDate , location from (let say from table "Location")
while executing this sql script i am getting the below error as its expecting values like '2014-12-03' but here its coming without quotes
ERROR:Insert data type mismatch for column date
Is there anyway to concat ' before and after date or is there any simpler way to achieve it ???
Thanks in advance.

Related

Concatenate string with different alias name for each value

I am getting string like this from sql query
[FY20-Q1],[FY20-Q2],[FY20-Q3],[FY20-Q4]
I want to make the output as below
[FY20-Q1] as currentQuater,[FY20-Q2] as nextQuater,[FY20-Q3] as nextQuater1,[FY20-Q4] as nextQuater2
Note-The above format is coming from this query
select #cols = COALESCE (#cols+',['+period+']','['+period+']')
from fn_somefnname()
Thanks in Advance!

Getting an error: Argument data type varchar is invalid for argument 2 of substring function

I'm trying to get a substring from the value of a column and I'm getting the following error Argument data type varchar is invalid for argument 2 of substring function.
The column type is NvarChar(50) and is a system column for an application, so I can't modify it.
Ideally I'd just be able to select the substring as part of the query without having to alter the table, or create a view or another table.
Here's my query
SELECT SUBSTRING(INVOICE__, ':', 1)
FROM dwsystem.dbo.DWGroup
Im trying to select only everything in the string after a specific character. In this case the : character.
Use charindex with : as the first argument
select substring(invoice__,charindex(':',invoice__)+1,len(invoice__))
from dwsystem.dbo.dwgroup
SUBSTRING parameter is start position and end position so both parameter will be number like below
SELECT SUBSTRING(INVOICE__, 1, 1)
FROM dwsystem.dbo.DWGroup
you can use SUBSTRING_INDEX as you used mysql
SELECT SUBSTRING_INDEX(INVOICE__,':',-1);
example
SELECT SUBSTRING_INDEX('mytestpage:info',':',-1); it will return
info

How to circumvent error Message 241 (Conversion failed when converting date and/or time from character string)

I am simply trying to sort by my acquisition date column that has dates in this type of format: '04/02/2019' etc... I have created and column named ACQ_DATE_CONVERTED in an new table and I usual get the results such as: 2019-04-02. Instead I am getting the
error Message 241 (Conversion failed when converting date and/or time from character string)
I have tried the following:
SELECT [ITEM], [ACQ_DATE],
CONVERT(DATE,[ACQ_DATE]) AS ACQ_DATE_CONVERTED
,' ' AS MFG
INTO [ABC].[dbo].[My_Store_records_CONVERTED]
FROM [ABC].[dbo].[My_Store_records]
After my results I then order by ACQ_Date_Converted.
SELECT [ITEM], [ACQ_DATE]
FROM [ABC].[dbo].[My_Store_records_CONVERTED]
ORDER by [ACQ_DATE_CONVERTED]
My expected results should look something like this in the table:
Column_A Column_B ColumnC
Rows ITEM ACQ_DATE ACQ_DATE_CONVERTED
1. ITEM_1 04/09/2007 2007-04-09
2. Store item 01/26/2008 2008-01-26
etc...
Can you please try the following query:
REPLACE(CONVERT(VARCHAR(10), [ACQ_DATE], 111), '/', '-') AS ACQ_DATE_CONVERTED
Since you are converting from ACQ_DATE, so you can use ORDER BY [ACQ_DATE], it will work.
Demo on DB Fiddle
Thank you everyone that helped with input for this issue.
I discovered that I had a hidden character in all of the cells for the column I wanted to ORDER by; therefore, causing every line to fail. However, with the below I was able to overcome this stumbling block. I hope that anyone having this issue will find my solution below helpful.
REMOVING THE HIDDEN CHARACTER.
, REPLACE([ACQ_DATE], CHAR(10),'') AS [ACQ_DATE_CONVERTED]
,' ' AS MFG
INTO [ABC].[dbo].[My_Store_records_CONVERTED]
FROM [ABC].[dbo].[My_Store_records]
and sorting the new table by ACQ_DATE_Converted;
ALTER TABLE [ABC].[dbo].[My_Store_records_CONVERTED] ALTER COLUMN [ACQ_DATE_CONVERTED] DATE NULL;
and then of course;
ORDER by [ACQ_DATE_CONVERTED]
Best Regards,
Dex

Invalid digits on Redshift

I'm trying to load some data from stage to relational environment and something is happening I can't figure out.
I'm trying to run the following query:
SELECT
CAST(SPLIT_PART(some_field,'_',2) AS BIGINT) cmt_par
FROM
public.some_table;
The some_field is a column that has data with two numbers joined by an underscore like this:
some_field -> 38972691802309_48937927428392
And I'm trying to get the second part.
That said, here is the error I'm getting:
[Amazon](500310) Invalid operation: Invalid digit, Value '1', Pos 0,
Type: Long
Details:
-----------------------------------------------
error: Invalid digit, Value '1', Pos 0, Type: Long
code: 1207
context:
query: 1097254
location: :0
process: query0_99 [pid=0]
-----------------------------------------------;
Execution time: 2.61s
Statement 1 of 1 finished
1 statement failed.
It's literally saying some numbers are not valid digits. I've already tried to get the exactly data which is throwing the error and it appears to be a normal field like I was expecting. It happens even if I throw out NULL fields.
I thought it would be an encoding error, but I've not found any references to solve that.
Anyone has any idea?
Thanks everybody.
I just ran into this problem and did some digging. Seems like the error Value '1' is the misleading part, and the problem is actually that these fields are just not valid as numeric.
In my case they were empty strings. I found the solution to my problem in this blogpost, which is essentially to find any fields that aren't numeric, and fill them with null before casting.
select cast(colname as integer) from
(select
case when colname ~ '^[0-9]+$' then colname
else null
end as colname
from tablename);
Bottom line: this Redshift error is completely confusing and really needs to be fixed.
When you are using a Glue job to upsert data from any data source to Redshift:
Glue will rearrange the data then copy which can cause this issue. This happened to me even after using apply-mapping.
In my case, the datatype was not an issue at all. In the source they were typecast to exactly match the fields in Redshift.
Glue was rearranging the columns by the alphabetical order of column names then copying the data into Redshift table (which will
obviously throw an error because my first column is an ID Key, not
like the other string column).
To fix the issue, I used a SQL query within Glue to run a select command with the correct order of the columns in the table..
It's weird why Glue did that even after using apply-mapping, but the work-around I used helped.
For example: source table has fields ID|EMAIL|NAME with values 1|abcd#gmail.com|abcd and target table has fields ID|EMAIL|NAME But when Glue is upserting the data, it is rearranging the data by their column names before writing. Glue is trying to write abcd#gmail.com|1|abcd in ID|EMAIL|NAME. This is throwing an error because ID is expecting a int value, EMAIL is expecting a string. I did a SQL query transform using the query "SELECT ID, EMAIL, NAME FROM data" to rearrange the columns before writing the data.
Hmmm. I would start by investigating the problem. Are there any non-digit characters?
SELECT some_field
FROM public.some_table
WHERE SPLIT_PART(some_field, '_', 2) ~ '[^0-9]';
Is the value too long for a bigint?
SELECT some_field
FROM public.some_table
WHERE LEN(SPLIT_PART(some_field, '_', 2)) > 27
If you need more than 27 digits of precision, consider a decimal rather than bigint.
If you get error message like “Invalid digit, Value ‘O’, Pos 0, Type: Integer” try executing your copy command by eliminating the header row. Use IGNOREHEADER parameter in your copy command to ignore the first line of the data file.
So the COPY command will look like below:
COPY orders FROM 's3://sourcedatainorig/order.txt' credentials 'aws_access_key_id=<your access key id>;aws_secret_access_key=<your secret key>' delimiter '\t' IGNOREHEADER 1;
For my Redshift SQL, I had to wrap my columns with Cast(col As Datatype) to make this error go away.
For example, setting my columns datatype to Char with a specific length worked:
Cast(COLUMN1 As Char(xx)) = Cast(COLUMN2 As Char(xxx))

Update Query to get rid of "AM" & "PM" in string

I wrote a basic update query:
Update WA SET WA.Time_Updated = Replace(Time_Updated, 'PM', ' ');
to which I don't get any real error message other than
Microsoft can't update 251 records etc due to type conversion error
There are 5000 records in there. I have the date column as Date/Time and all my other columns (non-dates) as Short Text. The query just does not update anything in the table and keeps it previously was. Any ideas?
Just convert your text times to Date values:
Select *, TimeValue([Time_Updated]) As TimeUpdated From WA
Then, when you display TimeUpdate, format the value as you like.
Can deal with the imported structure.
Consider:
Hour("12:03:00 PM") + Minute("12:03:00 PM")/60 + Second("12:03:00 PM")/3600
This calculates to 12.05
So don't change the raw data, calculate in query. Just use your field name in place of the static value in the expression.