Programlama yapalım ve Öğrenelim. - Delphi Eğitim108
  Ana Sayfa
  .NET Eğitim Notları
  Visual C# .NET Örnek Kodları
  VisualBasic.NET Örnek Kodları
  J# Örnekleri
  ASP.NET Örnek Kodları
  Delphi Eğitim
  => Delphi Eğitim1
  => Delphi Eğitim2
  => Delphi Eğitim3
  => Delphi Eğitim4
  => Delphi Eğitim5
  => Delphi Eğitim6
  => Delphi Eğitim7
  => Delphi Eğitim8
  => Delphi Eğitim9
  => Delphi Eğitim10
  => Delphi Eğitim11
  => Delphi Eğitim13
  => Delphi Eğitim14
  => Delphi Eğitim15
  => Delphi Eğitim16
  => Delphi Eğitim17
  => Delphi Eğitim18
  => Delphi Eğitim19
  => Delphi Eğitim20
  => Delphi Eğitim21
  => Delphi Eğitim22
  => Delphi Eğitim23
  => Delphi Eğitim24
  => Delphi Eğitim25
  => Delphi Eğitim26
  => Delphi Eğitim27
  => Delphi Eğitim28
  => Delphi Eğitim29
  => Delphi Eğitim30
  => Delphi Eğtim31
  => Delphi Eğitim32
  => Delphi Eğitim33
  => Delphi Eğitim34
  => Delphi Eğitim35
  => Delphi Eğitim36
  => Delphi Eğitim37
  => Delphi Eğitim38
  => Delphi Eğitim39
  => Delphi Eğitim40
  => Delphi Eğitim41
  => Delphi Eğitim42
  => Delphi Eğitim43
  => Delphi Eğitim44
  => Delphi Eğitim45
  => Delphi Eğitim46
  => Delphi Eğitim47
  => Delphi Eğitim48
  => Delphi Eğitim49
  => Delphi Eğitim50
  => Delphi Eğitim51
  => Delphi Eğitim52
  => Delphi Eğitim53
  => Delphi Eğitim54
  => Delphi Eğitim55
  => Delphi Eğitim56
  => Delphi Eğitim57
  => Delphi Eğitim58
  => Delphi Eğitim59
  => Delphi Eğitim60
  => Delphi Eğitim61
  => Delphi Eğitim62
  => Delphi Eğitim63
  => Delphi Eğitim64
  => Delphi Eğitim65
  => Delphi Eğitim66
  => Delphi Eğitim67
  => Delphi Eğitim68
  => Delphi Eğitim69
  => Delphi Eğitim70
  => Delphi Eğitim71
  => Delphi Eğitim72
  => Delphi Eğitim73
  => Delphi Eğitim74
  => Delphi Eğitim75
  => Delphi Eğitim76
  => Delphi Eğitim77
  => Delphi Eğitim78
  => Delphi Eğitim79
  => Delphi Eğitim80
  => Delphi Eğitim81
  => Delphi Eğitim82
  => Delphi Eğitim83
  => Delphi Eğitim84
  => Delphi Eğitim85
  => Delphi Eğitim86
  => Delphi Eğitim87
  => Delphi Eğitim88
  => Delphi Eğitim89
  => Delphi Eğitim90
  => Delphi Eğitim91
  => Delphi Eğitim92
  => Delphi Eğitim93
  => Delphi Eğitim94
  => Delphi Eğitim95
  => Delphi Eğitim96
  => Delphi Eğitim97
  => Delphi Eğitim98
  => Delphi Eğitim99
  => Delphi Eğitim100
  => Delphi Eğitim101
  => Delphi Eğitim102
  => Delphi Eğitim103
  => Delphi Eğitim104
  => Delphi Eğitim105
  => Delphi Eğitim106
  => Delphi Eğitim107
  => Delphi Eğitim108
  => Delphi Eğitim109
  => Delphi Eğitim110
  => Delphi Eğitim111
  => Delphi Eğitim112
  => Delphi Eğitim113
  => Delphi Eğitim114
  => Delphi Eğitim115
  => Delphi Eğitim116
  => Delphi Eğitim117
  => Delphi Eğitim118
  => Delphi Eğitim119
  => Delphi Eğitim120
  => Delphi Eğitim121
  => Delphi Eğitim122
  => Delphi Eğitim123
  => Delphi Eğitim124
  => Delphi Eğitim125
  => Delphi Eğitim126
  => Delphi Eğitim127
  => Delphi Eğitim128
  => Delphi Eğitim129
  => Delphi Eğitim130
  => Delphi Eğitim131
  => Delphi Eğitim132
  => Delphi Eğitim133
  => Delphi Eğitim134
  => Delphi Eğitim135
  => Delphi Eğitim136
  => Delphi Eğitim137
  => Delphi Eğitim138
  => Delphi Eğitim139
  => Delphi Eğitim140
  => Delphi Eğitim141
  => Delphi Eğitim142
  => Delphi Eğitim143
  => Delphi Eğitim144
  => Delphi Eğitim145
  => Delphi Eğitim146
  => Delphi eğitim147
  => Delphi Eğitim148
  => Delphi Eğitim149
  => Delphi Eğitim150
  => Delphi Eğitim151
  => Delphi Eğitim152
  => Delphi Eğitim153
  => Delphi Eğitim154
  => Delphi Eğitim155
  => Delphi Eğitim156
  => Delphi Eğitim157
  => Delphi Eğitim158
  => Delphi Eğitim159
  => Delphi Eğitim160
  => Delphi Eğitim161
  => Delphi Eğitim162
  => Delphi Eğitim164
  => Delphi Eğitim165
  => Delphi Eğitim166
  => Delphi Eğitim167
  => Delphi Eğitim168
  => Delphi Eğitim169
  => Delphi Eğitim170
  => Delphi Eğitim171
  => Delphi Eğitim172
  => Delphi Eğitim173
  => Delphi Eğitim174
  => Delphi Eğitim175
  => Delphi Eğitim176
  => Delphi Eğitim177
  => Delphi Eğitim178
  => Delphi Eğitim179
  => Delphi Eğitim180
  => Delphi Eğitim181
  => Delphi Eğitim182
  => Delphi Eğitim183
  => Delphi Eğitim184
  => Delphi Eğitim185
  => Delphi Eğitim186
  => Delphi Eğitim187
  => Delphi Eğitim188
  => Delphi Eğitim189
  => Delphi Eğitim190
  => Delphi Eğitim191
  => Delphi Eğitim192
  => Delphi Eğitim193
  => Delphi Eğitim194
  => Delphi Eğitim195
  => Delphi Eğitim196
  => Delphi Eğitim197
  => Delphi Eğitim198
  => Delphi Eğitim199
  => Delphi Eğitim200
  => Delphi Eğitim201
  => Delphi Eğitim202
  => Delphi Eğitim203
  => Delphi Eğitim204
  => Delphi Eğitim205
  => Delphi Eğitim206
  => Delphi Eğitim207
  => Delphi Eğitim208
  => Delphi Eğitim209
  => Delphi Eğitim210
  => Delphi Eğitim211
  => Delphi Eğitim212
  => Delphi Eğitim213
  => Delphi Eğitim214
  => Delphi Eğitim215
  => Delphi Eğitim216
  => Delphi Eğitim217
  => Delphi Eğitim218
  => Delphi Eğitim219
  => Delphi Eğitim220
  => Delphi Eğitim221
  => Delphi Eğitim222
  => Delphi Eğitim223
  => Delphi Eğitim224
  => Delphi Eğitim225
  => Delphi Eğitim226
  => Delphi Eğitim227
  => Delphi Eğitim228
  => Delphi Eğitim229
  => Delphi Eğitim230
  => Delphi Eğitim231
  => Delphi Eğitim232
  => Delphi Eğitim233
  => Delphi Eğitim234
  => Delphi Eğitim235
  => Delphi Eğitim236
  => Delphi Eğitim237
  => Delphi Eğitim238
  => Delphi Eğitim239
  => Delphi Eğitim240
  => Delphi Eğitim241
  => Delphi Eğitim242
  İletişim

