How do I replace NULL in datediff with a text - sql

I have a 2 different date columns and I'm using datediff to calculate the years of working. But there is nulls when the people is still working I wonder how can I replace it with a text 'Still Working'
So this is the table that I have
ID DateofHire DateofTermination
1 2011-07-05 NULL
2 2015-03-30 2016-06-16
3 2011-07-05 2012-09-24
4 2008-01-07 NULL
I have use the datediff formula and it shows like this :
DATEDIFF(year,DateofHire,DateofTermination) as 'Years of working'
ID Years_of_Working
1 NULL
2 1
3 1
4 NULL
How can I replace the NULL after I wrote a datediff query i wanted to replace it with 'Still Working'
The desired table would be like this:
ID Years_of_Working
1 Still working
2 1
3 1
4 Still Working

I assume that you will get Years_of_Working as Integer values. Replacing the attribute of the table with a different type is inconsistent.
I would just replace NULL values with 0, which would implicitly mean, that these people are still working.
So if the original table is called hire_info, then solution would look like this:
with diff_years as (
select ID,DATEDIFF(year,DateofHire,DateofTermination) as 'Years of working'
from hire_info
)
select ID, ISNULL(Years_of_Working,0) Years_of_Working
from diff_years

You can use this query. You should change the table name to your own table name.
SELECT id, if(isnull(DateofTermination),"Still working",TIMESTAMPDIFF(year,DateofHire,DateofTermination)) as 'Years of working' from date_diff;

Related

SQL - split numeric into 2 columns?

