How does one cast and then join elements together in Firebird?
SELECT
ARTICLE_NAME ||','||CAST
FROM
(select cast(QUANTITY as varchar(50)) from PREORDER_ITEM)
I also tried
with DATA as ( select cast(QUANTITY as varchar(50) ) from PREORDER_ITEM )
SELECT
ARTICLE_NAME ||','||QUANTITY
FROM
DATA
Why doesn't this work?
There are multiple problems with your query:
When you do select cast(QUANTITY as varchar(50) ) from PREORDER_ITEM you have only one (1) column in the derived table. In other words, there is no ARTICLE_NAME in either query.
For a select expression of a derived table or common table expression, all columns must have an explicit column name. So, for an expression you need to specify an explicit alias using (AS <alias>). In other words, there is no column CAST in your first query, and no column QUANTITY in the second.
CAST is a reserved word, so when used as a column name like in your first query, it must be delimited by double quotes.
So, use:
select article_name || quantity
from (
select article_name, cast(quantity as varchar(50)) as quantity
from preorder_item
)
or
with data as (
select article_name, cast(quantity as varchar(50)) as quantity
from preorder_item
)
select article_name || quantity
from data
As an aside, you don't need to explicitly cast. When concatenating values, Firebird will automatically coerce to a string value, so you can also use:
select article_name || quantity
from preorder_item
Related
I want to select all columns in postgres table but cast two columns into a different datatype but this creates duplicate columns
SELECT
*, CAST(column1 AS varchar),
CAST(column2 AS varchar)
FROM
my_table
I've tried to use Except to avoid duplicated columns but postgres has an issue with Except
SELECT
* EXCEPT(column1, column2),
CAST(column1 AS varchar),
CAST(column2 AS varchar)
FROM
my_table
What I'm I missing?
Try giving column alias as below. You will get both the columns - actual as well as casted to varchar but with different names.
SELECT *,
CAST(column1 AS varchar) as new_column1,
CAST(column2 AS varchar) as new_column2
FROM
my_table
I want execute this procedure, but SQL tell me this error 'The type of column "q82" conflicts with the type of other columns specified in the UNPIVOT list"
al the Q's are INT
Does somebody know solution?
INSERT INTO
[stg].[fact_answer_cc]
(
[user_id],
[question_id],
[answer],
[check_sum]
)
SELECT
[q2] AS [user_id],
[question_id],
[answer],
HASHBYTES (
N'SHA1',
ISNULL ( CAST([q2] AS VARCHAR), '' ) + #pipe_delimiter +
ISNULL ( CAST([question_id] AS VARCHAR), '' )
) AS [check_sum]
FROM
(
SELECT
[q2],
[q80],
[q81],
[q82],
[q83],
[q84],
[q85],
[q86],
[q87],
[q88],
[q89],
[q90],
[q91],
[q92]
FROM
[src].[qa_data_cc]
) AS t
UNPIVOT
(
[answer] FOR [question_id] IN
(
[q80],
[q81],
[q82],
[q83],
[q84],
[q85],
[q86],
[q87],
[q88],
[q89],
[q90],
[q91],
[q92]
)
) AS unpvt
ORDER BY
[user_id],
[question_id];
The type of column conflicts with the type of other columns specified in the UNPIVOT list.
Means your column' s data type same but it' s lenght differ.
Example:
q80 VARCHAR(10)
q82 VARCHAR(5)
Solution: #Cast all fields to fix value
CAST(q80 AS VARCHAR(10))
CAST(q82 AS VARCHAR(10))
May someone please help me for this strange scenario. i have a data as given below.
DECLARE #TABLE TABLE
(
ID INT,
PHONE001 VARCHAR(500)
)
INSERT TEST
SELECT 1,'01323840261,01323844711' UNION ALL
SELECT 2,'' UNION ALL
SELECT 3,',01476862000' UNION ALL
SELECT 4,'01233625418,1223822583,125985' UNION ALL
SELECT 5,'2089840022,9.99021E+13'
and i am trying to put in seperate column for each comma value. the max number of column depends on the largest comma seperated string.
Expected Output
1|01323840261|01323844711|''
2|''|''|''
3|01476862000|''|''|
4|01233625418|1223822583|125985|
5|2089840022|9.99021E+13|''|
try
select id,T.c.value('t[1]','varchar(50)') as col1,
T.c.value('t[2]','varchar(50)') as col2 ,
T.c.value('t[3]','varchar(50)') as col3 from
(select id,cast ('<t>'+ replace(PHONE001,',','</t><t>') +'</t>'
as xml) x
from #TABLE) a cross apply x.nodes('.') t(c)
I am new to SQL
If I have a column like this
ID
00001234
00012345
00001235
00123456
I want to see a column of ID without '0' Like this
ID
1234
12345
1235
123456
How can I start? any advice?
In SQL Server you can use:
SELECT ID,
REPLACE(LTRIM(REPLACE(ID, '0', ' ') ), ' ', '0')
FROM mytable
The above can be easily adjusted to any other RDBMS you may use.
Cast it to Bigint and cast it back to varchar
Note:Assumption: RDBMS SQL SERVER, ID is of character type
SELECT * INTO #TAB FROM (
select '00001234' ID
UNION ALL
select '00012345'
UNION ALL
select '00001235'
UNION ALL
select '00123456'
)A
SELECT CAST(CAST(ID AS BIGINT) AS VARCHAR(50)) FROM #TAB
I have a column (varchar400) in the following form in an SQL table :
Info
UserID=1123456,ItemID=6685642
The column is created via our point of sale application, and so I cannot do the normal thing of simply splitting it into two columns as this would cause an obscene amount of work. My problem is that this column is used to store attributes of products in our database, and so while I am only concerned with UserID and ItemID, there may be superfluous information stored here, for example :
Info
IrrelevantID=666,UserID=123124,AnotherIrrelevantID=1232342,ItemID=1213124.
What I want to retrieve is simply two columns, with no error given if neither of these attributes exists in the Info column. :
UserID ItemID
123124 1213124
Would it be possible to do this effectively, with error checking, given that the length of the IDs are all variable, but all of the attributes are comma-separated and follow a uniform style (i.e "UserID=number").
Can anyone tell me the best way of dealing with my problem ?
Thanks a lot.
Try this
declare #infotable table (info varchar(4000))
insert into #infotable
select 'IrrelevantID=666,UserID=123124,AnotherIrrelevantID=1232342,ItemID=1213124.'
union all
select 'UserID=1123456,ItemID=6685642'
-- convert info column to xml type
; with cte as
(
select cast('<info ' + REPLACE(REPLACE(REPLACE(info,',', '" '),'=','="'),'.','') + '" />' as XML) info,
ROW_NUMBER() over (order by info) id
from #infotable
)
select userId, ItemId from
(
select T.N.value('local-name(.)', 'varchar(max)') as Name,
T.N.value('.', 'varchar(max)') as Value, id
from cte cross apply info.nodes('//#*') as T(N)
) v
pivot (max(value) for Name in ([UserID], [ItemId])) p
SQL DEMO
You can try this split function: http://www.sommarskog.se/arrays-in-sql-2005.html
Assuming ItemID=1213124. is terminated with a dot.
Declare #t Table (a varchar(400))
insert into #t values ('IrrelevantID=666,UserID=123124,AnotherIrrelevantID=1232342,ItemID=1213124.')
insert into #t values ('IrrelevantID=333,UserID=222222,AnotherIrrelevantID=0,ItemID=111.')
Select
STUFF(
Stuff(a,1,CHARINDEX(',UserID=',a) + Len(',UserID=')-1 ,'' )
,CharIndex
(',',
Stuff(a,1,CHARINDEX(',UserID=',a) + Len(',UserID=')-1 ,'' )
)
,400,'') as UserID
,
STUFF(
Stuff(a,1,CHARINDEX(',ItemID=',a) + Len(',ItemID=')-1 ,'' )
,CharIndex
('.',
Stuff(a,1,CHARINDEX(',ItemID=',a) + Len(',ItemID=')-1,'' )
)
,400,'') as ItemID
from #t