Switching lastname,firstname to firstname.lastname in sql server - sql-server-2014

I have the below data in a SQL Server 2014 column called EMP_CLASS_10_DESCR
OUELLETTE,MAXIME :845800
ANDERSON,ADRIEN :021252
MITRI,NATHALIA :687173
What I like to do is have the data represented in the following way by way of a query.
MAXIME.OUELLETTE
ADRIEN.ANDERSON
NATHALIA.MITRI
What I've done so far is
CASE WHEN CHARINDEX(':', EMP_CLASS_10_DESCR)
= 0 THEN EMP_CLASS_10_DESCR ELSE LEFT(EMP_CLASS_10_DESCR, CHARINDEX(':', EMP_CLASS_10_DESCR) - 1) END AS FULL_NAME,
But all that did was give me the following
OUELLETTE,MAXIME
ANDERSON,ADRIEN
MITRI,NATHALIA
I still need to flip the names around and replace the , (comma) with a . (period)
Any help will be much appreciated. I've done some searches but couldn't find anything close. I'm still very new to SQL Server and just trying to learn.

You can use PARSENAME:
WITH CTE AS
(
SELECT CASE
WHEN CHARINDEX(':',YourColumn) > 0
THEN RTRIM(LTRIM(LEFT(YourColumn,CHARINDEX(':',YourColumn)-1)))
ELSE YourColumn
END NewColumn
FROM dbo.YourTable
)
SELECT CONCAT(PARSENAME(REPLACE(NewColumn,',','.'),1),'.',PARSENAME(REPLACE(NewColumn,',','.'),2))
FROM CTE;

Related

How to hide the Repeated record in SQL

Using this query:
SELECT
linerbltbl.billofladingid,
linerbltbl.grossweighttotal,
linerb‌​ltbl.netweighttotal,
linerblwisecontainerdetailstbl.containertype,
linerblwisecont‌​ainerdetailstbl.cont‌​ainernumber,
linerblwisecontainerdetailstbl.sealnumber,
linerblwisecontain‌​erdetailstbl.princip‌​lecharge
FROM linerbltbl,
linerblwisecontainerdetailstbl
WHERE linerbltbl.shippername IS NOT NULL
AND linerbltbl.billofladingid =
linerblwisecontainerdetailstbl.bil‌​lofladingid
See this image. It shows what my output IS and what I would like it to be.
You will note that the repeated data is blanked out. Is there a way to do this in my query?
First of all, I think you are going about this wrong. While you CAN do this from within the query itself (see this page) you are probably better off doing it in whatever is consuming the output. For example, if you are taking this data and pasting it into MS Excel, you could check out this post which does is via conditional formatting. If you are consuming it from inside code, you can add logic to handle it there (since I don't know what you are going to do with it, it's hard to add any examples).
But, if you insist on doing it from within SQL it's going to be something to this effect (borrowing from the linked example):
;with t as
(SELECT
linerbltbl.billofladingid,
linerbltbl.grossweighttotal,
linerb‌​ltbl.netweighttotal,
linerblwisecontainerdetailstbl.containertype,
linerblwisecont‌​ainerdetailstbl.cont‌​ainernumber,
linerblwisecontainerdetailstbl.sealnumber,
linerblwisecontain‌​erdetailstbl.princip‌​lecharge
FROM linerbltbl,
linerblwisecontainerdetailstbl
WHERE linerbltbl.shippername IS NOT NULL
AND linerbltbl.billofladingid =
linerblwisecontainerdetailstbl.bil‌​lofladingid
)
SELECT
CASE WHEN RR = 1 THEN billofladingid ELSE '' END BillOfLadingID,
CASE WHEN RR = 1 THEN grossweighttotal ELSE '' END GrossWeight,
CASE WHEN RR = 1 THEN netweighttotal ELSE '' END NetWeight,
containertype,
cont‌​ainernumber,
sealnumber,
princip‌​lecharge
FROM
(SELECT (ROW_NUMBER() OVER(PARTITION BY billofladingid ORDER BY billofladingid, grossweighttotal)) [RR], *
FROM t) SUBQUERY1

Little bit of trouble with my MySQL query