I am trying to split some numeric keys in my table into separate columns (to help save space in SSAS, lower cardinality)
My data looks like the below..
LeadKey
1
2
3
5522
83746623
I want to split these into 2 columns... with 4 digits in each column. (where applicable, as anything 1>9999 won't have anything populated in the 2nd column)
So an example output of the above would be the below..
LeadKey Split1 Split2
1 1
2 2
35566 3556 6
5522 5522
83746623 8374 6623
How could I achieve this? I have split columns easily before using substring and a known character.. but never had to do a split like this. Does anyone have an approach to handle this?
Here is a solution in case you have the LeadKey numbers as int.
select LeadKey
,left(LeadKey, 4) Split1
,right(LeadKey, case when len(LeadKey)-4 < 0 then 0 else len(LeadKey)-4 end) Split2
from t
LeadKey
Split1
Split2
1
1
2
2
35566
3556
6
5522
5522
83746623
8374
6623
Fiddle
In this example, I used left for the Split1, and show the values past the 4th position for the Split2:
I've included a testing temporary table to hold our the testing values.
Feel free to adjust the code to work with your situation.
DECLARE #thelist TABLE
(
LeadKey int
);
INSERT INTO #thelist (LeadKey)
select 1 union all
select 2 union all
select 35566 union all
select 5522 union all
select 83746623
select cast(x1.LeadKey as varchar(19)),
Left(x1.LeadKey, 4) as 'Split1',
(case when len(x1.LeadKey) > 4 then right(x1.LeadKey, len(x1.LeadKey) - 4)
else '' end
) as 'Split2'
from #thelist as x1

Select the column not like and combine column in one table [duplicate]

This question already has answers here:
How to SUM two fields within an SQL query
(9 answers)
Closed 10 months ago.
My question is how can I select the currency not equal to USD and CNY and rename as others. Also, combining it into a new column AMT_Others.
ID Name Date AMT_HKD AMT_JPY AMT_USD AMT_TWD AMT_CNY
1 Amy 01/04/2022 0 5000 0 0 0
2 Bill 01/03/2022 200 0 0 0 0
3 Cathy 02/02/2022 0 0 80 2000 200
Result:
ID Name Date AMT_Others
1 Amy 01/04/2022 5000
2 Bill 01/03/2022 200
3 Cathy 02/02/2022 2000
my code: (It cannot generate what I want, what should be added? Thanks)
select ID, Name, Date, (AMT_HKD, AMT_JPY and AMT_TWD) as AMT_Others
basically add them up as long as we have only one non zero value for all currencies except USD and CNY
Select ID, Name, Date,
AMT_HKD + AMT_JPY + AMT_TWD as AMT_Others
from yourtable
You can get your desired result by using + instead of comma or AND:
SELECT id, name, ydate, amt_hkd + amt_jpy + amt_twd AS amt_others FROM yourtable;
But this is quite unpleasant in case there are null values in some of your columns. In this case, the sum would be null, too, even if some of the columns are not null. You can use COALESCE to prevent this and to force building a sum. Null values will be replaced by 0 (or another value if you change it):
SELECT id, name, ydate,
COALESCE(amt_hkd,0) + COALESCE(amt_jpy,0) + COALESCE(amt_twd,0) AS amt_others
FROM yourtable;
You can check this and see the difference between the two query results here: db<fiddle
You should also prevent table names or column names that are SQL key words, like the "date" column in your example. That'
s why I renamed it in my answer. Of course, if you can't change this, you have to live with it, but if you can, you should choose another name.
the other answers will work, unless there can be null values, if there is a row where one of these columns is ´null` then the result will also be null
select 1 + 2 -- output is 3
select 1 + 2 + null -- output is null
solution to this problem
isnull(AMT_HKD, 0) -- easy but slow
COALESCE(amt_hkd, 0) -- easy but still not the fastest solution
case when amt_hkd is null then 0 else amt_hkd end -- fastest
SELECT ID, Name, Date,
AMT_HKD + AMT_JPY + AMT_TWD as AMT_Others
FROM tb_name;
This will work

Use JSON array in SQL server instead of traditional relational database, each value in 1 row

I have saved the answer values in a table in rows, 1 answer 1 row, 5 rows in this example.
If I migrate it to JSON it will be 2 rows(JSON)
Table
Id
Optionsid
Pid
Column
1
2
1
null
2
1
2
null
3
1
2
null
4
2
2
null
5
3
1
null
I want to calculate how many answers(pid) for each Optionsid with
SELECT COUNT(pid)AS Counted, OptionsId
FROM Answer GROUP BY [Column], OptionsId
Table Results
Counted
Optionsid
2
1
2
2
1
3
I have run thus query and saved it in a new table
select * from Answer for Json Auto
Json Table I added {"answer":} to the Json
id
pid
json
1
1
{"Answer":[{"Id":1,"Optionsid":2,"Pid":1}]}
2
2
{"Answer":[{"Id":2,"Optionsid":1,"Pid":2},{"Id":2,"Optionsid":1,"Pid":2},{"Id":3,"Optionsid":2,"Pid":2},{"Id":4,"Optionsid":3,"Pid":2}]}
I want to get the same result from Json Table as the Table result above, but I can get it to work
This Query only take the first[0] in the array, i want a query who take all values in the array.
Can someone help me with this query?
Select Count(Json_value ([json], '$.Answer[0].Pid')) as Counted,
Json_value ([json], '$.Answer[0].Optionsid') as OptionsId
from [PidJson]
group by Json_value ([json],'$.Answer[0].Column'),Json_value
([json],'$.Answer[0].Optionsid')
Here is a fiddle if you want to see
https://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=0a2df33717a3917bae699ea3983b70b4
Here is the solution
SELECT Count(JsonData.Pid) as Counted,
JsonData.Optionsid
FROM PidJson AS RelationsTab
CROSS APPLY OPENJSON (RelationsTab.json,
N'$.Answer')
WITH (
Pid VARCHAR(200) N'$.Pid',
Optionsid VARCHAR(200) N'$.Optionsid',
ColumnValue INT N'$.Column'
) AS JsonData
Group by JsonData.ColumnValue, JsonData.Optionsid
Thanks for your time and that you "force" me to clearify my question and I find the solution

How to convert column values to single row value in postgresql

Data
id cust_name
1 walmart_ca
2 ikea_mo
2 ikea_ca
2 ikea_in
1 walmart_in
when i do
select id,cust_name from test where id=2
Query returns below output
id cust_name
2 ikea_mo
2 ikea_ca
2 ikea_in
How can i get or store the result as single column value as shown below
id cust_name
2 {ikea_mo,ikea_ca,ikea_in}
you should use string_agg function and here is an example for it
select string_agg(field_1,',') from db.schema.table;
you should mention the separator in your case its , so I am doing string_agg(field_1,',')

Getting a union query to create single records

So I've put together a union query in access, but it displays in this fashion.
Name Column1 Column2
John 1 0
Jim 2 0
Mike 3 0
John 0 2
Jim 0 1
Mike 0 3
I would like for it to display like this:
Name Column1 Column2
John 1 2
Jim 2 1
Mike 3 3
In my select statement I'm setting column2 to 0 in the first portion of the union statement, and setting column1 to 0 in the second part. Now I realize that's why I'm getting the 0s in the end result, but is there a way to achieve my desired display with a union or do I need something more complex.
EDIT: The reason I set those columns to 0, was to avoid it asking me to enter a value when it could not find a value for column1 or 2.
PS. this is not my actual query, but rather just a quick example I was able to throw on here for question purposes.
select [Name], max([Column1]) as col1, max([Column2]) as col2
from ( put union query here ) as x
group by [Name]
edit -- didn't notice that your data was a query result and not a table. Put your current query where indicated in the from clause (as an inline view)