IIF Condition Ques - tsql

Can someone explain this condition, because I'm getting wrong Time data eg : I'm expecting sch departure time as 15.10 but I'm getting 15.01
[Sch Dep Time] = IIF(DATEPART(Hour,[Journey and Details.schdeptime]) < 10 AND DATEPART(Minute,[Journey and Details.schdeptime]) < 10,
('0' + DATEPART(Hour,[Journey and Details.schdeptime]) + ':0' + DATEPART(Minute,[Journey and Details.schdeptime])),
IIF(DATEPART(Hour,[Journey and Details.schdeptime]) < 10,
('0' + DATEPART(Hour,[Journey and Details.schdeptime]) + ':' + DATEPART(Minute,[Journey and Details.schdeptime])),
(DATEPART(Hour,[Journey and Details.schdeptime]) + ':0' + DATEPART(Minute,[Journey and Details.schdeptime]))))

Update
It suddenly hit me that [Sch Dep Time] should contain the time component of the dataTime value stored in [Journey and Details.schdeptime], in a minute resolution. For that, you don't need to mess around with specific date parts and string concatenation, all you have to do is use convert:
[Sch Dep Time] = CONVERT(char(5), [Journey and Details.schdeptime], 108)
The 108 style returns hh:mm:ss (24 hours), and by using char(5) you are just taking the first 5 chars of that string - hh:mm.
First version
You have overcomplicated things. Try using the old right('00' + val, 2) trick instead:
[Sch Dep Time] = RIGHT('00' + CAST(DATEPART(Hour,[Journey and Details.schdeptime]) AS VARCHAR(2)), 2) + ':' +
RIGHT('00' + CAST(DATEPART(Minute,[Journey and Details.schdeptime]) AS VARCHAR(2)), 2)
Exlpanation:
You start off by concatenating leading zeroes to the string you want.
Suppose you have a string representing a number that must always have 4 digits, but it might be 1234 or 0003 - so you start by doing '0000' + #YourNumber.
Then, you use RIGHT to trim off any unwanted zeros - suppose you now have 000023, but you want 0023 - you do RIGHT('000023', 4) to get the last 4 chars.

Related

Separator for five or more digits number in Tableau

I was working on a Tableau Project. We want to have a separator for five or more digits numbers.
For ex:-
1 as 1
12 as 12
123 as 123
1234 as 1234
12345 as 12,345
123456 as 1,23,456
Can you please assist me, how to achieve this?
I am nearly sure that this cannot be done as long as numbers are formatted as numbers. However, as a workaround, I have developed a method which however will convert numbers to string. Let's say you have a column col of desired numbers
copy your column say col2 (save original for future use) and convery type to string
Create a new calculated field say desired by using this calculation
If LEN([Col2]) <= 4 THEN
[Col2]
ELSEIF LEN([Col2]) < 6 THEN
REPLACE([Col2], RIGHT([Col2], 3), "") + "," +RIGHT([Col2], 3)
ELSEIF LEN([Col2]) <8 THEN
REPLACE([Col2], RIGHT([Col2], 5), "") + "," +
REPLACE(RIGHT([Col2],5), RIGHT([Col2], 3), "") + "," +RIGHT([Col2], 3)
ELSE
REPLACE([Col2], RIGHT([Col2], 7), "") + "," +
REPLACE(RIGHT([Col2],7), RIGHT([Col2], 5), "") + "," +
REPLACE(RIGHT([Col2],5), RIGHT([Col2], 3), "") + "," +RIGHT([Col2], 3)
END
this CF will work exactly as desired for upto 9 digits.
Alignment is not a big problem, if considered

SSRS subtract 1 day when using calender

I'm trying to get the correct date from calendar using expression. But with my code, it gives 32 days and I need 31 days. How can I use DateAdd with this?
="Date: " + cstr(left(Parameters!KP2Ky.Value, 4) + "." + Mid(Parameters!KP2Ky.Value, 5, 2)+ "." + Right(Parameters!KP2Ky.Value,2))
I tried but I couldn't figure out how you are getting 32 in your result.
I would make the parameter a DATE type.
For the last day of the month previous to the selected month, you could use
="Date: " & FORMAT(DATEADD("d", 0 - DAY(Parameters!KP2Ky.Value), Parameters!KP2Ky.Value), "yyyy.MM.dd")
The last day of the selected month is a little more complicated:
="Date: " & FORMAT(DATEADD("d", 0 - DAY(DATEADD("M", 1, DATEADD("d", 1 - DAY(Parameters!KP2Ky.Value), Parameters!KP2Ky.Value))), DATEADD("M", 1, DATEADD("d", 1 - DAY(Parameters!KP2Ky.Value), Parameters!KP2Ky.Value))), "yyyy.MM.dd")

Functions with pre millenium dates in q

I've built a function in q such that I can see how many Sunday's fall on the 1st of the month between two dates
\W 1 f3:{[sd;ed] count distinct `week$(sd + til 1 + ed - sd) where (`dd$distinct `week$sd + til 1 + ed - sd)=01}
How can I edit with to work with pre 2000 dates? Can I put a modulus around the negative dates? Or will that redender my function incorrect?
You can also try this:
q) f:{sum 1=mod[`date$a[1] + til 1+(-). a:(0;1<`dd$x)+`month$(y;x);7]}
q) f[2018.01.01;2018.12.31] / 2
q) f[1998.01.02;1999.12.31] / 4

Convert function to Classic ASP

I am using a mailing list program which inserts a date into a web link that is "encoded" so it can't be changed or edited by users.
The format is described as follows:
An eight character string, AABBCCDD,where:
Year = 1980 + HexToInt(BB) / 3
Month = HexToInt(CC) / 7 - 21
Day = HexToInt(DD) / 7 - 5
There is also a checksum included to avoid casual modification:
AA = IntToHex(Year + Month + Day mod 200)
For example 2660BDAF would refer to 20 June, 2012.
Can you help me convert the following to Classic ASP:
CodedDateStr = Request.querystring("Exp")
AYear = 1980 + HexToInt(CodedDateStr[3] + CodedDateStr[4]) / 3
AMonth = HexToInt(CodedDateStr[5] + CodedDateStr[6]) / 7 - 21
ADay = HexToInt(CodedDateStr[7] + CodedDateStr[8]) / 7 - 5
ACheckSum = AYear + AMonth + ADay mod 200
if ACheckSum <> HexToInt(CodedDateStr[1] + CodedDateStr[2]) then
ValidDate = 0
else
ValidDate = 1
end if
AExpiryDate = EncodeDate(ADay, AMonth, AYear)
if Date() > AExpiryDate then
ExpiredOffer = 1
else
ExpiredOffer = 0
end if
....
It looks like the HexToInt equivalent is clng("&h" & hexnumber)
I'm not sure about EncodeDate, i hope it is not something cludgy like CDate(AMonth + "/" + ADay + "/" + AYear)
CLng("&h" & hexnumber) looks like a good method for HexToInt.
For EncodeDate, look at the DateSerial function, which takes a year, month, and day, and returns a Date value.

produce leading 0 in the minutes field

This snippet:
select Datename(hh,DATEADD(HH, -5, [time])) + ':' + Datename(mi,[time])....
will produce:
11:4
But i need the leading '0' in front of the '4'.
You can pad the minutes with leading zeros, and then take the right two characters:
SELECT Datename(hh,DATEADD(HH, -5, [time])) + ':' +
right('00' + Datename(mi,[time]), 2)
declare #hoje datetime = getdate()
select #hoje,format(#hoje,'HH') +':' + format(#hoje,'mm')