sql server dogum gunu bulma

use pubs

 

 

go

 

---------------- Inserting only the time part into a datetime column --------------

/* Creating a Test Table */

Create Table MyDateTest99

(

 DateColumn datetime

)

go

 

 

/* Inserting the test value into the table */

insert into MyDateTest99 values ('10:00 AM')

go

 

 

/* Selecting the result */

select DateColumn from MyDateTest99

go

 

 

/* Performing Cleanup */

drop table MyDateTest99

go

 

 

---------------- Inserting only the date part into a datetime column --------------

use pubs

 

go

 

/* Creating a Test Table */

Create Table MyDateTest99

(

 DateColumn datetime

)

go

 

 

/* Inserting the test value into the table */

insert into MyDateTest99 values ('January 1, 2000')

go

 

 

/* Selecting the result */

select DateColumn from MyDateTest99

go

 

 

/* Performing Cleanup */

drop table MyDateTest99

go

 

 

 

 

 

So, the most common question that is asked is:

Q: How do I get SQL Server to return only the Date component or only the Time component from the datetime data type?

A: By using the Convert function. The syntax for using the convert function is:

 

CONVERT ( data_type [ ( length ) ] , expression [ , style ] )

 

By varying the datatype and length, we can get the desired component. Moreover, the style argument in the Convert function is provided exclusively for use with date and time data. Some sample code illustrating the same is as follows:

 

