Sending an email from Ktor server running on VPS - kotlin
I am trying to send an email from a Ktor application.
When the following code is running on localhost it works fine
val responseEmail = SimpleEmail()
responseEmail.hostName = "smtp.googlemail.com"
responseEmail.setSmtpPort(465)
responseEmail.setAuthenticator(DefaultAuthenticator("email#gmail.com", "pass"))
responseEmail.isSSLOnConnect = true
responseEmail.setFrom("email#gmail.com")
responseEmail.subject = "Subject"
responseEmail.setMsg("msg")
responseEmail.addTo(email)
responseEmail.send()
Even if I set responseEmail.isSSLOnConnect = false still does not work
But when I run the application on a VPS (doesn't have SSL), I get the following exception:
org.apache.commons.mail.EmailException: Sending the email to the following server failed: smtp.googlemail.com:465
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1469)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at org.apache.commons.mail.Email.send(Email.java:1496)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at eu.bbsapps.forgottenfilmsapi.data.modules.AccountManagementModule.forgottenPassword(AccountManagementModu>
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at eu.bbsapps.forgottenfilmsapi.routes.AccountManagementKt$accountManagementRoute$11$1.invokeSuspend(Account>
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at eu.bbsapps.forgottenfilmsapi.routes.AccountManagementKt$accountManagementRoute$11$1.invoke(AccountManagem>
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at eu.bbsapps.forgottenfilmsapi.routes.AccountManagementKt$accountManagementRoute$11$1.invoke(AccountManagem>
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:243)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.util.pipeline.SuspendFunctionGun.proceed(SuspendFunctionGun.kt:113)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.util.pipeline.SuspendFunctionGun.execute(SuspendFunctionGun.kt:133)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.util.pipeline.Pipeline.execute(Pipeline.kt:77)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.routing.Routing.executeResult(Routing.kt:151)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.routing.Routing.interceptor(Routing.kt:35)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.routing.Routing$Feature$install$1.invokeSuspend(Routing.kt:103)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.routing.Routing$Feature$install$1.invoke(Routing.kt)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:243)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.util.pipeline.SuspendFunctionGun.proceed(SuspendFunctionGun.kt:113)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.features.ContentNegotiation$Feature$install$1.invokeSuspend(ContentNegotiation.kt:110)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.features.ContentNegotiation$Feature$install$1.invoke(ContentNegotiation.kt)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:243)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.util.pipeline.SuspendFunctionGun.proceed(SuspendFunctionGun.kt:113)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.features.CallLogging$Feature$install$2.invokeSuspend(CallLogging.kt:139)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.features.CallLogging$Feature$install$2.invoke(CallLogging.kt)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:243)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.util.pipeline.SuspendFunctionGun.proceed(SuspendFunctionGun.kt:113)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.util.pipeline.SuspendFunctionGun.execute(SuspendFunctionGun.kt:133)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.util.pipeline.Pipeline.execute(Pipeline.kt:77)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.server.engine.DefaultEnginePipelineKt$defaultEnginePipeline$2.invokeSuspend(DefaultEnginePipeline>
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.server.engine.DefaultEnginePipelineKt$defaultEnginePipeline$2.invoke(DefaultEnginePipeline.kt)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:243)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.util.pipeline.SuspendFunctionGun.proceed(SuspendFunctionGun.kt:113)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.util.pipeline.SuspendFunctionGun.execute(SuspendFunctionGun.kt:133)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.util.pipeline.Pipeline.execute(Pipeline.kt:77)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.server.netty.NettyApplicationCallHandler$handleRequest$1.invokeSuspend(NettyApplicationCallHandle>
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.server.netty.NettyApplicationCallHandler$handleRequest$1.invoke(NettyApplicationCallHandler.kt)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at kotlinx.coroutines.intrinsics.UndispatchedKt.startCoroutineUndispatched(Undispatched.kt:55)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at kotlinx.coroutines.CoroutineStart.invoke(CoroutineStart.kt:112)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at kotlinx.coroutines.AbstractCoroutine.start(AbstractCoroutine.kt:126)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch(Builders.common.kt:56)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at kotlinx.coroutines.BuildersKt.launch(Unknown Source)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.server.netty.NettyApplicationCallHandler.handleRequest(NettyApplicationCallHandler.kt:43)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.server.netty.NettyApplicationCallHandler.channelRead(NettyApplicationCallHandler.kt:34)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.netty.channel.AbstractChannelHandlerContext.access$600(AbstractChannelHandlerContext.java:61)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.netty.channel.AbstractChannelHandlerContext$7.run(AbstractChannelHandlerContext.java:370)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:164)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:472)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:500)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.server.netty.EventLoopGroupProxy$Companion$create$factory$1$1.run(NettyApplicationEngine.kt:216)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at java.lang.Thread.run(Thread.java:748)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: Caused by: javax.mail.AuthenticationFailedException: 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&>
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: 534-5.7.14 4-yJLa62muQ6uc6fMiBsu1s3xvCVFPJMWkV7YJdO-ZDZWOwlDsYdqEJjjq0uadKzohvM1
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: 534-5.7.14 56A4kwN7LQ9Sg-98Z2lpmlz7gz5ozvN6E4A-kQJ2U_Kk79iX5ziUZy503ETUA2-J>
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: 534-5.7.14 Please log in via your web browser and then try again.
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: 534-5.7.14 Learn more at
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: 534 5.7.14 https://support.google.com/mail/answer/78754 ay38sm4917546wmb.3 - gsmtp
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:932)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:843)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:748)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at javax.mail.Service.connect(Service.java:388)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at javax.mail.Service.connect(Service.java:246)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at javax.mail.Service.connect(Service.java:195)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at javax.mail.Transport.send0(Transport.java:254)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at javax.mail.Transport.send(Transport.java:124)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1459)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: ... 51 common frames omitted
How can I fix this? Is there a way to send an email from Ktor server running on a VPS without SSL.
Things I have done:
I tried using a different port: 465, 587
I have enabled less secure apps
Solution: Authorize access for your IP or client machine using the https://accounts.google.com/DisplayUnlockCaptcha link. That can resolve authentication issues.
Related
Filtering a list based on sed and awk command, multiple delimiter
I need help in this issue. I have some files in a folder and I want to list it, but I want to list only the main Cours-name. -r--r----- 1 saeid azubi 0 Jun 10 14:34 'Gymnastik - Bauch-Beine-Po 01.mp4' -r--r----- 1 saeid azubi 0 Jun 10 14:34 'Gymnastik - Bauch-Beine-Po 02.mp4' -r--r----- 1 saeid azubi 0 Jun 10 14:34 'Gymnastik - Bauch-Beine-Po 03.mp4' -r--r----- 1 saeid azubi 0 Jun 10 14:34 'Gymnastik - Bauch-Beine-Po 04.mp4' -r--r----- 1 saeid azubi 0 Jun 10 14:35 'Gymnastik - Bauch-Beine-Po 05.mp4' -r--r----- 1 saeid azubi 0 Jun 10 14:35 'Gymnastik - Bauch-Beine-Po 06.mp4' -r--r----- 1 saeid azubi 0 Jun 10 14:35 'Gymnastik - Bauch-Beine-Po 07.mp4' -r--r----- 1 saeid azubi 0 Jun 10 14:35 'Gymnastik - Bauch-Beine-Po 08.mp4' -r--r----- 1 saeid azubi 0 Jun 10 14:35 'Gymnastik - Bauch-Beine-Po 09.mp4' -r--r----- 1 saeid azubi 0 Jun 10 14:35 'Gymnastik - Bauch-Beine-Po 10.mp4' -r--r----- 1 saeid azubi 0 Jun 10 14:35 'Gymnastik - Bauch-Beine-Po 11.mp4' -r--r----- 1 saeid azubi 0 Jun 10 14:35 'Gymnastik - Entspannung.mp4' -r--r----- 1 saeid azubi 0 Jun 10 14:35 'Gymnastik - Muskeln wie Stahl 01.mp4' -r--r----- 1 saeid azubi 0 Jun 10 14:35 'Gymnastik - Muskeln wie Stahl 02.mp4' -r--r----- 1 saeid azubi 0 Jun 10 14:35 'Gymnastik - Muskeln wie Stahl 03.mp4' -r--r----- 1 saeid azubi 0 Jun 10 14:35 'Gymnastik - Muskeln wie Stahl 04.mp4' -r--r----- 1 saeid azubi 0 Jun 10 14:35 'Gymnastik - Muskeln wie Stahl 05.mp4' -r--r----- 1 saeid azubi 0 Jun 10 14:35 'Gymnastik - Muskeln wie Stahl 06.mp4' -r--r----- 1 saeid azubi 0 Jun 10 14:35 'Gymnastik - Muskeln wie Stahl 07.mp4' -r--r----- 1 saeid azubi 0 Jun 10 14:34 'Gymnastik - RückenFit 01.mp4' -r--r----- 1 saeid azubi 0 Jun 10 14:34 'Gymnastik - RückenFit 02.mp4' -r--r----- 1 saeid azubi 0 Jun 10 14:34 'Gymnastik - RückenFit 03.mp4' -r--r----- 1 saeid azubi 0 Jun 10 14:34 'Gymnastik - RückenFit 04.mp4' -r--r----- 1 saeid azubi 0 Jun 10 14:34 'Gymnastik - RückenFit 05.mp4' -r--r----- 1 saeid azubi 0 Jun 10 14:34 'Gymnastik - Stretching 01.mp4' -r--r----- 1 saeid azubi 0 Jun 10 14:34 'Gymnastik - Stretching 02.mp4' -r--r----- 1 saeid azubi 0 Jun 10 14:34 'Gymnastik - Stretching 03.mp4' here The output should be like this : Bauch-Beine-Po Entspannung Muskeln wie Stahl RückenFit Stretching I tried this code but its not what exactly what I want: ls -l | awk -F'[ .\$]' '{print $11 }' | uniq Thanks for helping in advance
You might also use awk. Use - as the field separator and remove the optional space and digits followed by the extension from the last field. For example ls -A1 | awk -F ' - ' '{sub(/( [0-9]+)?\.[^.]+$/, "", $NF);print $NF}' | sort | uniq In the example code: $NF is the last field, which will first be changed by sub and then printed The pattern ( [0-9]+)?\.[^.]+$ matches an optional space and 1+ digits followed by a dot and any char except a dot until the end of the string
here is finelly what i wanted ls -lrtA |cut -f11-13 -d ' '| sed 's/[0-9]*\.[^.]*$//'| sort | uniq
how to wite the date fuctionality for below output
I want the output like this for whole year: Jan 01 - Jan 14 - 2021 Jan 15 - Jan 28 - 2021 Jan 29 - Feb 11 - 2021 Feb 12 - Feb 25 - 2021 Feb 26 - Mar 11 - 2021 Mar 12 - Mar 25 - 2021 Mar 26 - Apr 08 - 2021 Apr 09 - Apr 22 - 2021 Apr 22 - May 06 - 2021 May 07 - May 20 - 2021 May 21 - Jun 03 - 2021 Jun 04 - Jun 17 - 2021 Jun 18 - Jul 01 - 2021 Jul 02 - Jul 15 - 2021 Jul 16 - Jul 29 - 2021 Jul 30 - Aug 12 - 2021 Aug 13 - Aug 26 - 2021 Aug 27 - Sep 09 - 2021 Sep 10 - Sep 23 - 2021 Sep 24 - Oct 07 - 2021 Oct 08 - Oct 21 - 2021 Oct 22 - Nov 04 - 2021 Nov 05 - Nov 18 - 2021 Dec 19 - Dec 02 - 2021 Dec 03 - Dec 16 - 2021 Dec 17 - Dec 30 - 2021
One option uses generate_series(): select d.dt dt_start, d.dt + '13 days'::interval dt_end from generate_series( date_trunc('year', current_date), date_trunc('year', current_date) + '1 year'::interval - '13 days'::interval, '14 days'::interval ) d(dt) order by dt_start
SELECT to_char(d, 'Mon DD - ') || to_char(d + INTERVAL '13 days', 'Mon DD - YYYY') FROM generate_series( TIMESTAMP '2021-01-01', TIMESTAMP '2021-12-17', INTERVAL '14 days' ) AS d; ?column? ------------------------ Jan 01 - Jan 14 - 2021 Jan 15 - Jan 28 - 2021 Jan 29 - Feb 11 - 2021 Feb 12 - Feb 25 - 2021 Feb 26 - Mar 11 - 2021 Mar 12 - Mar 25 - 2021 Mar 26 - Apr 08 - 2021 Apr 09 - Apr 22 - 2021 Apr 23 - May 06 - 2021 May 07 - May 20 - 2021 May 21 - Jun 03 - 2021 Jun 04 - Jun 17 - 2021 Jun 18 - Jul 01 - 2021 Jul 02 - Jul 15 - 2021 Jul 16 - Jul 29 - 2021 Jul 30 - Aug 12 - 2021 Aug 13 - Aug 26 - 2021 Aug 27 - Sep 09 - 2021 Sep 10 - Sep 23 - 2021 Sep 24 - Oct 07 - 2021 Oct 08 - Oct 21 - 2021 Oct 22 - Nov 04 - 2021 Nov 05 - Nov 18 - 2021 Nov 19 - Dec 02 - 2021 Dec 03 - Dec 16 - 2021 Dec 17 - Dec 30 - 2021 (26 rows)
Add a day to a predefined date SQL Server 2008 R2
My question is very simple, I am making a query which I intend to make an insertion from two dates to a temp Table. That is, 2020-04-01 00:00:00 and 20-04-01 23:59:00, hence the number of days you need increases. I am doing it this way E.G: DECLARE #StartDate DATETIME = '2020-04-01' DECLARE #EndDate DATETIME = '2020-04-01' SET #StartDate = #StartDate + '00:00:00' SET #EndDate = #EndDate + '23:59:00' WHILE (#StartDate <= #EndDate) BEGIN --Insert into a temp table set #StartDate = DATEADD(DAY, 1, #StartDate); set #EndDate = DATEADD(day, 1, #EndDate); print #StartDate print #EndDate END; , however it goes into a repetitive cycle and does not stop. Besides, it is not only respecting the increment of the day that I need. Can someone help me with this please. I want to get to this: Initial date: 2020-04-01 00:00:00. End Date: 2020-04-01 23:59:00. Starting day increment 2020-04-02 00:00:00 End Date Increment: 2020-04-02 23:59:00. My query is throwing this at me: Apr 2 2020 12:00AM Apr 2 2020 11:59PM Apr 3 2020 12:00AM Apr 3 2020 11:59PM Apr 4 2020 12:00AM Apr 4 2020 11:59PM Apr 5 2020 12:00AM Apr 5 2020 11:59PM Apr 6 2020 12:00AM Apr 6 2020 11:59PM Apr 7 2020 12:00AM Apr 7 2020 11:59PM Apr 8 2020 12:00AM Apr 8 2020 11:59PM Apr 9 2020 12:00AM Apr 9 2020 11:59PM Apr 10 2020 12:00AM Apr 10 2020 11:59PM Apr 11 2020 12:00AM Apr 11 2020 11:59PM Apr 12 2020 12:00AM Apr 12 2020 11:59PM Apr 13 2020 12:00AM Apr 13 2020 11:59PM Apr 14 2020 12:00AM Apr 14 2020 11:59PM Apr 15 2020 12:00AM Apr 15 2020 11:59PM Apr 16 2020 12:00AM Apr 16 2020 11:59PM Apr 17 2020 12:00AM Apr 17 2020 11:59PM Apr 18 2020 12:00AM Apr 18 2020 11:59PM Apr 19 2020 12:00AM Apr 19 2020 11:59PM Apr 20 2020 12:00AM Apr 20 2020 11:59PM Apr 21 2020 12:00AM Apr 21 2020 11:59PM Apr 22 2020 12:00AM Apr 22 2020 11:59PM Apr 23 2020 12:00AM Apr 23 2020 11:59PM Apr 24 2020 12:00AM Apr 24 2020 11:59PM Apr 25 2020 12:00AM Apr 25 2020 11:59PM Apr 26 2020 12:00AM Apr 26 2020 11:59PM Apr 27 2020 12:00AM Apr 27 2020 11:59PM Apr 28 2020 12:00AM Apr 28 2020 11:59PM Apr 29 2020 12:00AM Apr 29 2020 11:59PM Apr 30 2020 12:00AM Apr 30 2020 11:59PM May 1 2020 12:00AM May 1 2020 11:59PM May 2 2020 12:00AM May 2 2020 11:59PM May 3 2020 12:00AM May 3 2020 11:59PM May 4 2020 12:00AM May 4 2020 11:59PM May 5 2020 12:00AM May 5 2020 11:59PM May 6 2020 12:00AM May 6 2020 11:59PM May 7 2020 12:00AM May 7 2020 11:59PM May 8 2020 12:00AM May 8 2020 11:59PM May 9 2020 12:00AM May 9 2020 11:59PM May 10 2020 12:00AM May 10 2020 11:59PM May 11 2020 12:00AM May 11 2020 11:59PM May 12 2020 12:00AM May 12 2020 11:59PM May 13 2020 12:00AM May 13 2020 11:59PM May 14 2020 12:00AM May 14 2020 11:59PM May 15 2020 12:00AM May 15 2020 11:59PM May 16 2020 12:00AM May 16 2020 11:59PM May 17 2020 12:00AM May 17 2020 11:59PM May 18 2020 12:00AM May 18 2020 11:59PM May 19 2020 12:00AM May 19 2020 11:59PM May 20 2020 12:00AM May 20 2020 11:59PM May 21 2020 12:00AM May 21 2020 11:59PM May 22 2020 12:00AM May 22 2020 11:59PM May 23 2020 12:00AM May 23 2020 11:59PM May 24 2020 12:00AM May 24 2020 11:59PM May 25 2020 12:00AM May 25 2020 11:59PM May 26 2020 12:00AM May 26 2020 11:59PM May 27 2020 12:00AM May 27 2020 11:59PM May 28 2020 12:00AM May 28 2020 11:59PM May 29 2020 12:00AM May 29 2020 11:59PM May 30 2020 12:00AM May 30 2020 11:59PM May 31 2020 12:00AM May 31 2020 11:59PM Jun 1 2020 12:00AM Jun 1 2020 11:59PM Jun 2 2020 12:00AM Jun 2 2020 11:59PM Jun 3 2020 12:00AM Jun 3 2020 11:59PM Jun 4 2020 12:00AM Jun 4 2020 11:59PM Jun 5 2020 12:00AM Jun 5 2020 11:59PM Jun 6 2020 12:00AM Jun 6 2020 11:59PM Jun 7 2020 12:00AM Any suggestion? Where is my mistake ?. Thank you.
Use a recursive CTE: DECLARE #StartDate DATETIME = '2020-04-01'; DECLARE #EndDate DATETIME = '2020-04-01'; with dates as ( select #startdate as startdate, dateadd(minute, -1, dateadd(day, 1, #startdate)) as enddate union all select dateadd(day, 1, startdate), dateadd(day, 1, enddate) from dates where startdate < #enddate ) select * from dates option (maxrecursion 0); Here is a db<>fiddle.
How to get the data in decline pattern/trending using Oracle SQL?
How can I get the result only for those monthly data in decline pattern/trending ? And the data as below; ID JAN FEB MAR APR MAY JUN 112 50 45 40 35 30 20 113 30 30 30 30 30 30 114 20 25 20 20 20 20 115 45 50 60 60 30 30 Expected output ; ID JAN FEB MAR APR MAY JUN 112 50 45 40 35 30 20 115 45 50 60 60 30 30 Thanks !
SELECT * FROM your_table WHERE JAN > JUN or SELECT * FROM your_table WHERE JAN > FEB AND FEB > MAR AND MAR > APR AND APR > MAY AND MAY > JUN
It appears that what you're looking for is SELECT * FROM SOME_TABLE WHERE JAN > FEB OR FEB > MAR OR MAR > APR OR APR > MAY OR MAY > JUN Best of luck.
Split data datetime wise in SQL Server
I have a requirement to group the data day-wise from 8:00 AM today to 7:59 AM the next day as 1 full day. For example from the below table: SoldDatetime Qty Dec 20,2015 12:05 AM 1 Dec 20,2015 1:05 AM 2 Dec 20,2015 7:05 AM 3 Dec 20,2015 8:05 AM 4 Dec 20,2015 10:05 AM 5 Dec 20,2015 11:05 PM 6 Dec 21,2015 12:05 AM 7 Dec 21,2015 1:05 AM 8 Dec 21,2015 7:05 AM 9 Dec 21,2015 8:05 AM 10 Dec 21,2015 10:05 AM 11 Dec 21,2015 11:05 PM 12 Dec 22,2015 12:05 AM 13 Dec 22,2015 1:05 AM 14 Dec 22,2015 7:05 AM 15 Dec 22,2015 8:05 AM 16 Dec 22,2015 10:05 AM 17 Dec 22,2015 11:05 PM 18 Dec 23,2015 12:05 AM 19 Dec 23,2015 1:05 AM 20 Dec 23,2015 7:05 AM 21 Dec 23,2015 8:05 AM 22 Dec 23,2015 10:05 AM 23 Dec 23,2015 11:05 PM 24 Dec 24,2015 12:05 AM 25 Dec 24,2015 1:05 AM 26 Dec 24,2015 7:05 AM 27 Dec 24,2015 8:05 AM 28 Dec 24,2015 10:05 AM 29 Dec 24,2015 11:05 PM 30 Dec 25,2015 12:05 AM 31 Dec 25,2015 1:05 AM 32 Dec 25,2015 7:05 AM 33 Dec 25,2015 8:05 AM 34 Dec 25,2015 10:05 AM 35 Dec 25,2015 11:05 PM 36 If I have to run a query to filter for date range Dec 21,2015 8:00 AM to Dec 25,2015 7:59 AM. I need the output in below format SoldDateRange Qty Dec 20,2015 8:00 AM - Dec 21,2015 7:59AM 39 Dec 21,2015 8:00 AM - Dec 22,2015 7:59AM 75 Dec 22,2015 8:00 AM - Dec 23,2015 7:59AM 111 Dec 23,2015 8:00 AM - Dec 24,2015 7:59AM 147 Dec 24,2015 8:00 AM - Dec 25,2015 7:59AM 183 Can someone help with the SQL query for this? Thanks in advance
Here is a query which should give you the full output you mentioned in your original question. SELECT CONCAT(CONVERT(VARCHAR, t.adjustedTime, 107), ' 8:00 AM', ' - ', CONVERT(VARCHAR, DATEADD(DAY, 1, t.adjustedTime), 107), ' 7:59AM'), t.Qty FROM ( SELECT CAST(DATEADD(HOUR, -8, SoldDateTime) AS DATE) AS adjustedTime, SUM(Qty) AS Qty FROM yourTable GROUP BY CAST(DATEADD(HOUR, -8, SoldDateTime) AS DATE) ) t ORDER BY t.adjustedTime
You can do this by subtracting 8 hours. The basic idea is: select cast(dateadd(hour, -8, solddaterange) as date) as SoldDate, sum(qty) from t group by cast(dateadd(hour, -8, solddaterange) as date) order by SoldDate; EDIT: You could use the same idea. For instance: where cast(dateadd(hour, -8, solddaterange) as date) = '2016-01-01'