I want hour value only from the currenttime
Like if it is 4:31:13 so i want only 4 and that also should be in 12hrs format..
Please help me...
SELECT
case when DATEPART(hh, time) > 12
then DATEPART(hh, time) - 12
else DATEPART(hh, time) end as hour
from time;
SAMPLE FIDDLE.
This will also correctly handle 12 AM and 12 PM time:
select case datepart(hh, getdate())
when 0 then 12
when 12 then 12
else datepart(hh, getdate()) % 12
end;
SELECT (DATEPART(HOUR, GETDATE()) + 11) % 12 + 1
At 1 am this will return:
1 + 11 = 12; 12 % 12 = 0; 0 + 1 = 1
At 1 pm (13:00 in 24 hrs format) you'll get:
13 + 11 = 24; 24 % 12 = 0; 0 + 1 = 1
Similarly, at 2 am and at 2 pm (14:00):
2 + 11 = 13; 13 % 12 = 1; 1 + 1 = 2
14 + 11 = 25; 25 % 12 = 1; 1 + 1 = 2
and so on till 11 am and 11 pm. At 12 am (0:00) & 12 pm (12:00) it will go like this:
0 + 11 = 11; 11 % 12 = 11; 11 + 1 = 12
12 + 11 = 23; 23 % 12 = 11; 11 + 1 = 12
I don't know what database engine are you using, but in PostgreSQL you can do this:
select to_char(current_timestamp, 'HH12');
SELECT SUBSTRING(LTRIM(RIGHT(CONVERT(VARCHAR(20), GETDATE(), 100), 7)),1,1)
SELECT DATEPART(hour, '2008-09-27 09:30:00.000')
Refer this
Related
I assign normal rv with mean 12 to ultimately generate order_date for my mock data. But when i am checking it out to see whether 12 is most used value, it turns out to be not. Any recommendations why it is the case...
def random_date_generator(month):
day_range = calendar.monthrange(2020, month)[1]
day = random.randint(1, day_range)
**first_hour = int(np.random.normal(12, 2))**
# second_hour = int(np.random.normal(18, 2))
hour = random.choices([first_hour])[0]
minute = random.randint(1, 59)
date = dt.datetime(2020, month, day, hour, minute).strftime("%Y/%m/%d %H:%M")
return date
columns = ['Order ID', 'Product', 'Quantity Ordered', 'Price Each', 'Order Date',
'Purchase Address', "Month"]
df = pd.DataFrame(columns=columns)
order_id = 123
for month_int in range(1, 13):
if month_int == 12:
order_amount = int(np.random.normal(100, 30))
if month_int == 11:
order_amount = int(np.random.normal(90, 30))
if month_int < 11:
order_amount = int(np.random.normal(60, 10))
for i in range(order_amount):
products_list = [product for product in products]
weights = [products[key][1] for key in products_list]
product = random.choices(products_list, weights=weights)[0]
price = products[product]
date = random_date_generator(month_int)
address = generate_random_addresses()
month = calendar.month_name[month_int]
df.loc[i] = [order_id, product, "NA" ,price, date, address, month_int]
order_id += 1
df.to_csv(f"{month}_data2.csv")
print(f"{month}_data2.csv")
break
january = pd.read_csv("January_data2.csv")
january["Hour"] = january["Order Date"].str[-5:-3]
january.groupby("Hour").count()
That is the output and as you see the most generated time is 12 but 10.
Unnamed: 0 Order ID Product Quantity Ordered Price Each Order Date Purchase Address Month
Hour
07 2 2 2 0 2 2 2 2
08 2 2 2 0 2 2 2 2
09 9 9 9 0 9 9 9 9
10 15 15 15 0 15 15 15 15
11 11 11 11 0 11 11 11 11
12 10 10 10 0 10 10 10 10
13 9 9 9 0 9 9 9 9
14 7 7 7 0 7 7 7 7
15 3 3 3 0 3 3 3 3
I have a table called BomNarration with a column called Narration that contains a line of texts, and there is one keyword called "Rev" that exists in every line. I want to select the words before and after the keyword. One problem is that in different lines, the words I want to select have different lengths and the keyword can be in anywhere. Two example of the line of texts in the column is "do this that for such such GX-77568 REV A." and "Follow BW-8452-355 REV D for this this that that".
I am writing the sql query on R studio to send to a sqlserver. Right now, I can only grab 11 character before the keyword and everything after the keyword.
SELECT LEFT(SUBSTRING(BomNarration.Narration,
(CHARINDEX('REV', BomNarration.Narration) - 12),
LEN(BomNarration.Narration)), 11) AS Drawing,
LEFT(SUBSTRING(BomNarration.Narration,
(CHARINDEX('REV', BomNarration.Narration) + 3),
LEN(BomNarration.Narration)), 3) AS RevNum
FROM BomNarration
I expected only the 11 character before the keyword and 3 character after the keyword will show up. What I want is only the word before and after the keyword to show up ("GX-77568", "A." and "BW-8452-355" "D"). But what I get is only the 11 character before the keyword and everything after the keyword ("ch GX-77568" "A." and "BW-8452-355" "D for this this that that")
The result table on my screen
Op Ln Drawing RevNum
<
1 1 10 ING 33B8222 -
<
2 1 13 RSC16A0019 D. AN NCR MUST BE RAISED AND
<
3 1 20 NG C16A0019 E
<
4 5 2 SW-900-0009 B
<
5 6 6 SW-800-0004 A
<
6 14 24 SW-900-0078 A
<
7 15 12 SW-900-0078 A
<
8 16 4 SW-800-0006 A
<
9 22 6 SW-800-0006 A
<
10 25 4 SW-900-0008 C
<
11 27 3 SW-900-0055 B
<
12 28 4 SW-900-0055 B
<
13 29 3 SW-900-0055 B
<
14 30 3 SW-900-0055 B
<
15 36 1 SW-900-0255 A
<
16 36 4 SW-900-0266 A
<
17 36 8 SW-900-0057 A. IF THIS *
<
18 39 2 SW-900-0041 A
<
19 45 3 SW-900-0020 A
<
20 46 2 SW-900-0020 A
Just to be clear, I would display the entire code
SELECT BomOperation.Op,
CASE WHEN BomOperations.AutoNarrCode = 0
THEN BomOperationsStd.Ln
ELSE BomOperations.Ln
END,
CASE WHEN BomOperations.AutoNarrCode = 0
THEN LEFT(SUBSTRING(BomNarrationStd.Narration, (CHARINDEX('REV', BomNarrationStd.Narration) - 12), LEN(BomNarrationStd.Narration)), 11)
ELSE LEFT(SUBSTRING(BomNarration.Narration, (CHARINDEX('REV', BomNarration.Narration) - 12), LEN(BomNarration.Narration)), 11)
END
CASE WHEN BomOperations.AutoNarrCode = 0
THEN LEFT(SUBSTRING(BomNarration.NarrationStd, CHARINDEX('REV', BomNarrationStd.Narration) + 3, LEN(BomNarrationStd.Narration)), 3)
ELSE LEFT(SUBSTRING(BomNarration.Narration, CHARINDEX('REV', BomNarration.Narration) + 3, LEN(BomNarration.Narration)), 3)
END
FROM ......
Three chars after 'REV' is
Left(SUBSTRING(BomNarration.Narration, CHARINDEX('REV', BomNarration.Narration) + 3, LEN(BomNarration.Narration)), 3)
DEMO
SELECT LEFT(SUBSTRING(BomNarration.Narration,
(CHARINDEX('REV', BomNarration.Narration) - 12),
LEN(BomNarration.Narration)), 11) AS Drawing,
LEFT(SUBSTRING(BomNarration.Narration,
(CHARINDEX('REV', BomNarration.Narration) + 3),
LEN(BomNarration.Narration)), 3) AS RevNum
FROM (values
('do this that for such such GX-77568 REV A.')
,('Follow BW-8452-355 REV D for this this that that'))
BomNarration(Narration)
returns
Drawing RevNum
ch GX-77568 A.
BW-8452-355 D
Your code after the last edit works OK.
I have a gritty industrial control problem i'm trying to solve with T-SQL.
The goal is to calculate an index position for each of two pallet loading robots, positioned in one of two ranges; 2 to 78 (robot 1) and 4 to 80 (robot 2).
Each robot indexes in steps of 4 so complete coverage of 80 spots on the pallet is achieved. The robots work side by side with a minimum spacing of 2 spots while they move along the pallet.
Two sized boxes can be placed on the pallet, one twice as long as the other. If two small boxes are placed side by side taking up 1 spot each, a single larger box can be placed on top, taking up 2 spots until a maximum height is reached. Thus the spot number for a small box is always odd and for a large box is always even and the robot index number is always even. e.g. (see diagram) from index position 14 spots 13 and 15 are loaded, and from index 20 spots 19 and 21 can be loaded.
Robot Index Positions
I need a conversion formula that calculates the Index number for a given Spot and Robot.
The calculated Index column should look like the following:
Spot Robot Index
1 1 2
2 1 2
3 1 2
- - -
13 1 14
14 1 14
15 1 14
16 2 16
17 2 16
18 1 18
19 2 20
- - -
- - -
77 1 78
78 1 78
79 2 80
80 2 80
One way would be to do an update to the Index column with every possible combination of Spot and Robot using a simple CASE WHEN selection or maybe do lookups on a reference table holding every possible combination. What I would like to explore (if any math wizards are inclined!) is a math formula that calculate the Index value.
So far I've come up with the following by converting formula developed for use in Excel. The Robot 2 section is incomplete. The 95 to 99 values are for error checking.
UPDATE MovesTable SET [Index] =
CASE
WHEN Robot = 1 THEN
CASE
WHEN Spot%4 = 0 THEN '99'
WHEN Spot = 1 or Spot = 2 or Spot = 3 THEN '02'
WHEN Spot = 5 or Spot = 6 or Spot = 7 THEN '06'
WHEN Spot = 9 or Spot = 10 or Spot = 11 THEN '10'
WHEN Spot%10 = 4 THEN CONCAT(Spot/10,'4')
WHEN Spot < 57 AND (((Spot/10)%2 = 1 AND (Spot%10)%2 = 1) AND (Spot%10 = 3 OR Spot%10 = 5)) THEN CONCAT(Spot/10,'4')
WHEN Spot%10 = 8 THEN CONCAT(Spot/10,'8')
WHEN Spot < 57 AND (((Spot/10)%2 = 1 AND (Spot%10)%2 = 1) AND (Spot%10 = 7 OR Spot%10 = 9)) THEN CONCAT(Spot/10,'8')
WHEN Spot%10 = 2 THEN CONCAT(Spot/10,'2')
WHEN Spot < 57 AND (((Spot/10)%2 = 1 AND (Spot%10)%2 = 0) AND (Spot%10 = 1 OR Spot%10 = 3)) THEN CONCAT(Spot/10,'2')
WHEN Spot%10 = 6 THEN CONCAT(Spot/10,'6')
WHEN Spot < 57 AND (((Spot/10)%2 = 0 AND (Spot%10)%2 = 1) AND (Spot%10 = 5 OR Spot%10 = 7)) THEN CONCAT(Spot/10,'6')
WHEN Spot%10 = 0 THEN CONCAT(Spot/10,'')
WHEN Spot = 49 THEN '50'
WHEN Spot < 57 AND (((Spot/10)%2 = 0 AND (Spot%10)%2 = 1) AND Spot%10 = 9) THEN '30'
WHEN Spot < 57 AND (((Spot/10)%2 = 1 AND (Spot%10)%2 = 1) AND Spot%10 = 1) THEN CONCAT(Spot/10,'0')
WHEN Spot > 56 AND (((Spot/10)%2 = 1 AND (Spot%10)%2 = 1) AND (Spot%10 = 7 OR Spot%10 = 9)) THEN CONCAT(Spot/10,'8')
WHEN Spot > 56 AND (((Spot/10)%2 = 0 AND (Spot%10)%2 = 1) AND (Spot%10 = 1 OR Spot%10 = 3)) THEN CONCAT(Spot/10,'2')
WHEN Spot > 56 AND (((Spot/10)%2 = 0 AND (Spot%10)%2 = 1) AND (Spot%10 = 5 OR Spot%10 = 7)) THEN CONCAT(Spot/10,'6')
ELSE '98'
END
ELSE
CASE
WHEN Robot = 2 THEN
CASE
WHEN (Spot%2 = 0 AND Spot%4 <> 0) OR (Spot = 1 OR Spot = 2) THEN '97'
WHEN Spot = 4 then '04'
WHEN Spot = 8 then '08'
WHEN Spot%4 = 0 THEN Spot
WHEN Spot = 2 OR Spot = 5 THEN '05'
WHEN Spot = 7 OR Spot = 9 THEN '08'
WHEN Spot = 19 THEN '20'
WHEN Spot = 39 THEN '40'
WHEN Spot = 59 THEN '60'
ELSE '96'
END
ELSE '95'
END
END
I tried to solve this mathematically, rather than by analyzing cases, etc. It matches all of your sample results:
declare #t table (Spot int, Robot int, [Index] int)
insert into #t(Spot,Robot,[Index]) values
(1 ,1 , 2 ),
(2 ,1 , 2 ),
(3 ,1 , 2 ),
(13 ,1 ,14 ),
(14 ,1 ,14 ),
(15 ,1 ,14 ),
(16 ,2 ,16 ),
(17 ,2 ,16 ),
(18 ,1 ,18 ),
(19 ,2 ,20 ),
(77 ,1 ,78 ),
(78 ,1 ,78 ),
(79 ,2 ,80 ),
(80 ,2 ,80 )
select *,
CONVERT(int,
ROUND((Spot +
CASE WHEN Robot = 1 THEN 2 ELSE 0 END
)/4.0,0)* 4 -
CASE WHEN Robot = 1 THEN 2 ELSE 0 END
) as Index2
from #t
The logic is "round to the nearest multiple of four" but we use a couple of expressions to offset Robot 1's results by 2.
Results:
Spot Robot Index Index2
----------- ----------- ----------- -----------
1 1 2 2
2 1 2 2
3 1 2 2
13 1 14 14
14 1 14 14
15 1 14 14
16 2 16 16
17 2 16 16
18 1 18 18
19 2 20 20
77 1 78 78
78 1 78 78
79 2 80 80
80 2 80 80
I'm trying to write PL/SQL to get how much you pay if you purchase a certain amount of items. If you purchase 3 items, you get 1 free.
Therefore, for every 4th item purchased one of them is free. That means I pay for 3 items if 4 items are "purchased." If 10 items are attained then 8 should be paid for, 2 are free.
p-b v_p
*** ***
1 1 = 1
2 2 = 2
3 3 = 3
4 3(1) = 3
5 3(1) + 1 = 4
6 3(1) + 2 = 5
7 3(1) + 3 = 6
8 3(1) + 3(1) = 6
9 3(1) + 3(1) + 1 = 7
10 3(1) + 3(1) + 2 = 8
11 3(1) + 3(1) + 3 = 9
12 3(1) + 3(1) + 3(1) = 9
I've got
trunc(p-b / 3 * 2.4);
but my values are inconsistent.
Sometimes it works with
trunc(p-b / 3 * 2.25);
Am I doing the formula incorrectly? I'm supposed to be using trunc or mod.
To calculate the amount of Payed/Free stuff:
DECLARE
nAmount NUMBER := '10';
nPay NUMBER;
nFree NUMBER;
BEGIN
nPay := nAmount - TRUNC(nAmount / 4);
nFree := TRUNC(nAmount / 4);
DBMS_OUTPUT.PUT_LINE('Pay: ' || nPay);
DBMS_OUTPUT.PUT_LINE('Free: ' || nFree);
END;
/
I would use trunc and modulo
SELECT p-b, (trunc(p-b /4)) * 3 + mod(p-b, 4)
FROM your_table
demo
We can use the below formula to attain the result.
Assume n = p-b
n - ( n - mod( n, 4 ) ) / 4
Eg:
SELECT n, n - (n - mod(n,4))/4 result
FROM (SELECT LEVEL n
FROM DUAL
CONNECT BY LEVEL <= 12)
in simple way the result is:
p-b - trunc(p-b / 4);
I am trying to dynamically modify my where clause by using a declared variable in a case statement above. Forgive me if there is a simple answer to this that I'm missing, but I'm entirely self taught and still have a lot to learn. Here's a sample of the syntax. I am not going to bother with the entire syntax of the statement, just enough that hopefully you will understand what I'm trying to do.
declare #myvar1 double
declare #myvar2 double
SELECT
q1.host_po_nbr,
q1.etd,
CASE
WHEN WEEKDAY(TODAY) = 1 Then 11
WHEN WEEKDAY(TODAY) = 2 Then 12
WHEN WEEKDAY(TODAY) = 3 Then 13
WHEN WEEKDAY(TODAY) = 4 Then 14
WHEN WEEKDAY(TODAY) = 5 Then 15
END #myvar1,
CASE
WHEN WEEKDAY(TODAY) = 1 Then 5
WHEN WEEKDAY(TODAY) = 2 Then 6
WHEN WEEKDAY(TODAY) = 3 Then 7
WHEN WEEKDAY(TODAY) = 4 Then 8
WHEN WEEKDAY(TODAY) = 5 Then 9
END #myvar2,
FROM q1
WHERE q1.etd BETWEEN today - #myvar1 AND today - #myvar2
I'm assuming that you're only using variables to try and reference the calculation in the SELECT clause from your WHERE clause. You can't reference aliases from the SELECT in your WHERE clause (at least not in most SQL systems). The reason is that The WHERE clause is evaluated before the select, so those aliases do not exist at the time the WHERE clause is being evaluated. You can use a subquery, however:
SELECT * FFROM
(SELECT
q1.host_po_nbr,
q1.etd,
CASE
WHEN WEEKDAY(TODAY) = 1 Then 11
WHEN WEEKDAY(TODAY) = 1 Then 12
WHEN WEEKDAY(TODAY) = 1 Then 13
WHEN WEEKDAY(TODAY) = 1 Then 14
WHEN WEEKDAY(TODAY) = 1 Then 15
END myvar1,
CASE
WHEN WEEKDAY(TODAY) = 1 Then 5
WHEN WEEKDAY(TODAY) = 1 Then 6
WHEN WEEKDAY(TODAY) = 1 Then 7
WHEN WEEKDAY(TODAY) = 1 Then 8
WHEN WEEKDAY(TODAY) = 1 Then 9
END myvar2,
FROM q1
) q
WHERE etd BETWEEN today - myvar1 AND today - myvar2
Try this:
SELECT
q1.host_po_nbr,
q1.etd,
CASE
WHEN WEEKDAY(TODAY) = 1 Then 11
WHEN WEEKDAY(TODAY) = 1 Then 12
WHEN WEEKDAY(TODAY) = 1 Then 13
WHEN WEEKDAY(TODAY) = 1 Then 14
WHEN WEEKDAY(TODAY) = 1 Then 15
END myvar1,
CASE
WHEN WEEKDAY(TODAY) = 1 Then 5
WHEN WEEKDAY(TODAY) = 1 Then 6
WHEN WEEKDAY(TODAY) = 1 Then 7
WHEN WEEKDAY(TODAY) = 1 Then 8
WHEN WEEKDAY(TODAY) = 1 Then 9
END myvar2,
FROM q1
HAVING q1.etd BETWEEN today - myvar1 AND today - myvar2
This, however, could be inefficient. If this does not solve your problem, post more details, in particular, your table structure (SHOW CREATE TABLE) and a good description of what you are trying to achieve.