use pubs

 

 

go

 

---------------- Selecting only the date part from a datetime column --------------

/* Creating a Test Table */

Create Table MyDateTest99

(

 DateColumn datetime

)

go

 

 

/* Inserting the test value into the table */

insert into MyDateTest99 values (getdate())

go

 

 

/* Selecting the result */

select convert(varchar,DateColumn,101) from MyDateTest99

go

 

 

/* Performing Cleanup */

drop table MyDateTest99

go

 

 

use pubs

 

 

go

 

---------------- Selecting only the date part from a datetime column --------------

/* Creating a Test Table */

Create Table MyDateTest99

(

 DateColumn datetime

)

go

 

 

/* Inserting the test value into the table */

insert into MyDateTest99 values (getdate())

go

 

 

/* Selecting the result */

select convert(varchar,DateColumn,108) from MyDateTest99

go

 

 

/* Performing Cleanup */

drop table MyDateTest99

go

 

 

The list of styles that can be used are:

 

Style ID

 Style Type

 

0 or 100  mon dd yyyy hh:miAM (or PM)

101 mm/dd/yy

102 yy.mm.dd

103 dd/mm/yy

104 dd.mm.yy

105 dd-mm-yy

106 dd mon yy

107 Mon dd, yy

108 hh:mm:ss

9 or 109  mon dd yyyy hh:mi:ss:mmmAM (or PM)

110 mm-dd-yy

111 yy/mm/dd

112 yymmdd

13 or 113  dd mon yyyy hh:mm:ss:mmm(24h)

114 hh:mi:ss:mmm(24h)

20 or 120  yyyy-mm-dd hh:mi:ss(24h)

21 or 121  yyyy-mm-dd hh:mi:ss.mmm(24h)

126 yyyy-mm-dd Thh:mm:ss.mmm(no spaces)

130 dd mon yyyy hh:mi:ss:mmmAM

131 dd/mm/yy hh:mi:ss:mmmAM

 

These styles are the format of input to be used when converting character data into datetime and format of output while converting datetime data into characters:

 

use pubs

 

 

go

 

---------------- Example for the demonstration of use of style while input of data--------------

/* Creating a Test Table */

Create Table MyDateTest99

(

 DateColumn datetime

)

go

 

 

/* Inserting the test values into the table */

-- Inserting in US format

insert into MyDateTest99  select convert(datetime,'05/08/2004',101)

-- Inserting in UK format

insert into MyDateTest99  select convert(datetime,'08/05/2004',103)

-- Inserting in ISO Format

insert into MyDateTest99  select convert(datetime,'20040508',112)

go

 

 

/* Selecting the result */

select DateColumn from MyDateTest99

go

 

 

/* Performing Cleanup */

drop table MyDateTest99

go

 

 

use pubs

 

 

go

 

---------------- Example for the demonstration of use of style while output of data--------------

 

/* Creating a Test Table */

Create Table MyDateTest99

(

 DateColumn datetime

)

go

/* Inserting the test values into the table */

insert into MyDateTest99  select convert(datetime,'05/08/2004',101)

 

go

 

/* Selecting the result */

-- In US Format

select convert(varchar,DateColumn,101) from MyDateTest99

-- In UK Format

select convert(varchar,DateColumn,103) from MyDateTest99

-- In ISO Format

select convert(varchar,DateColumn,112) from MyDateTest99

 

go

/* Performing Cleanup */

drop table MyDateTest99

go

 

 

 

 

Some other functions that can be used for various purposes are DATEADD, DATEDIFF, DATENAME, DATEPART, DAY, GETDATE, MONTH, and YEAR. Here's some further detail on these functions as well as a code sample showing their use:

 

Dateadd: Returns a new datetime value based on adding an interval to the specified date.

 

Syntax: DATEADD ( datepart, number, date )

 

Datediff: Returns the number of date and time boundaries crossed between two specified dates.

 

Syntax: DATEDIFF ( datepart, startdate, enddate )

 

Datename: Returns a character string representing the specified datepart of the specified date.

 

Syntax: DATENAME ( datepart, date )

 

Datepart: Returns an integer representing the specified datepart of the specified date.

 

Syntax: DATEPART ( datepart, date )

 

