| |

VerySource

 Forgot password?
 Register
Search
View: 1450|Reply: 10

Ask a very simple SQL how to deal with strings?

[Copy link]

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-3-15 17:00:01
| Show all posts |Read mode
The field tdate in the table is of type varchar (50)

The content looks like this
2017-01-23
2017-12-15

How to turn it into

As long as the year and month (the year is the same), add a 0 after the month
For example, the two records above should look like this:
2017001
2017012
Like this?
Reply

Use magic Report

0

Threads

88

Posts

55.00

Credits

Newbie

Rank: 1

Credits
55.00

 China

Post time: 2020-6-14 14:15:02
| Show all posts
select replace(left('2017-01-23',7),'-','0')
Reply

Use magic Report

0

Threads

114

Posts

69.00

Credits

Newbie

Rank: 1

Credits
69.00

 China

Post time: 2020-6-14 14:30:01
| Show all posts
select tdate=left(tdate,4)+'0'+substring(tdate,6,2) from tablename
Reply

Use magic Report

0

Threads

211

Posts

108.00

Credits

Newbie

Rank: 1

Credits
108.00

 Gabon

Post time: 2020-6-14 14:45:01
| Show all posts
select rtrim(year(tdate))+right('000'+rtrim(month(tdate)), 3) from tbName
Reply

Use magic Report

0

Threads

30

Posts

25.00

Credits

Newbie

Rank: 1

Credits
25.00

 China

Post time: 2020-6-14 15:45:01
| Show all posts
select convert(varchar(4),getdate(),112)+'0'+substring(convert(varchar(6),getdate(),112),5,2)
Reply

Use magic Report

0

Threads

126

Posts

73.00

Credits

Newbie

Rank: 1

Credits
73.00

 China

Post time: 2020-6-14 17:00:01
| Show all posts
select left(tdate,4)+'0'+substring(tdate,5,2) from..
Reply

Use magic Report

0

Threads

126

Posts

73.00

Credits

Newbie

Rank: 1

Credits
73.00

 China

Post time: 2020-6-14 19:15:01
| Show all posts
select left(tdate,4)+'0'+substring(tdate,6,2) from..
Reply

Use magic Report

0

Threads

88

Posts

55.00

Credits

Newbie

Rank: 1

Credits
55.00

 China

Post time: 2020-6-14 19:45:01
| Show all posts
select replace(left(column_name,7),'-','0') from table_name
Reply

Use magic Report

0

Threads

100

Posts

53.00

Credits

Newbie

Rank: 1

Credits
53.00

 China

Post time: 2020-6-14 21:00:01
| Show all posts
select left(tdate,4)+'0'+substring(tdate,6,2)
Reply

Use magic Report

0

Threads

40

Posts

29.00

Credits

Newbie

Rank: 1

Credits
29.00

 China

Post time: 2020-6-14 23:30:01
| Show all posts
select datepart(yy,tdate)+'0'+datepart(mm,tdate) col from tablename
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