Examples for Sybase


This file contains selected examples, some of which also show the result that Sybase gives. Refer also to the file that contains only the queries: Queries

Examples that have been omitted either have no equivalent to Sybase, or they are trivial after examples given previously.

1. select now=convert(char,getdate(),3)

 now                            
 ------------------------------ 
 23/11/97

2. select datename(hour,getdate()),datename(minute,getdate()), datename(second,getdate())

3. select getdate()

6. select interval=datepart(year,"Nov 1 1993")+datepart(year,getdate())

We see that to simulate an interval, we have to use an integer. interval ----------- 3990

8. select convert(datetime,"1997-01-01",105) -------------------------- Jan 1 1997 12:00AM

9. select convert(datetime,"12:34:56")

10. select convert(datetime,"1997-01-01 12:34:56",105)

11. select day=9,second=datepart(second,"23:45:12") day second ----------- ----------- 9 12

For the following examples, we have the table birthdays, which is populated with some data:

Name Birthday favorable nickname ---------- -------------------------- --------- ---------- Elias Dec 7 1980 12:00AM Y NULL Jeremy Sep 15 1976 12:00AM NULL NULL Lena Jul 7 1942 12:00AM Y NULL Yiannis Nov 7 1941 12:00AM Y NULL

Name is the primary key, birthday cannot be null, and the other 2 fields can be null.

13. select Birthday from birthdays where Birthday="Sep 15 1976" Birthday -------------------------- Sep 15 1976 12:00AM

14. select Birthday from birthdays where Birthday<"sep 15 1976"

15. select Birthday from birthdays where Birthday<>"Sep 15 1976"

16. select Birthday from birthdays where Birthday>"Sep 10 1976" AND Birthday<"sep 20 1976"

This is equivalent to Birthday BETWEEN 9/10/76 AND 9/20/76

17. select date=getdate() where datepart(month,getdate())=datepart(month,"Dec 1 1950")

(i.e. show the current date only if the current month is December.) If we actually need to show equivalence between two intervals, it is pretty easy, since we represent intervals with integers, so we would be doing simple integer comparisons. The drawback to this approach is that we don't know what kind our interval is, i.e. if it is 1, is it a month or a second? We have to know in advance what kind of interval it is. Look at the following example: ID interval type -- -------- ------- 1 10 MIN 2 10 SEC 3 15 MIN then select ID from intervals where interval>10, where we mean 10 as a second interval, would give us only the 3rd interval. The correct query would be: select ID from intervals where (interval>10 AND type="SEC") OR (interval>0 AND type<>"SEC") Of course we would run into problems in different situations, since we will need to do arithmetic on converting from minutes to hours, hours to days etc. So for a query that needs something like interval<1 hour, we would have to check for interval<60 and type="MIN" or interval<3600 and type="SEC" . so writing down a query for interval compirison can get pretty tedious, but it could be done if we carefully pick the complete query. equivalently, for 18-20, we use the appropriate predicate (for 20, use the equivalent of example 16) for example, to test for inequality, if we know that the interval that we are testing is in hours, then we have to check for interval*24<>ourinterval AND type="HOUR" OR interval*24*365<>ourinterval AND type="YEAR" etc

The following examples just uses a table that allows a NULL datetime.

21. select name from dates where birthday=NULL

22. select ID from intervals where interval=NULL

23. select Birthday from birthdays where Birthday<="dec 31 1977" and datepart(year,"jan 1 1977")<="datepart(year,Birthday)" + 1

(i.e. select the birthday only if Birthday+ 1 year overlaps the period 1/77 to 12/77) Birthday -------------------------- Sep 15 1976 12:00AM

24. select dateadd(month,10,getdate())

Shows the date and time 10 months from today

25+26. select datediff(month,getdate(),"Sep 1 1980") Shows how many months have passed since September 1 1980.

28.Displaying the current date: select convert(char(3),datename(month,getdate()))+" " +convert(char(2),datename(day,getdate()))+"," +convert(char(4),datename(year,getdate())) Displaying the current time: select convert(char(2),datename(hour,getdate()))+":" +convert(char(2),datename(minute,getdate()))+":" +convert(char(2),datename(second,getdate()))

29. select datename(month,getdate()),datename(day,getdate()), datename(year,getdate()) Selects the appropriate date fields from datetime from the current datetime.

30. Similarly, select the appropriate time fields (hour, minute, second). Selecting just the date part from the current timestamp (i.e. resetting the time fields to 12:00AM): select convert (datetime, (convert(char(3),datename(month,getdate()))+" " +convert(char(2),datename(day,getdate()))+"," +convert(char(4),datename(year,getdate())) ) ) This is an example of making all possible conversions back and forth from datetime to char: select convert (datetime, convert(char(4),datename(year,getdate())) +"-" +convert (char(3),datename(month,getdate())) +"-" +convert(char(2),datename(day,getdate())) +" " +convert(char(2),datename(hour,getdate())) +":" +convert(char(2),datename(minute,getdate())) +":" +convert(char(2),datename(second,getdate())) ,105)

31. select date=getdate()