Day: Returns an integer representing the day datepart of the specified date.

 

Syntax: DAY ( date )

 

Getdate: Returns the current system date and time in the Microsoft® SQL Serverstandard internal format for datetime values.

 

Syntax: GETDATE ( )

 

Month: Returns an integer that represents the month part of a specified date.

 

Syntax: MONTH ( date )

 

Year: Returns an integer that represents the year part of a specified date.

 

Syntax: YEAR ( date )

 

declare @datevar datetime

select @datevar = getdate()

 

/*Example for getdate() : getting current datetime*/

select getdate() [Current Datetime]

 

/*Example for dateadd : getting date 7 days from current datetime*/

select dateadd(dd, 7, @datevar) [Date 7 days from now]

 

/*Example for datediff : getting no of days passed since 01-01-2004*/

select datediff(dd,'20040101',@datevar) [No of days since 01-01-2004]

 

/*Example for datename : getting month name*/

select datename(mm, @datevar) [Month Name]

 

/*Example for datepart : getting week from date*/

select datepart(wk, @datevar ) [Week No]

 

/*Example for day : getting day part of date*/

select day (@datevar) [Day]

 

/*Example for month : getting month part of date*/

select month(@datevar) [Month]

 

/*Example for year : getting year part of date*/

select year(@datevar) [Year]

 

 

 

Now I will provide you with some code samples which you can use for various tasks. I will try to include as many examples I can think of, but this list is not exhaustive:

 

1. To find the first day of a month:

 

select dateadd(dd,-(day(DateColumn)-1),DateColumn)

 

 

2. To find last day of a month:

 

select dateadd(dd,-(day(dateadd(mm,1,DateColumn))),dateadd(mm,1,DateColumn))

 

 

3. To find birthdays in next seven days:

 

use pubs

 

 

go

 

/* Creating a Test Table */

Create Table MyDateTest99

(

 Birthday datetime

)

go

/* Inserting the test value into the table */

insert into MyDateTest99 select convert (varchar(10),'19780129',120)

insert into MyDateTest99 select convert (varchar(10),'19670821',120)

insert into MyDateTest99 select convert (varchar(10),'19910112',120)

insert into MyDateTest99 select convert (varchar(10),dateadd(dd,2,getdate()),120)

insert into MyDateTest99 select convert (varchar(10),'19791016',120)

 

 

go

/* Selecting the result */

select

 Birthday

from

 MyDateTest99

where

 datediff

  (

   dd

   ,convert(datetime,'1900/'+cast(month(getdate()) as varchar)+'/'+cast (day(getdate()) as varchar),111)

   ,convert(datetime,'1900/'+cast(month(Birthday) as varchar)+'/'+cast (day(Birthday) as varchar),111)

  ) between 0 and 7

go

/* Performing Cleanup */

drop table MyDateTest99

go

 

 

4. Number of hours until weekend, that is until Friday at 5 PM (my favorite):

 

use pubs

 

 

go

 

Create  function udf_Time_to_Weekend (@d1 datetime) returns datetime

as

begin

 declare @d2 datetime

 select @d2 =  case  when (datepart(hh,dateadd(dd,(7-datepart(dw,@d1)),@d1)) >= 17 and 7-datepart(dw,@d1) = 0)

    then  dateadd(hh,17,convert(varchar(10),dateadd(dd,7,@d1),101))

    else dateadd(hh,17,convert(varchar(10),dateadd(dd,(7-datepart(dw,@d1)),@d1),101))

   end

 return  @D2

END

go

Create   procedure HoursTillWeekend as

set datefirst 6

select DATEDIFF(MI,GETDATE(),dbo.udf_Time_to_Weekend(getdate()))/60 "Hours Till Weekend"

go

exec HoursTillWeekend

go

drop procedure HoursTillWeekend

go

drop function udf_Time_to_Weekend

go

 

 

5. First and last days of quarter, in which a date falls:

 

use pubs

go

/* Creating a Test Table */

Create Table MyDateTest99

(

 DateColumn datetime

)

go

/* Inserting the test value into the table */

insert into MyDateTest99 select convert (varchar(10),'19780129',120)

insert into MyDateTest99 select convert (varchar(10),'19670821',120)

insert into MyDateTest99 select convert (varchar(10),'19910112',120)

insert into MyDateTest99 select convert (varchar(10),'19791016',120)

go

/* Selecting the result */

select

 datepart(qq,DateColumn) QuarterNo

 ,dateadd(qq,datepart(qq,DateColumn),dateadd(dd,-(datepart(dy,DateColumn)-1),DateColumn)) FirstDayOfQuarter

 ,dateadd(qq,datepart(qq,DateColumn)+1,dateadd(dd,-(datepart(dy,DateColumn)),DateColumn)) LastDayOfQuarter