My MySQL Query
Ok so I'm trying to add another column (leave_remaining) that will display that if the leave_status is 'ok' then the leave_remaining will show the number of leave days that employee has left. I keep getting errors with this. What is the right syntax here? Thank you
This works:
select id,leave_started,leave_ended,no_of_leave_allowed,
leave_ended-leave_started AS no_of_leaves_taken,
if (leave_ended-leave_started >no_of_leave_allowed,
'leave exceeded','ok')as leave_status
from leave_taken;
This does not:
if (leave_status,'ok',
(no_leave_allowed-no_leaves_taken))as leave_remaining
from leave_taken;
select * from leave_taken;
If I'm understanding you properly, I think you want this:
SELECT
id, leave_started, leave_ended, no_of_leave_allowed,
(leave_ended - leave_started) AS no_of_leaves_taken,
IF(
(leave_ended - leave_started) > no_of_leave_allowed,
'leave exceeded',
'ok'
) AS leave_status,
IF (
(leave_ended - leave_started) > no_of_leave_allowed,
(no_of_leave_allowed - (leave_ended - leave_started)),
0
) AS leave_remaining
FROM leave_taken;
Though not sure how you have defined leave_started and leave_ended that you can subtract them like that...
http://sqlfiddle.com/#!9/16cdb3/6
SELECT has to be before the IF, altough you are not doing anything.
The correct statement is
SELECT something as alias, something_else as alias_else, if(,,) as alias_if
FROM table
WHERE where = clause
When you have to use some statement (IF or CASE for example), that result in a field, it has to be inside a select.

How to use select with update query ? mysql

I want to count points for each business for having deals or gallery :
so if the business has un empty deal, or has at least one gallery , business_data_count should be 2.
this what i've tried :
UPDATE `business` businessTable SET
business_data_count
=
sum(
(
SELECT
CASE
WHEN count(*)>= 1 then count(*)
ELSE 0
END as points
FROM gallery WHERE bussId=businessTable.bussId
)
+
(
SELECT
case
WHEN deal!='' then 1
ELSE 0
end
FROM business WHERE bussId=businessTable.bussId
)
where 1
but i got this error :
you cant specify table business for update
How to fix this ?
There's no need to do a separate select from the update table. Try this instead (untested):
UPDATE business
SET business_data_count = (deal != '')
+ (SELECT COUNT(*)
FROM gallery
WHERE bussId = business.bussId);
On a separate note, it's generally bad practice to store data that can be easily extracted with a query, such as the above.
I think you have a typo.
I think you should have:
UPDATE `business`.businessTable SET
instead of
UPDATE `business` businessTable SET
If you use the schema when defining your table in your query, you need to separate them with a dot (.).
Maybe it is not the only problem, but that's the first that comes to mind.

Repeat Case Statement in MySQL

SELECT `pur_view`.`pro_id` AS `pro_id`,
coalesce(sum((CASE WHEN (`pur_view`.`ware_id` = 1) THEN `pur_view`.`qty` END)),0) AS `Ware_1`,
coalesce(sum((CASE WHEN (`pur_view`.`ware_id` = 3) THEN `pur_view`.`qty` END)),0) AS `Ware_2`,
coalesce(sum((CASE WHEN `pur_view`.`ware_id` THEN `pur_view`.`qty` END)),0) AS `total`
FROM `pur_view`
GROUP BY `pur_view`.`pro_id`
And i need to repeat ware_id dynamically please help me
Seem you can use procedure with cursor to do it. We need select distinct ware_id first. And foreach it to select.
But I think that is complex way. The simple way to select normally, and use php or C ... to convert it to format that you want.

MySQL - help with IF... ELSE usage within query

I need to add an if/else stament inside a MySQL query but can't get it to work.
Below is an example with pseudo-code of what I want to accomplish.
SELECT *
FROM calendar
WHERE calendar.alert = 1
IF calendar.repeat = 0 THEN HAVING calendar.UTC_1 > UNIX_TIMESTAMP();
ELSE "get all records regardless of calendar.repeat value";
END IF;
Any suggestions to make this happen? I couldn't find the correct syntax for this MySQL IF ELSE.
Thanks for helping!
SELECT *
FROM calendar
WHERE calendar.alert = 1
AND CASE
WHEN `repeat` =0 THEN UTC_1 > UNIX_TIMESTAMP()
ELSE 1=1 END;
You can use IF-ELSE only inside the body of stored procedure/trigger. You can use IF(condition, value_if_condition_is_true, value_if_condition_is_false) in SELECT, but I prefer CASE(you can easily rewrite the query above to
.... AND IF(`repeat` = 0, UTC_1>UNIX_TIMESTAMP(),1=1)
Not sure I got the syntax all right here, but I like the idea of somethign like this. In my opinion it is much easier to look at in the future and see the two groups you're grabbing with the following example. I'm not educated on efficiency of case vs union in MySQL but it seems to me like the case would be less efficient as well. Maybe someone can answer that for sure?
SELECT *
FROM calendar
WHERE
calendar.alert = 1
AND calendar.repeat = 0
AND calendar.UTC_1 > UNIX_TIMTESTAMP();
UNION
SELECT *
FROM calendar
WHERE
calendar.alert = 1
AND calendar.repeat != 0