Open History to see the last commit
1---
2id: caseWhen
3title: caseWhen
4---
5
6# Function: caseWhen()
7
8## Call Signature
9
10```ts
11function caseWhen<C1, V1>(condition1, value1): CaseWhenResult<[V1], false>;
12```
13
14Defined in: [packages/db/src/query/builder/functions.ts:399](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/functions.ts#L399)
15
16Returns the value for the first matching condition, similar to SQL
17`CASE WHEN`.
18
19Arguments are evaluated as condition/value pairs followed by an optional
20default value. Scalar branch values return a query expression and can be used
21in expression contexts like `select`, `where`, `orderBy`, `groupBy`,
22`having`, and equality join operands. If no scalar branch matches and no
23default is provided, the result is `null`.
24
25When a branch value is a projection object, `caseWhen` becomes a select-only
26projection value. Projection branches can include nested fields, ref spreads,
27and includes. If no projection branch matches and no default is provided, the
28result is `undefined`.
29
30### Type Parameters
31
32#### C1
33
34`C1` *extends* `ExpressionLike`
35
36#### V1
37
38`V1` *extends* `CaseWhenValue`
39
40### Parameters
41
42#### condition1
43
44`C1`
45
46#### value1
47
48`V1`
49
50### Returns
51
52`CaseWhenResult`\<\[`V1`\], `false`\>
53
54### Examples
55
56```ts
57caseWhen(gt(user.age, 18), `adult`, `minor`)
58```
59
60```ts
61caseWhen(
62 gt(user.age, 65),
63 `senior`,
64 gt(user.age, 18),
65 `adult`,
66 `minor`,
67)
68```
69
70```ts
71caseWhen(gt(user.age, 18), {
72 ...user,
73 posts: q
74 .from({ post: postsCollection })
75 .where(({ post }) => eq(post.userId, user.id)),
76})
77```
78
79## Call Signature
80
81```ts
82function caseWhen<C1, V1, D>(
83 condition1,
84 value1,
85defaultValue): CaseWhenResult<[V1, D], true>;
86```
87
88Defined in: [packages/db/src/query/builder/functions.ts:403](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/functions.ts#L403)
89
90Returns the value for the first matching condition, similar to SQL
91`CASE WHEN`.
92
93Arguments are evaluated as condition/value pairs followed by an optional
94default value. Scalar branch values return a query expression and can be used
95in expression contexts like `select`, `where`, `orderBy`, `groupBy`,
96`having`, and equality join operands. If no scalar branch matches and no
97default is provided, the result is `null`.
98
99When a branch value is a projection object, `caseWhen` becomes a select-only
100projection value. Projection branches can include nested fields, ref spreads,
101and includes. If no projection branch matches and no default is provided, the
102result is `undefined`.
103
104### Type Parameters
105
106#### C1
107
108`C1` *extends* `ExpressionLike`
109
110#### V1
111
112`V1` *extends* `CaseWhenValue`
113
114#### D
115
116`D` *extends* `CaseWhenValue`
117
118### Parameters
119
120#### condition1
121
122`C1`
123
124#### value1
125
126`V1`
127
128#### defaultValue
129
130`D`
131
132### Returns
133
134`CaseWhenResult`\<\[`V1`, `D`\], `true`\>
135
136### Examples
137
138```ts
139caseWhen(gt(user.age, 18), `adult`, `minor`)
140```
141
142```ts
143caseWhen(
144 gt(user.age, 65),
145 `senior`,
146 gt(user.age, 18),
147 `adult`,
148 `minor`,
149)
150```
151
152```ts
153caseWhen(gt(user.age, 18), {
154 ...user,
155 posts: q
156 .from({ post: postsCollection })
157 .where(({ post }) => eq(post.userId, user.id)),
158})
159```
160
161## Call Signature
162
163```ts
164function caseWhen<C1, V1, C2, V2>(
165 condition1,
166 value1,
167 condition2,
168value2): CaseWhenResult<[V1, V2], false>;
169```
170
171Defined in: [packages/db/src/query/builder/functions.ts:408](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/functions.ts#L408)
172
173Returns the value for the first matching condition, similar to SQL
174`CASE WHEN`.
175
176Arguments are evaluated as condition/value pairs followed by an optional
177default value. Scalar branch values return a query expression and can be used
178in expression contexts like `select`, `where`, `orderBy`, `groupBy`,
179`having`, and equality join operands. If no scalar branch matches and no
180default is provided, the result is `null`.
181
182When a branch value is a projection object, `caseWhen` becomes a select-only
183projection value. Projection branches can include nested fields, ref spreads,
184and includes. If no projection branch matches and no default is provided, the
185result is `undefined`.
186
187### Type Parameters
188
189#### C1
190
191`C1` *extends* `ExpressionLike`
192
193#### V1
194
195`V1` *extends* `CaseWhenValue`
196
197#### C2
198
199`C2` *extends* `ExpressionLike`
200
201#### V2
202
203`V2` *extends* `CaseWhenValue`
204
205### Parameters
206
207#### condition1
208
209`C1`
210
211#### value1
212
213`V1`
214
215#### condition2
216
217`C2`
218
219#### value2
220
221`V2`
222
223### Returns
224
225`CaseWhenResult`\<\[`V1`, `V2`\], `false`\>
226
227### Examples
228
229```ts
230caseWhen(gt(user.age, 18), `adult`, `minor`)
231```
232
233```ts
234caseWhen(
235 gt(user.age, 65),
236 `senior`,
237 gt(user.age, 18),
238 `adult`,
239 `minor`,
240)
241```
242
243```ts
244caseWhen(gt(user.age, 18), {
245 ...user,
246 posts: q
247 .from({ post: postsCollection })
248 .where(({ post }) => eq(post.userId, user.id)),
249})
250```
251
252## Call Signature
253
254```ts
255function caseWhen<C1, V1, C2, V2, D>(
256 condition1,
257 value1,
258 condition2,
259 value2,
260defaultValue): CaseWhenResult<[V1, V2, D], true>;
261```
262
263Defined in: [packages/db/src/query/builder/functions.ts:419](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/functions.ts#L419)
264
265Returns the value for the first matching condition, similar to SQL
266`CASE WHEN`.
267
268Arguments are evaluated as condition/value pairs followed by an optional
269default value. Scalar branch values return a query expression and can be used
270in expression contexts like `select`, `where`, `orderBy`, `groupBy`,
271`having`, and equality join operands. If no scalar branch matches and no
272default is provided, the result is `null`.
273
274When a branch value is a projection object, `caseWhen` becomes a select-only
275projection value. Projection branches can include nested fields, ref spreads,
276and includes. If no projection branch matches and no default is provided, the
277result is `undefined`.
278
279### Type Parameters
280
281#### C1
282
283`C1` *extends* `ExpressionLike`
284
285#### V1
286
287`V1` *extends* `CaseWhenValue`
288
289#### C2
290
291`C2` *extends* `ExpressionLike`
292
293#### V2
294
295`V2` *extends* `CaseWhenValue`
296
297#### D
298
299`D` *extends* `CaseWhenValue`
300
301### Parameters
302
303#### condition1
304
305`C1`
306
307#### value1
308
309`V1`
310
311#### condition2
312
313`C2`
314
315#### value2
316
317`V2`
318
319#### defaultValue
320
321`D`
322
323### Returns
324
325`CaseWhenResult`\<\[`V1`, `V2`, `D`\], `true`\>
326
327### Examples
328
329```ts
330caseWhen(gt(user.age, 18), `adult`, `minor`)
331```
332
333```ts
334caseWhen(
335 gt(user.age, 65),
336 `senior`,
337 gt(user.age, 18),
338 `adult`,
339 `minor`,
340)
341```
342
343```ts
344caseWhen(gt(user.age, 18), {
345 ...user,
346 posts: q
347 .from({ post: postsCollection })
348 .where(({ post }) => eq(post.userId, user.id)),
349})
350```
351
352## Call Signature
353
354```ts
355function caseWhen<C1, V1, C2, V2, C3, V3>(
356 condition1,
357 value1,
358 condition2,
359 value2,
360 condition3,
361value3): CaseWhenResult<[V1, V2, V3], false>;
362```
363
364Defined in: [packages/db/src/query/builder/functions.ts:432](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/functions.ts#L432)
365
366Returns the value for the first matching condition, similar to SQL
367`CASE WHEN`.
368
369Arguments are evaluated as condition/value pairs followed by an optional
370default value. Scalar branch values return a query expression and can be used
371in expression contexts like `select`, `where`, `orderBy`, `groupBy`,
372`having`, and equality join operands. If no scalar branch matches and no
373default is provided, the result is `null`.
374
375When a branch value is a projection object, `caseWhen` becomes a select-only
376projection value. Projection branches can include nested fields, ref spreads,
377and includes. If no projection branch matches and no default is provided, the
378result is `undefined`.
379
380### Type Parameters
381
382#### C1
383
384`C1` *extends* `ExpressionLike`
385
386#### V1
387
388`V1` *extends* `CaseWhenValue`
389
390#### C2
391
392`C2` *extends* `ExpressionLike`
393
394#### V2
395
396`V2` *extends* `CaseWhenValue`
397
398#### C3
399
400`C3` *extends* `ExpressionLike`
401
402#### V3
403
404`V3` *extends* `CaseWhenValue`
405
406### Parameters
407
408#### condition1
409
410`C1`
411
412#### value1
413
414`V1`
415
416#### condition2
417
418`C2`
419
420#### value2
421
422`V2`
423
424#### condition3
425
426`C3`
427
428#### value3
429
430`V3`
431
432### Returns
433
434`CaseWhenResult`\<\[`V1`, `V2`, `V3`\], `false`\>
435
436### Examples
437
438```ts
439caseWhen(gt(user.age, 18), `adult`, `minor`)
440```
441
442```ts
443caseWhen(
444 gt(user.age, 65),
445 `senior`,
446 gt(user.age, 18),
447 `adult`,
448 `minor`,
449)
450```
451
452```ts
453caseWhen(gt(user.age, 18), {
454 ...user,
455 posts: q
456 .from({ post: postsCollection })
457 .where(({ post }) => eq(post.userId, user.id)),
458})
459```
460
461## Call Signature
462
463```ts
464function caseWhen<C1, V1, C2, V2, C3, V3, D>(
465 condition1,
466 value1,
467 condition2,
468 value2,
469 condition3,
470 value3,
471defaultValue): CaseWhenResult<[V1, V2, V3, D], true>;
472```
473
474Defined in: [packages/db/src/query/builder/functions.ts:447](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/functions.ts#L447)
475
476Returns the value for the first matching condition, similar to SQL
477`CASE WHEN`.
478
479Arguments are evaluated as condition/value pairs followed by an optional
480default value. Scalar branch values return a query expression and can be used
481in expression contexts like `select`, `where`, `orderBy`, `groupBy`,
482`having`, and equality join operands. If no scalar branch matches and no
483default is provided, the result is `null`.
484
485When a branch value is a projection object, `caseWhen` becomes a select-only
486projection value. Projection branches can include nested fields, ref spreads,
487and includes. If no projection branch matches and no default is provided, the
488result is `undefined`.
489
490### Type Parameters
491
492#### C1
493
494`C1` *extends* `ExpressionLike`
495
496#### V1
497
498`V1` *extends* `CaseWhenValue`
499
500#### C2
501
502`C2` *extends* `ExpressionLike`
503
504#### V2
505
506`V2` *extends* `CaseWhenValue`
507
508#### C3
509
510`C3` *extends* `ExpressionLike`
511
512#### V3
513
514`V3` *extends* `CaseWhenValue`
515
516#### D
517
518`D` *extends* `CaseWhenValue`
519
520### Parameters
521
522#### condition1
523
524`C1`
525
526#### value1
527
528`V1`
529
530#### condition2
531
532`C2`
533
534#### value2
535
536`V2`
537
538#### condition3
539
540`C3`
541
542#### value3
543
544`V3`
545
546#### defaultValue
547
548`D`
549
550### Returns
551
552`CaseWhenResult`\<\[`V1`, `V2`, `V3`, `D`\], `true`\>
553
554### Examples
555
556```ts
557caseWhen(gt(user.age, 18), `adult`, `minor`)
558```
559
560```ts
561caseWhen(
562 gt(user.age, 65),
563 `senior`,
564 gt(user.age, 18),
565 `adult`,
566 `minor`,
567)
568```
569
570```ts
571caseWhen(gt(user.age, 18), {
572 ...user,
573 posts: q
574 .from({ post: postsCollection })
575 .where(({ post }) => eq(post.userId, user.id)),
576})
577```
578
579## Call Signature
580
581```ts
582function caseWhen<C1, V1, C2, V2, C3, V3, C4, V4>(
583 condition1,
584 value1,
585 condition2,
586 value2,
587 condition3,
588 value3,
589 condition4,
590value4): CaseWhenResult<[V1, V2, V3, V4], false>;
591```
592
593Defined in: [packages/db/src/query/builder/functions.ts:464](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/functions.ts#L464)
594
595Returns the value for the first matching condition, similar to SQL
596`CASE WHEN`.
597
598Arguments are evaluated as condition/value pairs followed by an optional
599default value. Scalar branch values return a query expression and can be used
600in expression contexts like `select`, `where`, `orderBy`, `groupBy`,
601`having`, and equality join operands. If no scalar branch matches and no
602default is provided, the result is `null`.
603
604When a branch value is a projection object, `caseWhen` becomes a select-only
605projection value. Projection branches can include nested fields, ref spreads,
606and includes. If no projection branch matches and no default is provided, the
607result is `undefined`.
608
609### Type Parameters
610
611#### C1
612
613`C1` *extends* `ExpressionLike`
614
615#### V1
616
617`V1` *extends* `CaseWhenValue`
618
619#### C2
620
621`C2` *extends* `ExpressionLike`
622
623#### V2
624
625`V2` *extends* `CaseWhenValue`
626
627#### C3
628
629`C3` *extends* `ExpressionLike`
630
631#### V3
632
633`V3` *extends* `CaseWhenValue`
634
635#### C4
636
637`C4` *extends* `ExpressionLike`
638
639#### V4
640
641`V4` *extends* `CaseWhenValue`
642
643### Parameters
644
645#### condition1
646
647`C1`
648
649#### value1
650
651`V1`
652
653#### condition2
654
655`C2`
656
657#### value2
658
659`V2`
660
661#### condition3
662
663`C3`
664
665#### value3
666
667`V3`
668
669#### condition4
670
671`C4`
672
673#### value4
674
675`V4`
676
677### Returns
678
679`CaseWhenResult`\<\[`V1`, `V2`, `V3`, `V4`\], `false`\>
680
681### Examples
682
683```ts
684caseWhen(gt(user.age, 18), `adult`, `minor`)
685```
686
687```ts
688caseWhen(
689 gt(user.age, 65),
690 `senior`,
691 gt(user.age, 18),
692 `adult`,
693 `minor`,
694)
695```
696
697```ts
698caseWhen(gt(user.age, 18), {
699 ...user,
700 posts: q
701 .from({ post: postsCollection })
702 .where(({ post }) => eq(post.userId, user.id)),
703})
704```
705
706## Call Signature
707
708```ts
709function caseWhen<C1, V1, C2, V2, C3, V3, C4, V4, D>(
710 condition1,
711 value1,
712 condition2,
713 value2,
714 condition3,
715 value3,
716 condition4,
717 value4,
718defaultValue): CaseWhenResult<[V1, V2, V3, V4, D], true>;
719```
720
721Defined in: [packages/db/src/query/builder/functions.ts:483](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/functions.ts#L483)
722
723Returns the value for the first matching condition, similar to SQL
724`CASE WHEN`.
725
726Arguments are evaluated as condition/value pairs followed by an optional
727default value. Scalar branch values return a query expression and can be used
728in expression contexts like `select`, `where`, `orderBy`, `groupBy`,
729`having`, and equality join operands. If no scalar branch matches and no
730default is provided, the result is `null`.
731
732When a branch value is a projection object, `caseWhen` becomes a select-only
733projection value. Projection branches can include nested fields, ref spreads,
734and includes. If no projection branch matches and no default is provided, the
735result is `undefined`.
736
737### Type Parameters
738
739#### C1
740
741`C1` *extends* `ExpressionLike`
742
743#### V1
744
745`V1` *extends* `CaseWhenValue`
746
747#### C2
748
749`C2` *extends* `ExpressionLike`
750
751#### V2
752
753`V2` *extends* `CaseWhenValue`
754
755#### C3
756
757`C3` *extends* `ExpressionLike`
758
759#### V3
760
761`V3` *extends* `CaseWhenValue`
762
763#### C4
764
765`C4` *extends* `ExpressionLike`
766
767#### V4
768
769`V4` *extends* `CaseWhenValue`
770
771#### D
772
773`D` *extends* `CaseWhenValue`
774
775### Parameters
776
777#### condition1
778
779`C1`
780
781#### value1
782
783`V1`
784
785#### condition2
786
787`C2`
788
789#### value2
790
791`V2`
792
793#### condition3
794
795`C3`
796
797#### value3
798
799`V3`
800
801#### condition4
802
803`C4`
804
805#### value4
806
807`V4`
808
809#### defaultValue
810
811`D`
812
813### Returns
814
815`CaseWhenResult`\<\[`V1`, `V2`, `V3`, `V4`, `D`\], `true`\>
816
817### Examples
818
819```ts
820caseWhen(gt(user.age, 18), `adult`, `minor`)
821```
822
823```ts
824caseWhen(
825 gt(user.age, 65),
826 `senior`,
827 gt(user.age, 18),
828 `adult`,
829 `minor`,
830)
831```
832
833```ts
834caseWhen(gt(user.age, 18), {
835 ...user,
836 posts: q
837 .from({ post: postsCollection })
838 .where(({ post }) => eq(post.userId, user.id)),
839})
840```
841
842## Call Signature
843
844```ts
845function caseWhen<C1, V1, C2, V2, C3, V3, C4, V4, C5, V5>(
846 condition1,
847 value1,
848 condition2,
849 value2,
850 condition3,
851 value3,
852 condition4,
853 value4,
854 condition5,
855value5): CaseWhenResult<[V1, V2, V3, V4, V5], false>;
856```
857
858Defined in: [packages/db/src/query/builder/functions.ts:504](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/functions.ts#L504)
859
860Returns the value for the first matching condition, similar to SQL
861`CASE WHEN`.
862
863Arguments are evaluated as condition/value pairs followed by an optional
864default value. Scalar branch values return a query expression and can be used
865in expression contexts like `select`, `where`, `orderBy`, `groupBy`,
866`having`, and equality join operands. If no scalar branch matches and no
867default is provided, the result is `null`.
868
869When a branch value is a projection object, `caseWhen` becomes a select-only
870projection value. Projection branches can include nested fields, ref spreads,
871and includes. If no projection branch matches and no default is provided, the
872result is `undefined`.
873
874### Type Parameters
875
876#### C1
877
878`C1` *extends* `ExpressionLike`
879
880#### V1
881
882`V1` *extends* `CaseWhenValue`
883
884#### C2
885
886`C2` *extends* `ExpressionLike`
887
888#### V2
889
890`V2` *extends* `CaseWhenValue`
891
892#### C3
893
894`C3` *extends* `ExpressionLike`
895
896#### V3
897
898`V3` *extends* `CaseWhenValue`
899
900#### C4
901
902`C4` *extends* `ExpressionLike`
903
904#### V4
905
906`V4` *extends* `CaseWhenValue`
907
908#### C5
909
910`C5` *extends* `ExpressionLike`
911
912#### V5
913
914`V5` *extends* `CaseWhenValue`
915
916### Parameters
917
918#### condition1
919
920`C1`
921
922#### value1
923
924`V1`
925
926#### condition2
927
928`C2`
929
930#### value2
931
932`V2`
933
934#### condition3
935
936`C3`
937
938#### value3
939
940`V3`
941
942#### condition4
943
944`C4`
945
946#### value4
947
948`V4`
949
950#### condition5
951
952`C5`
953
954#### value5
955
956`V5`
957
958### Returns
959
960`CaseWhenResult`\<\[`V1`, `V2`, `V3`, `V4`, `V5`\], `false`\>
961
962### Examples
963
964```ts
965caseWhen(gt(user.age, 18), `adult`, `minor`)
966```
967
968```ts
969caseWhen(
970 gt(user.age, 65),
971 `senior`,
972 gt(user.age, 18),
973 `adult`,
974 `minor`,
975)
976```
977
978```ts
979caseWhen(gt(user.age, 18), {
980 ...user,
981 posts: q
982 .from({ post: postsCollection })
983 .where(({ post }) => eq(post.userId, user.id)),
984})
985```
986
987## Call Signature
988
989```ts
990function caseWhen<C1, V1, C2, V2, C3, V3, C4, V4, C5, V5, D>(
991 condition1,
992 value1,
993 condition2,
994 value2,
995 condition3,
996 value3,
997 condition4,
998 value4,
999 condition5,
1000 value5,
1001defaultValue): CaseWhenResult<[V1, V2, V3, V4, V5, D], true>;
1002```
1003
1004Defined in: [packages/db/src/query/builder/functions.ts:527](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/functions.ts#L527)
1005
1006Returns the value for the first matching condition, similar to SQL
1007`CASE WHEN`.
1008
1009Arguments are evaluated as condition/value pairs followed by an optional
1010default value. Scalar branch values return a query expression and can be used
1011in expression contexts like `select`, `where`, `orderBy`, `groupBy`,
1012`having`, and equality join operands. If no scalar branch matches and no
1013default is provided, the result is `null`.
1014
1015When a branch value is a projection object, `caseWhen` becomes a select-only
1016projection value. Projection branches can include nested fields, ref spreads,
1017and includes. If no projection branch matches and no default is provided, the
1018result is `undefined`.
1019
1020### Type Parameters
1021
1022#### C1
1023
1024`C1` *extends* `ExpressionLike`
1025
1026#### V1
1027
1028`V1` *extends* `CaseWhenValue`
1029
1030#### C2
1031
1032`C2` *extends* `ExpressionLike`
1033
1034#### V2
1035
1036`V2` *extends* `CaseWhenValue`
1037
1038#### C3
1039
1040`C3` *extends* `ExpressionLike`
1041
1042#### V3
1043
1044`V3` *extends* `CaseWhenValue`
1045
1046#### C4
1047
1048`C4` *extends* `ExpressionLike`
1049
1050#### V4
1051
1052`V4` *extends* `CaseWhenValue`
1053
1054#### C5
1055
1056`C5` *extends* `ExpressionLike`
1057
1058#### V5
1059
1060`V5` *extends* `CaseWhenValue`
1061
1062#### D
1063
1064`D` *extends* `CaseWhenValue`
1065
1066### Parameters
1067
1068#### condition1
1069
1070`C1`
1071
1072#### value1
1073
1074`V1`
1075
1076#### condition2
1077
1078`C2`
1079
1080#### value2
1081
1082`V2`
1083
1084#### condition3
1085
1086`C3`
1087
1088#### value3
1089
1090`V3`
1091
1092#### condition4
1093
1094`C4`
1095
1096#### value4
1097
1098`V4`
1099
1100#### condition5
1101
1102`C5`
1103
1104#### value5
1105
1106`V5`
1107
1108#### defaultValue
1109
1110`D`
1111
1112### Returns
1113
1114`CaseWhenResult`\<\[`V1`, `V2`, `V3`, `V4`, `V5`, `D`\], `true`\>
1115
1116### Examples
1117
1118```ts
1119caseWhen(gt(user.age, 18), `adult`, `minor`)
1120```
1121
1122```ts
1123caseWhen(
1124 gt(user.age, 65),
1125 `senior`,
1126 gt(user.age, 18),
1127 `adult`,
1128 `minor`,
1129)
1130```
1131
1132```ts
1133caseWhen(gt(user.age, 18), {
1134 ...user,
1135 posts: q
1136 .from({ post: postsCollection })
1137 .where(({ post }) => eq(post.userId, user.id)),
1138})
1139```
1140
1141## Call Signature
1142
1143```ts
1144function caseWhen<C1, V1, C2, V2, C3, V3, C4, V4, C5, V5>(
1145 condition1,
1146 value1,
1147 condition2,
1148 value2,
1149 condition3,
1150 value3,
1151 condition4,
1152 value4,
1153 condition5,
1154 value5,
1155 condition6,
1156 value6, ...
1157 rest): any;
1158```
1159
1160Defined in: [packages/db/src/query/builder/functions.ts:552](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/functions.ts#L552)
1161
1162Returns the value for the first matching condition, similar to SQL
1163`CASE WHEN`.
1164
1165Arguments are evaluated as condition/value pairs followed by an optional
1166default value. Scalar branch values return a query expression and can be used
1167in expression contexts like `select`, `where`, `orderBy`, `groupBy`,
1168`having`, and equality join operands. If no scalar branch matches and no
1169default is provided, the result is `null`.
1170
1171When a branch value is a projection object, `caseWhen` becomes a select-only
1172projection value. Projection branches can include nested fields, ref spreads,
1173and includes. If no projection branch matches and no default is provided, the
1174result is `undefined`.
1175
1176### Type Parameters
1177
1178#### C1
1179
1180`C1` *extends* `ExpressionLike`
1181
1182#### V1
1183
1184`V1` *extends* `CaseWhenValue`
1185
1186#### C2
1187
1188`C2` *extends* `ExpressionLike`
1189
1190#### V2
1191
1192`V2` *extends* `CaseWhenValue`
1193
1194#### C3
1195
1196`C3` *extends* `ExpressionLike`
1197
1198#### V3
1199
1200`V3` *extends* `CaseWhenValue`
1201
1202#### C4
1203
1204`C4` *extends* `ExpressionLike`
1205
1206#### V4
1207
1208`V4` *extends* `CaseWhenValue`
1209
1210#### C5
1211
1212`C5` *extends* `ExpressionLike`
1213
1214#### V5
1215
1216`V5` *extends* `CaseWhenValue`
1217
1218### Parameters
1219
1220#### condition1
1221
1222`C1`
1223
1224#### value1
1225
1226`V1`
1227
1228#### condition2
1229
1230`C2`
1231
1232#### value2
1233
1234`V2`
1235
1236#### condition3
1237
1238`C3`
1239
1240#### value3
1241
1242`V3`
1243
1244#### condition4
1245
1246`C4`
1247
1248#### value4
1249
1250`V4`
1251
1252#### condition5
1253
1254`C5`
1255
1256#### value5
1257
1258`V5`
1259
1260#### condition6
1261
1262`ExpressionLike`
1263
1264#### value6
1265
1266`CaseWhenValue`
1267
1268#### rest
1269
1270...`CaseWhenValue`[]
1271
1272### Returns
1273
1274`any`
1275
1276### Examples
1277
1278```ts
1279caseWhen(gt(user.age, 18), `adult`, `minor`)
1280```
1281
1282```ts
1283caseWhen(
1284 gt(user.age, 65),
1285 `senior`,
1286 gt(user.age, 18),
1287 `adult`,
1288 `minor`,
1289)
1290```
1291
1292```ts
1293caseWhen(gt(user.age, 18), {
1294 ...user,
1295 posts: q
1296 .from({ post: postsCollection })
1297 .where(({ post }) => eq(post.userId, user.id)),
1298})
1299```