from

 MyDateTest99

go

/* Performing Cleanup */

drop table MyDateTest99

go

 

 

6. Number of days in a month:

 

Create Function

 udf_getNoOfDaysInMonth

 (

  @month int

  ,@year int

 )

returns

 int

as

begin

 return datepart( dd,dateadd(dd,-1,(dateadd(mm,@month,dateadd( yyyy,@year-1900,'19000101')))))

end

 

go

 

select dbo.udf_getNoOfDaysInMonth(2,2004)

 

go

 

 

A very common question asked in forums is regarding the change from a character column to a datetime column. The error encountered by developers is :

 

The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.

 

This is common because the varchar column does not provide any validations against the data and as a result, some invalid entries creep in. So, while converting to datetime, SQL Server is not able to change the character data to datetime and throws up an error. The easiest way to identify the rows that are causing problems and contain invalid datetime data is by using the isdate() function:

 

/* Example to show how to find invalid records */

 

use pubs

go

/* Creating a Test Table */

Create Table MyDateTest99

(

 DateColumn varchar(8)

)

go

/* Inserting the test value into the table */

insert into MyDateTest99 select '19780129'

insert into MyDateTest99 select '19670229'

insert into MyDateTest99 select '19910112'

insert into MyDateTest99 select '19791016'

go

/* Selecting the result */

select

  DateColumn

from

  MyDateTest99

where

 isdate(DateColumn) = 0

go

/* Performing Cleanup */

drop table MyDateTest99

 

Delphi - .....................................

 

sql server dogum gunu bulma

use pubs

 

 

go

 

---------------- Inserting only the time part into a datetime column --------------

/* Creating a Test Table */

Create Table MyDateTest99

(

 DateColumn datetime

)

go

 

 

/* Inserting the test value into the table */

insert into MyDateTest99 values ('10:00 AM')

go

 

 

/* Selecting the result */

select DateColumn from MyDateTest99

go

 

 

/* Performing Cleanup */

drop table MyDateTest99

go

 

 

---------------- Inserting only the date part into a datetime column --------------

use pubs

 

go

 

/* Creating a Test Table */

Create Table MyDateTest99

(

 DateColumn datetime

)

go

 

 

/* Inserting the test value into the table */

insert into MyDateTest99 values ('January 1, 2000')

go

 

 

/* Selecting the result */

select DateColumn from MyDateTest99

go

 

 

/* Performing Cleanup */

drop table MyDateTest99

go

 

 

 

 

 

So, the most common question that is asked is:

Q: How do I get SQL Server to return only the Date component or only the Time component from the datetime data type?

A: By using the Convert function. The syntax for using the convert function is:

 

CONVERT ( data_type [ ( length ) ] , expression [ , style ] )

 

By varying the datatype and length, we can get the desired component. Moreover, the style argument in the Convert function is provided exclusively for use with date and time data. Some sample code illustrating the same is as follows:

 

use pubs

 

 

go

 

---------------- Selecting only the date part from a datetime column --------------

/* Creating a Test Table */

Create Table MyDateTest99

(

 DateColumn datetime

)

go

 

 

/* Inserting the test value into the table */

insert into MyDateTest99 values (getdate())

go

 

 

/* Selecting the result */

select convert(varchar,DateColumn,101) from MyDateTest99

go

 

 

/* Performing Cleanup */

drop table MyDateTest99

go

 

 

use pubs

 

 

go

 

---------------- Selecting only the date part from a datetime column --------------

/* Creating a Test Table */

Create Table MyDateTest99

(

 DateColumn datetime

)

go

 

 

/* Inserting the test value into the table */

insert into MyDateTest99 values (getdate())

go

 

 

/* Selecting the result */

select convert(varchar,DateColumn,108) from MyDateTest99

go

 

 

/* Performing Cleanup */

drop table MyDateTest99

go

 

 

The list of styles that can be used are:

 

Style ID

 Style Type

 

0 or 100  mon dd yyyy hh:miAM (or PM)

101 mm/dd/yy

102 yy.mm.dd

103 dd/mm/yy

104 dd.mm.yy

105 dd-mm-yy

106 dd mon yy

107 Mon dd, yy

108 hh:mm:ss

9 or 109  mon dd yyyy hh:mi:ss:mmmAM (or PM)

110 mm-dd-yy

111 yy/mm/dd

112 yymmdd

13 or 113  dd mon yyyy hh:mm:ss:mmm(24h)

