| |

VerySource

 Forgot password?
 Register
Search
Author: 流星飞舞

Small question, how to get the number of days from a certain date in SQL?

[Copy link]

0

Threads

119

Posts

67.00

Credits

Newbie

Rank: 1

Credits
67.00

 China

Post time: 2020-4-8 13:00:02
| Show all posts
SELECT datediff (day, 'date to be compared', the field name to be compared by the database)
FROM table name
WHERE condition
Reply

Use magic Report

0

Threads

9

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-4-8 17:45:01
| Show all posts
Select
DATEDIFF (Day,
Convert (Datetime,
substring (
convert (varchar, getdate (), 112)
, 1,4)
+ substring (
Cast (Birthday As Varchar)
, 5, len (Birthday)
), 112),
getdate ()
)
Reply

Use magic Report

0

Threads

52

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 China

Post time: 2020-4-9 18:15:01
| Show all posts
SELECT DATEDIFF ([day], CONVERT (datetime, SUBSTRING (CONVERT (varchar, GETDATE (),
      112), 1, 4) + SUBSTRING (CONVERT (varchar, BirthDate, 112), 5, LEN (BirthDate)), 112),
      GETDATE ()) AS Expr1
FROM Employees
Reply

Use magic Report

0

Threads

26

Posts

21.00

Credits

Newbie

Rank: 1

Credits
21.00

 China

Post time: 2020-4-9 21:00:02
| Show all posts
Hey, LZ should strengthen exercise, analyze more, and learn to make inferences.

Birthday is your DateTime field, my example just dealt with the case of strings, you can put two
'20000101' string
Replace with
convert (varchar, Birthday, 112)

Convert the date to a string with convert and then process
Reply

Use magic Report

0

Threads

52

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 China

Post time: 2020-4-9 21:45:01
| Show all posts
I did n’t look at the specific calculations. I do n’t know if the logic is correct or not. I just converted the datetime type and asked lz to compare it myself.
Reply

Use magic Report

2

Threads

7

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

 Author| Post time: 2020-4-23 10:15:01
| Show all posts
Thank you for your advice, andshygzaagain
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list