32.+33. Intervals have to be of the same type, then there is no problem since intervals are integers and we just perform simple arithmetic. If there is a chance intervals are of different types, the appropriate conversion must happen first. For example, select add=interval+15 from intervals where type = "SEC", if we assume 15 is in seconds. This query will show all intervals that are in seconds with the value 15 added. Of course, if we want the complete results, we have to include select add= interval*60+15 where type="MIN" and so on.

34+35. select months=datediff(month,"Apr 5 1997","May 9 1997") Gives a result of 1 (it is not possible to also say 1 month and 4 days). 36-40. These use normal arithmetic operations: we can simulate intervals in Sybase using datepart, which yields an integer, thus you can do the normal arithmetic on them.

41. select convert(datetime,"1997-01-01",105)

42. select datename(hour,convert(datetime,"1997-01-01 11:00AM",105)) ------------------------------ 11

In Sybase, you can convert from any string (in an appropriate format) that contains part of a datetime, e.g. just the date or just the time. Then, you can select different dateparts using datename (and convert that to a string if desired).

43. select convert(datetime,"1997-01-01",105)

44. select convert(datetime,"12:00AM")

47. select convert(char(30),getdate())

49-52. Since there is no interval datatype, we can simulate them using integers. This is why in a lot of the examples use the function datepart, since it returns an integer.

53. select datename(day,getdate())

54. +55. Since we can't have an interval of 2 types (e.g. 1 day 2 hours), then to "extract" something out of an interval, we just have to check it's type, i.e. select interval from interval where type="DAY", if we need to extract day. More complex arithmetic is required if we need to extract a datepart from an interval which has a different datepart type.

62. For periods, we have created the following table:

create table periods ( periodID char(5) not null, p1 datetime not null, p2 datetime not null, constraint period_ID primary key (periodID) )

The table is populated with the following data:

periodID p1 p2 -------- -------------------------- -------------------------- acad Aug 25 1997 12:00AM Dec 20 1997 12:00AM acad2 Jan 14 1998 12:00AM May 14 1997 12:00AM break Oct 10 1997 12:00AM Oct 25 1997 12:00AM exams Dec 15 1997 12:00AM Dec 20 1997 12:00AM fall Sep 1 1997 12:00AM Nov 30 1997 12:00AM orien Aug 25 1997 12:00AM Aug 28 1997 12:00AM sprin Mar 1 1998 12:00AM May 31 1997 12:00AM ---------------------------------------------------------------

63. select periodID from periods where p1="Dec 15 1997" AND p2="Dec 30 1997" Shows all the periods that are equal to 12/15/97-12/30/97.

64. select periodID from periods where p2 <"dec 15 1997" shows all the periods that are before the period 12/15/97-12/30/97 periodid acad2 break fall orien sprin

65. select periodID from periods where p1>"Dec 30, 1997" Shows all periods before-1

 the December period we've 
used above.

66. select periodID from periods where p1 = "Dec 30 1997" Shows all the periods that "meet" 12/15/97-12/30/97 (none)

67-71. Similarly, we use the correct predicates using ANDs and ORs. E.g, p2>q1>p1 translates in the WHERE clause as: p2 > q1 AND q1 >p1.

72. select periodID from periods where p1="Dec 15 1997" AND p2<"dec 30 1997" this is the equivalent to p starts q, where q is 12/15/97-12/30/97 periodid exams

73. Similarly, choose the appropriate equivalent.

74. select periodID from periods where p2="Dec 30 1997" AND p1>"Dec 15 1997" This is the equivalent to p finishes q. (none)

75. Similarly, choose the correct predicate.

76. Combine the predicates (with OR) from 63, 66, 68 (ie it's either equal, it meets or overlaps is equivalent to the SQL OVERLAPS)

77. select periodID from periods where p1=NULL

78. select p1 from periods (shows all the beginnings)

79. select dateadd(day,-1,p1) from periods Shows the previous day to each period. -------------------------- Aug 24 1997 12:00AM Jan 13 1998 12:00AM Oct 9 1997 12:00AM Dec 14 1997 12:00AM Aug 31 1997 12:00AM Aug 24 1997 12:00AM Feb 28 1998 12:00AM

80. select dateadd(day,-1,p2) from periods Shows the last day of each period. -------------------------- Dec 19 1997 12:00AM May 13 1997 12:00AM Oct 24 1997 12:00AM Dec 19 1997 12:00AM Nov 29 1997 12:00AM Aug 27 1997 12:00AM May 30 1997 12:00AM

81. select p2 from periods Equivalent to ending(p) for all periods.

82. select datediff(datepart,p1,p2) from periods Equivalent to duration(p)

84+85. select dateadd(day,5,p1),dateadd(day,5,p2) from periods Equivalent to p + i (and i+p)in the period constructors

86. Similarly, use a negative number, ie in the example above, use -5.

87. select start="Dec 12 1997",end="Dec 13 1997" (you could use that to insert the above period in the table periods in the where clause of the insert values into periods query) 88-90. Similarly, you can select an individual date (ie a, b) or a date from a period (ie p1, p2) and use them as the appropriate part to insert a new date into the periods table.

97. Converting a period to a char: select convert(char(10),p1)+"-"+convert(char(10),p2) from periods