114 hh:mi:ss:mmm(24h)

20 or 120  yyyy-mm-dd hh:mi:ss(24h)

21 or 121  yyyy-mm-dd hh:mi:ss.mmm(24h)

126 yyyy-mm-dd Thh:mm:ss.mmm(no spaces)

130 dd mon yyyy hh:mi:ss:mmmAM

131 dd/mm/yy hh:mi:ss:mmmAM

 

These styles are the format of input to be used when converting character data into datetime and format of output while converting datetime data into characters:

 

use pubs

 

 

go

 

---------------- Example for the demonstration of use of style while input of data--------------

/* Creating a Test Table */

Create Table MyDateTest99

(

 DateColumn datetime

)

go

 

 

/* Inserting the test values into the table */

-- Inserting in US format

insert into MyDateTest99  select convert(datetime,'05/08/2004',101)

-- Inserting in UK format

insert into MyDateTest99  select convert(datetime,'08/05/2004',103)

-- Inserting in ISO Format

insert into MyDateTest99  select convert(datetime,'20040508',112)

go

 

 

/* Selecting the result */

select DateColumn from MyDateTest99

go

 

 

/* Performing Cleanup */

drop table MyDateTest99

go

 

 

use pubs

 

 

go

 

---------------- Example for the demonstration of use of style while output of data--------------

 

/* Creating a Test Table */

Create Table MyDateTest99

(

 DateColumn datetime

)

go

/* Inserting the test values into the table */

insert into MyDateTest99  select convert(datetime,'05/08/2004',101)

 

go

 

/* Selecting the result */

-- In US Format

select convert(varchar,DateColumn,101) from MyDateTest99

-- In UK Format

select convert(varchar,DateColumn,103) from MyDateTest99

-- In ISO Format

select convert(varchar,DateColumn,112) from MyDateTest99

 

go

/* Performing Cleanup */

drop table MyDateTest99

go

 

 

 

 

Some other functions that can be used for various purposes are DATEADD, DATEDIFF, DATENAME, DATEPART, DAY, GETDATE, MONTH, and YEAR. Here's some further detail on these functions as well as a code sample showing their use:

 

Dateadd: Returns a new datetime value based on adding an interval to the specified date.

 

Syntax: DATEADD ( datepart, number, date )

 

Datediff: Returns the number of date and time boundaries crossed between two specified dates.

 

Syntax: DATEDIFF ( datepart, startdate, enddate )

 

Datename: Returns a character string representing the specified datepart of the specified date.

 

Syntax: DATENAME ( datepart, date )

 

Datepart: Returns an integer representing the specified datepart of the specified date.

 

Syntax: DATEPART ( datepart, date )

 

Day: Returns an integer representing the day datepart of the specified date.

 

Syntax: DAY ( date )

 

Getdate: Returns the current system date and time in the Microsoft® SQL Serverstandard internal format for datetime values.

 

Syntax: GETDATE ( )

 

Month: Returns an integer that represents the month part of a specified date.

 

Syntax: MONTH ( date )

 

Year: Returns an integer that represents the year part of a specified date.

 

Syntax: YEAR ( date )

 

declare @datevar datetime

select @datevar = getdate()

 

/*Example for getdate() : getting current datetime*/

select getdate() [Current Datetime]

 

/*Example for dateadd : getting date 7 days from current datetime*/

select dateadd(dd, 7, @datevar) [Date 7 days from now]

 

/*Example for datediff : getting no of days passed since 01-01-2004*/

select datediff(dd,'20040101',@datevar) [No of days since 01-01-2004]

 

/*Example for datename : getting month name*/

select datename(mm, @datevar) [Month Name]

 

/*Example for datepart : getting week from date*/

select datepart(wk, @datevar ) [Week No]

 

/*Example for day : getting day part of date*/

select day (@datevar) [Day]

 

/*Example for month : getting month part of date*/

select month(@datevar) [Month]

 

/*Example for year : getting year part of date*/

select year(@datevar) [Year]

 

 

 

Now I will provide you with some code samples which you can use for various tasks. I will try to include as many examples I can think of, but this list is not exhaustive:

 

1. To find the first day of a month:

 

select dateadd(dd,-(day(DateColumn)-1),DateColumn)

 

 

2. To find last day of a month:

 

select dateadd(dd,-(day(dateadd(mm,1,DateColumn))),dateadd(mm,1,DateColumn))

 

 

3. To find birthdays in next seven days:

 

use pubs

 

 

go

 

/* Creating a Test Table */

Create Table MyDateTest99

(

 Birthday datetime

)

go

/* Inserting the test value into the table */

insert into MyDateTest99 select convert (varchar(10),'19780129',120)

insert into MyDateTest99 select convert (varchar(10),'19670821',120)

insert into MyDateTest99 select convert (varchar(10),'19910112',120)

insert into MyDateTest99 select convert (varchar(10),dateadd(dd,2,getdate()),120)

insert into MyDateTest99 select convert (varchar(10),'19791016',120)

 

 

go

/* Selecting the result */

select

 Birthday

from

 MyDateTest99

where

 datediff

  (

   dd

   ,convert(datetime,'1900/'+cast(month(getdate()) as varchar)+'/'+cast (day(getdate()) as varchar),111)

   ,convert(datetime,'1900/'+cast(month(Birthday) as varchar)+'/'+cast (day(Birthday) as varchar),111)

  ) between 0 and 7

go

/* Performing Cleanup */

drop table MyDateTest99

go

 

 

4. Number of hours until weekend, that is until Friday at 5 PM (my favorite):

 

use pubs

 

 

go

 

Create  function udf_Time_to_Weekend (@d1 datetime) returns datetime

as

begin

 declare @d2 datetime

 select @d2 =  case  when (datepart(hh,dateadd(dd,(7-datepart(dw,@d1)),@d1)) >= 17 and 7-datepart(dw,@d1) = 0)

    then  dateadd(hh,17,convert(varchar(10),dateadd(dd,7,@d1),101))

    else dateadd(hh,17,convert(varchar(10),dateadd(dd,(7-datepart(dw,@d1)),@d1),101))

   end

 return  @D2

END

go

Create   procedure HoursTillWeekend as

set datefirst 6

select DATEDIFF(MI,GETDATE(),dbo.udf_Time_to_Weekend(getdate()))/60 "Hours Till Weekend"

go

exec HoursTillWeekend

go

drop procedure HoursTillWeekend

go

drop function udf_Time_to_Weekend

go

 

 

5. First and last days of quarter, in which a date falls:

 

use pubs

go

/* Creating a Test Table */

Create Table MyDateTest99

(

 DateColumn datetime

)

go

/* Inserting the test value into the table */

insert into MyDateTest99 select convert (varchar(10),'19780129',120)

insert into MyDateTest99 select convert (varchar(10),'19670821',120)

insert into MyDateTest99 select convert (varchar(10),'19910112',120)

insert into MyDateTest99 select convert (varchar(10),'19791016',120)

go

/* Selecting the result */

select

 datepart(qq,DateColumn) QuarterNo

 ,dateadd(qq,datepart(qq,DateColumn),dateadd(dd,-(datepart(dy,DateColumn)-1),DateColumn)) FirstDayOfQuarter

 ,dateadd(qq,datepart(qq,DateColumn)+1,dateadd(dd,-(datepart(dy,DateColumn)),DateColumn)) LastDayOfQuarter

from

 MyDateTest99

go

/* Performing Cleanup */

drop table MyDateTest99

go

 

 

6. Number of days in a month:

 

Create Function

 udf_getNoOfDaysInMonth

 (

  @month int

  ,@year int

 )

returns

 int

as

begin

 return datepart( dd,dateadd(dd,-1,(dateadd(mm,@month,dateadd( yyyy,@year-1900,'19000101')))))

end

 

go

 

select dbo.udf_getNoOfDaysInMonth(2,2004)

 

go

 

 

A very common question asked in forums is regarding the change from a character column to a datetime column. The error encountered by developers is :

 

The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.

 

This is common because the varchar column does not provide any validations against the data and as a result, some invalid entries creep in. So, while converting to datetime, SQL Server is not able to change the character data to datetime and throws up an error. The easiest way to identify the rows that are causing problems and contain invalid datetime data is by using the isdate() function:

 

/* Example to show how to find invalid records */

 

use pubs

go

/* Creating a Test Table */

Create Table MyDateTest99

(

 DateColumn varchar(8)

)

go

/* Inserting the test value into the table */

insert into MyDateTest99 select '19780129'

insert into MyDateTest99 select '19670229'

insert into MyDateTest99 select '19910112'

insert into MyDateTest99 select '19791016'

go

/* Selecting the result */

select

  DateColumn

from

  MyDateTest99

where

 isdate(DateColumn) = 0

go

/* Performing Cleanup */

drop table MyDateTest99

 

Delphi - .....................................

 

Sql Server Web Erişim

sql server 1433 nolu portu dinliyor

 

Delphi - .....................................

 

Sql Server Web Erişim

sql server 1433 nolu portu dinliyor

 

Delphi - .....................................

 

sql server database dosya adını değiştirmek

EXEC sp_detach_db 'saiko'

 

EXEC xp_cmdshell 'rename "c:mgmssqlsaiko1_Data.MDF"

"c:mgmssqlNEWsaiko1_Data.MDF"'

 

EXEC xp_cmdshell 'rename "c:mgmssqlsaiko1_Log.LDF"

"c:mgmssqlNEWsaiko1_Log.LDF"'

 

EXEC sp_attach_db @dbname = 'saiko1',

@filename1='c:mgmssqlsaiko_Data.MDF',

@filename2='c:mgmssqlsaiko_Log.LDF'

 

Delphi - .....................................

 

sql server database dosya adını değiştirmek

EXEC sp_detach_db 'saiko'

 

EXEC xp_cmdshell 'rename "c:mgmssqlsaiko1_Data.MDF"

"c:mgmssqlNEWsaiko1_Data.MDF"'

 

EXEC xp_cmdshell 'rename "c:mgmssqlsaiko1_Log.LDF"

"c:mgmssqlNEWsaiko1_Log.LDF"'

 

EXEC sp_attach_db @dbname = 'saiko1',

@filename1='c:mgmssqlsaiko_Data.MDF',

@filename2='c:mgmssqlsaiko_Log.LDF'

 

Delphi - .....................................

 

sql server attach detach

EXEC sp_detach_db 'pubs', 'false'

 

 

EXEC sp_attach_db @dbname = 'pubs',

     @filename1 = 'C:MSSQLDatapubs.mdf',

     @filename2 = 'C:MSSQLDatapubs_log.ldf'

 

 

 

EXEC sp_attach_single_file_db @dbname = 'pubs',

     @physname = 'C:MSSQLDatapubs.mdf'

 

Delphi - .....................................

 

sql server attach detach

EXEC sp_detach_db 'pubs', 'false'

 

 

EXEC sp_attach_db @dbname = 'pubs',

     @filename1 = 'C:MSSQLDatapubs.mdf',

     @filename2 = 'C:MSSQLDatapubs_log.ldf'

 

 

 

EXEC sp_attach_single_file_db @dbname = 'pubs',

     @physname = 'C:MSSQLDatapubs.mdf'

 

Delphi - .....................................

 

Log dasyası tutmak...

1. Yöntem

 

procedure logla(log:string);

var

 st:tstringlist;

begin

  st:=tstringlist.create;

  st.loadfromfile('c:windowslog.log');

  st.add(log);

  st.savetofile('c:windowslog.log');

  st.free;

end;

 

 

*******************************************************************************

 

2.Yöntem

 

Ya da doğrudan dosyaya

 

procedure WriteToLog(data: string);

var

   Temp, FileName: String;

   nSize: DWord;

   LogFile: TextFile;

begin

   nSize:=100;

   SetLength(FileName, nSize);

   GetSystemDirectory(PChar(FileName), nSize);

   FileName:=PChar(FileName)+'benim.log';

   AssignFile(LogFile, FileName);

   try

     Append(LogFile);

   except

     ReWrite(LogFile);

   end;

   try

     WriteLn(LogFile, data);

   finally

     CloseFile(LogFile);

   end;

end;

 

'KiPSOFT' said the following on 24.03.2006 20:35:

 

 

KiPSOFT <kipsoftware@softhome.net>  arkadaşıma yardımları için teşekkür ederim.

 

Delphi - .....................................

 

Log dasyası tutmak...

1. Yöntem

 

procedure logla(log:string);

var

 st:tstringlist;

begin

  st:=tstringlist.create;

  st.loadfromfile('c:windowslog.log');

  st.add(log);

  st.savetofile('c:windowslog.log');

  st.free;

end;

 

 

*******************************************************************************

 

2.Yöntem

 

Ya da doğrudan dosyaya

 

procedure WriteToLog(data: string);

var

   Temp, FileName: String;

   nSize: DWord;

   LogFile: TextFile;

begin

   nSize:=100;

   SetLength(FileName, nSize);

   GetSystemDirectory(PChar(FileName), nSize);

   FileName:=PChar(FileName)+'benim.log';

   AssignFile(LogFile, FileName);

   try

     Append(LogFile);

   except

     ReWrite(LogFile);

   end;

   try

     WriteLn(LogFile, data);

   finally

     CloseFile(LogFile);

   end;

end;

 

 

Delphi - .....................................

Bu web sitesi ücretsiz olarak Bedava-Sitem.com ile oluşturulmuştur. Siz de kendi web sitenizi kurmak ister misiniz?
Ücretsiz kaydol